kiba-engine
console.c
Go to the documentation of this file.
1 
7 
8 #include <kiba/core/log.h>
9 
10 #include <execinfo.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 
14 static const i32 MAX_STACKTRACE_SIZE = 1024;
15 
16 static const char *colors[LOG_LEVEL_MAX] = {
17  "0;41", // LOG_LEVEL_FATAL
18  "1;31", // LOG_LEVEL_ERROR
19  "1;33", // LOG_LEVEL_WARN
20  "1;32", // LOG_LEVEL_INFO
21  "1;34", // LOG_LEVEL_DEBUG
22  "1;30" // LOG_LEVEL_TRACE
23 };
24 
25 void platform_console_write(const char *msg, u8 color) {
26  KB_ASSERT(color < LOG_LEVEL_MAX, "color index must not exceed available log levels");
27  fprintf(stdout, "\033[%sm%s\033[0m", colors[color], msg);
28 }
29 
30 void platform_console_write_error(const char *msg, u8 color) {
31  KB_ASSERT(color < LOG_LEVEL_MAX, "color index must not exceed available log levels");
32  fprintf(stderr, "\033[%sm%s\033[0m", colors[color], msg);
33 }
34 
36  void *array[MAX_STACKTRACE_SIZE];
37  i32 size = backtrace(array, MAX_STACKTRACE_SIZE);
38  char **strings = backtrace_symbols(array, size);
39  for (i32 i = 0; i < size; ++i) {
40  fprintf(stdout, "%3d : %s\n", i, strings[i]);
41  }
42  free(strings);
43 }
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
Interface to access platform specific console printing functionality.
Logging system.
#define KB_ASSERT(expr,...)
Perform runtime assertion and log failures.
Definition: log.h:133