#include "logger.h" #include "../defines.h" #include #include #include typedef enum level_color { COLOR_FATAL, COLOR_ERROR, COLOR_WARN, COLOR_INFO, COLOR_DEBUG, } level_color; void send_to_console(const char *message, level_color color) { const char *color_strings[] = {"41;97", "0;91", "0;93", "0;94", "0;92"}; printf("\033[%sm%s\033[0m\n", color_strings[color], message); } void send_to_error_log(const char *message) { // TODO: Add in file handling here so that we can write the log messages // to file as well instead of just to console printf("send_to_error_log() called...\n"); } void log_output(log_level level, const char *message, ...) { const char *level_strings[] = { "[FATAL]: ", "[ERROR]: ", "[WARN]: ", "[INFO]: ", "[DEBUG]: "}; // We are going to impose a 32K char limit on a single log entry // we will hold this in format_message as a place to collect all // the variadic arguments and process them together. We make sure // that the memory is cleared to 0 before we start working on it. const i32 msg_length = 32000; char format_message[msg_length]; memset(format_message, 0, sizeof(format_message)); // Now we need to assemble the message from the message + // the variadic arguments __builtin_va_list arg_ptr; va_start(arg_ptr, message); vsnprintf(format_message, msg_length, message, arg_ptr); va_end(arg_ptr); // Now we prepend the message with our message level char log_message[msg_length]; sprintf(log_message, "%s%s", level_strings[level], format_message); // Now we call the appropriate function depending on the level if (level <= 1) { send_to_console(log_message, level == FATAL ? COLOR_FATAL : COLOR_ERROR); send_to_error_log(log_message); } else { send_to_console(log_message, (level_color)level); } }