~sircmpwn/xrgears

ref: 1ac9460a3fe49c9a47bc6d8262e124fd3d369758 xrgears/vitamin-k/system/vikLog.hpp -rw-r--r-- 7.7 KiB
1ac9460aDrew DeVault Add wayland-direct window mode 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
 * vitamin-k
 *
 * Copyright 2017-2018 Collabora Ltd.
 *
 * Authors: Lubosz Sarnecki <lubosz.sarnecki@collabora.com>
 * SPDX-License-Identifier: MIT
 */

#pragma once

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <cstring>
#include <iostream>

#include "vulkan/vulkan.h"

#define LOG_TO_STD_ERR 0

#define vik_log(...) vik::Log::log(__FILE__, __LINE__, __VA_ARGS__)
#define vik_log_d(...) vik_log(vik::Log::DEBUG, __VA_ARGS__)
#define vik_log_i(...) vik_log(vik::Log::INFO, __VA_ARGS__)
#define vik_log_w(...) vik_log(vik::Log::WARNING, __VA_ARGS__)
#define vik_log_e(...) vik_log(vik::Log::ERROR, __VA_ARGS__)
#define vik_log_f(...) vik_log(vik::Log::FATAL, __VA_ARGS__)
#define vik_log_if(...) vik::Log::log_if(__FILE__, __LINE__, __VA_ARGS__)
#define vik_log_f_if(...) vik_log_if(vik::Log::FATAL, __VA_ARGS__)
#define vik_log_e_if(...) vik_log_if(vik::Log::ERROR, __VA_ARGS__)
#define vik_log_i_short(...) vik::Log::log_short(vik::Log::INFO, __VA_ARGS__)

// Macro to check and display Vulkan return results
#define vik_log_check(f) {\
  VkResult res = (f);\
  vik_log_f_if(res != VK_SUCCESS, "VkResult is %s", vik::Log::result_string(res).c_str());\
}

#define ENUM_TO_STR(r) case r: return #r
#define STR_TO_ENUM(r) if (str.compare(#r) == 0) return r

namespace vik {
class Log {
  static const bool use_color = true;

 public:
  enum type {
    DEBUG = 0,
    INFO,
    WARNING,
    ERROR,
    FATAL
  };

  /** @brief Returns an result code as a string */
  static std::string result_string(VkResult code) {
    switch (code) {
      ENUM_TO_STR(VK_SUCCESS);
      ENUM_TO_STR(VK_NOT_READY);
      ENUM_TO_STR(VK_TIMEOUT);
      ENUM_TO_STR(VK_EVENT_SET);
      ENUM_TO_STR(VK_EVENT_RESET);
      ENUM_TO_STR(VK_INCOMPLETE);
      ENUM_TO_STR(VK_ERROR_OUT_OF_HOST_MEMORY);
      ENUM_TO_STR(VK_ERROR_OUT_OF_DEVICE_MEMORY);
      ENUM_TO_STR(VK_ERROR_INITIALIZATION_FAILED);
      ENUM_TO_STR(VK_ERROR_DEVICE_LOST);
      ENUM_TO_STR(VK_ERROR_MEMORY_MAP_FAILED);
      ENUM_TO_STR(VK_ERROR_LAYER_NOT_PRESENT);
      ENUM_TO_STR(VK_ERROR_EXTENSION_NOT_PRESENT);
      ENUM_TO_STR(VK_ERROR_FEATURE_NOT_PRESENT);
      ENUM_TO_STR(VK_ERROR_INCOMPATIBLE_DRIVER);
      ENUM_TO_STR(VK_ERROR_TOO_MANY_OBJECTS);
      ENUM_TO_STR(VK_ERROR_FORMAT_NOT_SUPPORTED);
      ENUM_TO_STR(VK_ERROR_SURFACE_LOST_KHR);
      ENUM_TO_STR(VK_ERROR_NATIVE_WINDOW_IN_USE_KHR);
      ENUM_TO_STR(VK_SUBOPTIMAL_KHR);
      ENUM_TO_STR(VK_ERROR_OUT_OF_DATE_KHR);
      ENUM_TO_STR(VK_ERROR_INCOMPATIBLE_DISPLAY_KHR);
      ENUM_TO_STR(VK_ERROR_VALIDATION_FAILED_EXT);
      ENUM_TO_STR(VK_ERROR_INVALID_SHADER_NV);
      default:
        return "UNKNOWN RESULT";
    }
  }

  static std::string color_format_string(VkFormat code) {
    switch(code) {
      ENUM_TO_STR(VK_FORMAT_B8G8R8A8_UNORM);
      ENUM_TO_STR(VK_FORMAT_UNDEFINED);
      ENUM_TO_STR(VK_FORMAT_R8G8B8A8_SRGB);
      ENUM_TO_STR(VK_FORMAT_B8G8R8A8_SRGB);
      ENUM_TO_STR(VK_FORMAT_R8G8B8_SRGB);
      ENUM_TO_STR(VK_FORMAT_B8G8R8_SRGB);
      ENUM_TO_STR(VK_FORMAT_R5G6B5_UNORM_PACK16);
      ENUM_TO_STR(VK_FORMAT_B5G6R5_UNORM_PACK16);
      ENUM_TO_STR(VK_FORMAT_D32_SFLOAT_S8_UINT);
      ENUM_TO_STR(VK_FORMAT_D32_SFLOAT);
      ENUM_TO_STR(VK_FORMAT_D24_UNORM_S8_UINT);
      ENUM_TO_STR(VK_FORMAT_D16_UNORM_S8_UINT);
      ENUM_TO_STR(VK_FORMAT_D16_UNORM);
      default:
        return "UNKNOWN FORMAT";
    }
  }

