Got a very basic logger up and working. Can be used with the Q[LEVEL]
macros. Still needs some work to add coloring to the console and to be
able to write logs out to files.
This commit is contained in:
2024-02-10 09:50:02 -05:00
parent abbceb3443
commit 16e45e4675
4 changed files with 86 additions and 1 deletions

View File

@@ -0,0 +1,60 @@
#include "logger.h"
#include "../defines.h"
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
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) {
// TODO: Make use of the color we are passing in so that we can color the
// output on the console
printf("%s", 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\n", 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);
}
}

View File

@@ -0,0 +1,17 @@
#pragma once
typedef enum log_level {
FATAL,
ERROR,
WARN,
INFO,
DEBUG,
} log_level;
void log_output(log_level level, const char *message, ...);
#define QFATAL(message, ...) log_output(FATAL, message, ##__VA_ARGS__);
#define QERROR(message, ...) log_output(ERROR, message, ##__VA_ARGS__);
#define QWARN(message, ...) log_output(WARN, message, ##__VA_ARGS__);
#define QINFO(message, ...) log_output(INFO, message, ##__VA_ARGS__);
#define QDEBUG(message, ...) log_output(DEBUG, message, ##__VA_ARGS__);