Ylva And Malin
logger.h
Go to the documentation of this file.
1 
41 #pragma once
42 #include <cstdarg>
43 #include <cstdio>
44 #include <cstdlib>
45 #include <cstring>
46 #include <vector>
47 
48 #ifdef _WIN32
49 #include <Windows.h>
50 #endif
51 
52 #define LOG_COLOR_RESET "\033[0m"
53 #define LOG_COLOR_FG_BLACK "\033[0;30m"
54 #define LOG_COLOR_FG_RED "\033[0;31m"
55 #define LOG_COLOR_FG_GREEN "\033[0;32m"
56 #define LOG_COLOR_FG_YELLOW "\033[0;33m"
57 #define LOG_COLOR_FG_BLUE "\033[0;34m"
58 #define LOG_COLOR_FG_MAGENTA "\033[0;35m"
59 #define LOG_COLOR_FG_CYAN "\033[0;36m"
60 #define LOG_COLOR_FG_GREY "\033[0;37m"
61 #define LOG_COLOR_FG_WHITE "\033[0m"
62 
63 #define LOG_COLOR_BG_BLACK "\033[0;40m"
64 #define LOG_COLOR_BG_RED "\033[0;41m"
65 #define LOG_COLOR_BG_GREEN "\033[0;42m"
66 #define LOG_COLOR_BG_YELLOW "\033[0;43m"
67 #define LOG_COLOR_BG_BLUE "\033[0;44m"
68 #define LOG_COLOR_BG_MAGENTA "\033[0;45m"
69 #define LOG_COLOR_BG_CYAN "\033[0;46m"
70 #define LOG_COLOR_BG_GREY "\033[0;47m"
71 #define LOG_COLOR_BG_WHITE "\033[0m"
72 
73 
84 void
85 log_internal(FILE* file,
86  const char* type,
87  const char* color,
88  const char* filepath,
89  const char* func,
90  const int line,
91  const char* fmt,
92  ...);
93 
98 #ifdef _WIN32
99 #define LOG_TERMINAL_PAUSE system("pause");
100 #else
101 #define LOG_TERMINAL_PAUSE
102 #endif
103 
113 #define LOG_ERROR(fmt, ...) \
114 { \
115  log_internal(stderr, "ERROR", LOG_COLOR_FG_RED, __FILE__, __func__, __LINE__, fmt, ##__VA_ARGS__); \
116  LOG_TERMINAL_PAUSE; \
117  std::exit(EXIT_FAILURE); \
118 }
119 
125 #define LOG_WARN(fmt, ...) \
126 log_internal(stderr, "WARN", LOG_COLOR_FG_YELLOW, __FILE__, __func__, __LINE__, fmt, ##__VA_ARGS__);
127 
137 #ifndef LOG_NO_DEBUG
138 #define LOG_DEBUG(fmt, ...) \
139 log_internal(stdout, "DEBUG", LOG_COLOR_FG_CYAN, __FILE__, __func__, __LINE__, fmt, ##__VA_ARGS__);
140 
141 #else
142 #define LOG_DEBUG(fmt, ...)
143 #endif
144 
149 #define LOG_INFO(fmt, ...) \
150 log_internal(stdout, "INFO", LOG_COLOR_FG_WHITE, __FILE__, __func__, __LINE__, fmt, ##__VA_ARGS__);
151 
152 #ifdef _WIN32
153 inline
154 bool
155 log_internal_init_for_windows()
156 {
157  const auto outputs = { STD_OUTPUT_HANDLE, STD_ERROR_HANDLE };
158  for (const auto& item : outputs)
159  {
160  HANDLE handle = GetStdHandle(item);
161  if (handle == INVALID_HANDLE_VALUE)
162  return false;
163 
164  DWORD mode;
165  if (!GetConsoleMode(handle, &mode))
166  return false;
167 
168  mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
169  if (!SetConsoleMode(handle, mode))
170  return false;
171  }
172 
173  return true;
174 }
175 #endif
176 
177 inline
178 void
179 log_internal(std::FILE* file,
180  const char* type,
181  const char* color,
182  const char* filepath,
183  const char* func,
184  const int line,
185  const char* fmt,
186  ...)
187 {
188  // Enable virtual terminal in windows for text color
189  #ifdef _WIN32
190  static bool initialized = false;
191  if (!initialized)
192  {
193  if (!log_internal_init_for_windows())
194  {
195  std::fprintf(stderr, "Could not enable virtual terminal processing, you will not get any terminal colors\n");
196  }
197 
198  initialized = true;
199  }
200  #endif
201 
202  // Logging logic
203  va_list args1;
204  va_start(args1, fmt);
205  va_list args2;
206  va_copy(args2, args1);
207 
208  std::size_t size = 1 + std::vsnprintf(nullptr, 0, fmt, args1);
209  std::vector<char> buffer(size);
210 
211  va_end(args1);
212  std::vsnprintf(buffer.data(), size, fmt, args2);
213  va_end(args2);
214 
215  #ifdef _WIN32
216  const char* filename = std::strrchr(filepath, '\\');
217  #else
218  const char* filename = std::strrchr(filepath, '/');
219  #endif
220 
221  filename = (filename)
222  ? filename + 1 // Increment past '/' or '\\'
223  : filepath;
224 
225  std::fprintf(file, "%s[%-5s]%s: %-10s: %-10s:%3d: %s\n",
226  color, type, LOG_COLOR_RESET,
227  filename, func,
228  line, buffer.data());
229 
230  std::fflush(file);
231 }
GLint color
Definition: ym_gfx_sprite.c:19
typedef HANDLE(WINAPI *PFNWGLCREATEBUFFERREGIONARBPROC)(HDC hDC
int GLenum UINT size
Definition: wglext.h:321
#define LOG_COLOR_RESET
Logger.h ////////////////////////////////////////////////////////////////////////////////////////////...
Definition: logger.h:52
void log_internal(FILE *file, const char *type, const char *color, const char *filepath, const char *func, const int line, const char *fmt,...)
Actual function which is called by log macros.
#define va_copy(dest, src)
Definition: imgui.cpp:1791
void GLuint GLenum type
Definition: wglext.h:636