  static std::string present_mode_string(VkPresentModeKHR code) {
    switch(code) {
      ENUM_TO_STR(VK_PRESENT_MODE_FIFO_KHR);
      ENUM_TO_STR(VK_PRESENT_MODE_MAILBOX_KHR);
      ENUM_TO_STR(VK_PRESENT_MODE_IMMEDIATE_KHR);
      ENUM_TO_STR(VK_PRESENT_MODE_FIFO_RELAXED_KHR);
      ENUM_TO_STR(VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR);
      ENUM_TO_STR(VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR);
      default:
        vik_log_w("Unkown mode %d", code);
        return "UNKNOWN MODE";
    }
  }

  static std::string power_state_string(VkDisplayPowerStateEXT code) {
    switch(code) {
      ENUM_TO_STR(VK_DISPLAY_POWER_STATE_OFF_EXT);
      ENUM_TO_STR(VK_DISPLAY_POWER_STATE_SUSPEND_EXT);
      ENUM_TO_STR(VK_DISPLAY_POWER_STATE_ON_EXT);
      default:
        vik_log_w("Unkown mode %d", code);
        return "UNKNOWN MODE";
    }
  }

  static VkPresentModeKHR string_to_present_mode(const std::string& str) {
    STR_TO_ENUM(VK_PRESENT_MODE_FIFO_KHR);
    STR_TO_ENUM(VK_PRESENT_MODE_MAILBOX_KHR);
    STR_TO_ENUM(VK_PRESENT_MODE_IMMEDIATE_KHR);
    vik_log_w("Unknown present mode %s", str.c_str());
    return VK_PRESENT_MODE_FIFO_KHR;
  }

  static VkFormat string_to_color_format(const std::string& str) {
    STR_TO_ENUM(VK_FORMAT_B8G8R8A8_UNORM);
    STR_TO_ENUM(VK_FORMAT_UNDEFINED);
    STR_TO_ENUM(VK_FORMAT_R8G8B8A8_SRGB);
    STR_TO_ENUM(VK_FORMAT_B8G8R8A8_SRGB);
    STR_TO_ENUM(VK_FORMAT_R8G8B8_SRGB);
    STR_TO_ENUM(VK_FORMAT_B8G8R8_SRGB);
    STR_TO_ENUM(VK_FORMAT_R5G6B5_UNORM_PACK16);
    STR_TO_ENUM(VK_FORMAT_B5G6R5_UNORM_PACK16);
    vik_log_w("Unknown format %s", str.c_str());
    return VK_FORMAT_UNDEFINED;
  }

  static std::string color_space_string(VkColorSpaceKHR code) {
    switch(code) {
      ENUM_TO_STR(VK_COLORSPACE_SRGB_NONLINEAR_KHR);
      default:
        return "UNKNOWN COLOR SPACE";
    }
  }

  static const char* type_str(type t) {
    switch (t) {
      case DEBUG:
        return "d";
      case INFO:
        return "i";
      case WARNING:
        return "w";
      case ERROR:
        return "e";
      case FATAL:
        return "fatal";
      default:
        return "?";
    }
  }

  static int type_color(type t) {
    switch (t) {
      case DEBUG:
        return 36;
      case INFO:
        return 32;
      case WARNING:
        return 33;
      case ERROR:
      case FATAL:
        return 31;
      default:
        return 36;
    }
  }

  static FILE *type_stream(type t) {
#ifdef LOG_TO_STD_ERR
    return stderr;
#endif
    switch (t) {
      case DEBUG:
      case INFO:
      case WARNING:
        return stdout;
      case ERROR:
      case FATAL:
        return stderr;
    }
  }

  static std::string color_code(int code) {
    if (!use_color)
      return "";
    char code_str[7];
    snprintf(code_str, sizeof(code_str), "\e[%dm", code);
    return std::string(code_str);
  }

  static std::string strip_file_name(const char* file) {
    std::string striped_path = std::strrchr(file, '/') + 1;
    size_t lastindex = striped_path.find_last_of(".");
    return striped_path.substr(0, lastindex);
  }

  static void log(const char* file, int line, type t, const char *format, ...) {
    va_list args;
    va_start(args, format);
    log_values(file, line, t, format, args);
    va_end(args);
  }

  static void log_short(type t, const char *format, ...) {
    va_list args;
    va_start(args, format);
    log_short_values(t, format, args);
    va_end(args);
  }

  static void log_values(const char* file, int line, type t, const char *format, va_list args) {
    FILE *stream = type_stream(t);
    fprintf(stream, "%s[%s]%s ",
            color_code(type_color(t)).c_str(),
            type_str(t),
            color_code(0).c_str());
    fprintf(stream, "%s:%d | ", strip_file_name(file).c_str(), line);
    vfprintf(stream, format, args);
    fprintf(stream, "\n");
    if (t == FATAL)
      exit(1);
  }

  static void log_short_values(type t, const char *format, va_list args) {
    FILE *stream = type_stream(t);
    fprintf(stream, "%s[%s]%s ",
            color_code(type_color(t)).c_str(),
            type_str(t),
            color_code(0).c_str());
    vfprintf(stream, format, args);
    fprintf(stream, "\n");
  }

  static void log_if(const char* file, int line, type t, bool cond, const char *format, ...) {
    if (!cond)
      return;
    va_list args;
    va_start(args, format);
    log_values(file, line, t, format, args);
    va_end(args);
  }
};
}  // namespace vik