Ember
A C++ 20 'game engine' built with SDL3 with wide platform support.
logging.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "imports.h"
4 
5 #define STRINGIFY(x) #x
6 #define TO_STRING(x) STRINGIFY(x)
7 #define TRACE_FILE_LOG "[" __TIME__ "]" "[EMBER_ENGINE - " __FILE__ ":" TO_STRING(__LINE__)"] - "
8 
9 /*
10  @brief Class for logging, tracing and debugging
11 
12  - Web - Output to javascript console
13  - Windows - Output to console and file
14  - macOS - Output to xcode console
15  - Linux - Output to console and file
16  - Android - Output to logcat
17  - iOS - Output to xcode console
18 
19  Use the LOG_ERROR, LOG_INFO, LOG_WARN, LOG_DEBUG macros to log messages
20 
21  @note Do not push sensitive data to the logs
22 
23  @version 0.0.9
24 
25 */
26 class Logger {
27 public:
28  static Logger& Get() {
29  static Logger instance;
30  return instance;
31  }
32 
33  static void Start();
34 
35  void Push(const std::string& formatted_log);
36 
37  static void Destroy();
38 
39 private:
40  Logger() = default;
41  ~Logger() = default;
42 
43  void LogThread();
44 
45  std::mutex _mutex = std::mutex();
46  SDL_Thread* _log_thread = nullptr;
47  std::condition_variable _condition = std::condition_variable();
48  std::atomic<bool> _bIsRunning = false;
49  std::deque<std::string> _log_queue = std::deque<std::string>();
50 };
51 
52 
60 #define LOG_ERROR(...) \
61  do { \
62  char buffer[1024]; \
63  SDL_snprintf(buffer, sizeof(buffer), TRACE_FILE_LOG __VA_ARGS__); \
64  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%s", buffer); \
65  Logger::Get().Push(buffer); \
66  } while (0)
67 
75 #define LOG_INFO(...) \
76  do { \
77  char buffer[1024]; \
78  SDL_snprintf(buffer, sizeof(buffer), TRACE_FILE_LOG __VA_ARGS__); \
79  SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "%s", buffer); \
80  Logger::Get().Push(buffer); \
81  } while (0)
82 
90 #define LOG_DEBUG(...) \
91  do { \
92  char buffer[1024]; \
93  SDL_snprintf(buffer, sizeof(buffer), TRACE_FILE_LOG __VA_ARGS__); \
94  SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "%s", buffer); \
95  Logger::Get().Push(buffer); \
96  } while (0)
97 
105 #define LOG_VERBOSE(...) \
106  do { \
107  char buffer[1024]; \
108  SDL_snprintf(buffer, sizeof(buffer), TRACE_FILE_LOG __VA_ARGS__); \
109  SDL_LogVerbose(SDL_LOG_CATEGORY_APPLICATION, "%s", buffer); \
110  Logger::Get().Push(buffer); \
111  } while (0)
112 
120 #define LOG_WARN(...) \
121  do { \
122  char buffer[1024]; \
123  SDL_snprintf(buffer, sizeof(buffer), TRACE_FILE_LOG __VA_ARGS__); \
124  SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "%s", buffer); \
125  Logger::Get().Push(buffer); \
126  } while (0)
127 
135 #define LOG_TRACE(...) \
136  do { \
137  char buffer[1024]; \
138  SDL_snprintf(buffer, sizeof(buffer), TRACE_FILE_LOG __VA_ARGS__); \
139  SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "%s", buffer); \
140  Logger::Get().Push(buffer); \
141  } while (0)
142 
150 #define LOG_CRITICAL(...) \
151  do { \
152  char buffer[1024]; \
153  SDL_snprintf(buffer, sizeof(buffer), TRACE_FILE_LOG __VA_ARGS__); \
154  SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, "%s", buffer); \
155  Logger::Get().Push(buffer); \
156  } while (0)
157 
165 #define LOG_QUIT_ON_FAIL(x) \
166  if (!x) { \
167  LOG_ERROR("%s", SDL_GetError()); \
168  return SDL_APP_FAILURE; \
169  }
170 
171 
178 #define GL_ERROR() \
179  { \
180  unsigned int error = glGetError(); \
181  if (error != GL_NO_ERROR) { \
182  LOG_ERROR("API ERROR_CODE: %d", error); \
183  } \
184  }
185 
192 #define EMBER_TIMER_START() auto start = std::chrono::high_resolution_clock::now();
193 
194 
201 #define EMBER_TIMER_END(description) \
202  do { \
203  auto end = std::chrono::high_resolution_clock::now(); \
204  auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count(); \
205  LOG_INFO("%s took %lld (us), %.2f (ms)", description, duration, (float) duration / 1000.f); \
206  } while (0)
Definition: logging.h:26
static void Start()
Definition: logging.cpp:3
void Push(const std::string &formatted_log)
Definition: logging.cpp:19
static void Destroy()
Definition: logging.cpp:26
static Logger & Get()
Definition: logging.h:28