kiba-engine
log.c
Go to the documentation of this file.
1 
6 #include <kiba/core/log.h>
7 
8 #include <kiba/core/memory.h>
9 #include <kiba/format/format.h>
10 #include <kiba/platform/console.h>
11 
13 static const char *level_names[LOG_LEVEL_MAX] = {
14  "[FATAL]", // LOG_LEVEL_FATAL
15  "[ERROR]", // LOG_LEVEL_ERROR
16  "[WARN ]", // LOG_LEVEL_WARN
17  "[INFO ]", // LOG_LEVEL_INFO
18  "[DEBUG]", // LOG_LEVEL_DEBUG
19  "[TRACE]" // LOG_LEVEL_TRACE
20 };
21 
22 static log_level stacktrace_level = LOG_LEVEL_MAX;
23 static log_level active_log_level = LOG_LEVEL_TRACE;
24 
25 void enable_stacktraces(log_level level) { stacktrace_level = level; }
26 
27 void disable_stacktraces(void) { stacktrace_level = LOG_LEVEL_MAX; }
28 
29 void log_set_active_level(log_level level) { active_log_level = level; }
30 
31 void write_log_entry(log_level level, const char *file, long int line, const char *msg, ...) {
32  if (active_log_level != LOG_LEVEL_MAX && level <= active_log_level) {
33  char log_message[KB_MAX_LOG_ENTRY_SIZE] = {0};
34  format_buffer buf;
35  VA_LIST args;
36  VA_START(args, msg);
37  if (format_buffer_create_static(&buf, FORMAT_BUFFER_WRITE, log_message, KB_MAX_LOG_ENTRY_SIZE)
38  && format(&buf, "{raw_string} {raw_string}:{i64} ", level_names[level], file, line)
39  && format_va_list(&buf, msg, &args) && format_buffer_add_char(&buf, '\n')) {
40  write_to_console(level, log_message);
41  if (stacktrace_level != LOG_LEVEL_MAX && level <= stacktrace_level) {
43  }
44  }
45  VA_END(args);
46  }
47 }
48 
49 void write_to_console(log_level level, const char *msg) {
50  if (level < LOG_LEVEL_WARN) {
51  platform_console_write_error(msg, (u8) level);
52  } else {
53  platform_console_write(msg, (u8) level);
54  }
55 }
Interface to access platform specific console printing functionality.
void platform_print_stacktrace(void)
Print the the call stack of the caller.
Definition: console.c:35
void platform_console_write(const char *msg, u8 color)
Write message to the platforms console.
Definition: console.c:25
void platform_console_write_error(const char *msg, u8 color)
Write message to the platforms error console.
Definition: console.c:30
Lightweight layer between platform and other engine components to enable tracing/monitoring.
void disable_stacktraces(void)
Disables output of the stacktrace.
Definition: log.c:27
void log_set_active_level(log_level level)
Set the minimum log level to log.
Definition: log.c:29
void enable_stacktraces(log_level level)
Enables output of the stacktrace.
Definition: log.c:25
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
void write_to_console(log_level level, const char *msg)
Write string to console.
Definition: log.c:49
Logging system.
log_level
Available log levels.
Definition: log.h:16