logger.c (902B)
1 #include "logger.h" 2 #include "types.h" 3 4 #include <stdarg.h> 5 #include <stdio.h> 6 #include <string.h> 7 8 #define MAX_LOG_LENGTH 4096 9 10 void log_msg(log_level level, const char *message, ...) 11 { 12 char log_msg[MAX_LOG_LENGTH]; 13 memset(log_msg, 0, sizeof(log_msg)); 14 15 // Prefix with log level 16 const char *level_strings[6] = { 17 "[TRACE]: ", 18 "[DEBUG]: ", 19 "[INFO ]: ", 20 "[WARN ]: ", 21 "[ERROR]: ", 22 "[FATAL]: ", 23 }; 24 const char *prefix = level_strings[level]; 25 u32 prefix_length = strlen(prefix); 26 strncpy(log_msg, prefix, prefix_length); 27 28 // Append message 29 va_list arg_ptr; 30 va_start(arg_ptr, message); 31 vsnprintf( 32 log_msg + prefix_length, 33 MAX_LOG_LENGTH - prefix_length, 34 message, 35 arg_ptr 36 ); 37 va_end(arg_ptr); 38 39 // Print log message 40 fprintf(stderr, "%s\n", log_msg); 41 } 42