kiba-engine
log.h
Go to the documentation of this file.
1 
6 #pragma once
7 
8 #include <kiba/core/config.h>
9 
10 #include <assert.h>
11 
12 // TODO add to configuration header
13 #define KB_MAX_LOG_ENTRY_SIZE 8192
14 
16 typedef enum log_level {
17  LOG_LEVEL_FATAL,
18  LOG_LEVEL_ERROR,
19  LOG_LEVEL_WARN,
20  LOG_LEVEL_INFO,
21  LOG_LEVEL_DEBUG,
22  LOG_LEVEL_TRACE,
23  LOG_LEVEL_MAX
25 
33 KB_API void enable_stacktraces(log_level level);
34 
38 KB_API void disable_stacktraces(void);
39 
47 KB_API void log_set_active_level(log_level level);
48 
59 void write_log_entry(log_level level, const char *file, long int line, const char *msg, ...);
60 
68 void write_to_console(log_level level, const char *msg);
69 
75 #define STATIC_ASSERT static_assert
76 
133 #define KB_ASSERT(expr, ...) \
134  do { \
135  if (expr) { \
136  } else { \
137  write_log_entry(LOG_LEVEL_FATAL, __FILE__, __LINE__, "assertion for " #expr " failed: " __VA_ARGS__); \
138  DEBUG_BREAK; \
139  } \
140  } while (0)
141 #define KB_FATAL(...) write_log_entry(LOG_LEVEL_FATAL, __FILE__, __LINE__, __VA_ARGS__)
142 #define KB_ERROR(...) write_log_entry(LOG_LEVEL_ERROR, __FILE__, __LINE__, __VA_ARGS__)
143 
144 #ifdef KB_DEBUG_BUILD
145 # define KB_WARN(...) write_log_entry(LOG_LEVEL_WARN, __FILE__, __LINE__, __VA_ARGS__)
146 # define KB_INFO(...) write_log_entry(LOG_LEVEL_INFO, __FILE__, __LINE__, __VA_ARGS__)
147 # define KB_DEBUG(...) write_log_entry(LOG_LEVEL_DEBUG, __FILE__, __LINE__, __VA_ARGS__)
148 # define KB_TRACE(...) write_log_entry(LOG_LEVEL_TRACE, __FILE__, __LINE__, __VA_ARGS__)
149 # define KB_ASSERT_DEBUG(expr, ...) \
150  do { \
151  if (expr) { \
152  } else { \
153  write_log_entry(LOG_LEVEL_FATAL, \
154  __FILE__, \
155  __LINE__, \
156  "debug assertion for " #expr " failed: " __VA_ARGS__); \
157  DEBUG_BREAK; \
158  } \
159  } while (0)
160 #else
161 # define KB_WARN(...)
162 # define KB_INFO(...)
163 # define KB_DEBUG(...)
164 # define KB_TRACE(...)
165 # define KB_ASSERT_DEBUG(expr, ...) \
166  do { \
167  b8 _res = expr; \
168  UNUSED(_res); \
169  } while (0)
170 #endif
KB_API void disable_stacktraces(void)
Disables output of the stacktrace.
Definition: log.c:27
KB_API void log_set_active_level(log_level level)
Set the minimum log level to log.
Definition: log.c:29
void write_log_entry(log_level level, const char *file, long int line, const char *msg,...)
Create log entry and write to output channel.
Definition: log.c:31
KB_API void enable_stacktraces(log_level level)
Enables output of the stacktrace.
Definition: log.c:25
log_level
Available log levels.
Definition: log.h:16
void write_to_console(log_level level, const char *msg)
Write string to console.
Definition: log.c:49