diff --git a/CMakeLists.txt b/CMakeLists.txt index 53b354e..d00af91 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,8 @@ set(quantum-src "./quantum/src/") set(test_stuff-src "./test_stuff/src/") set(CMAKE_EXPORT_COMPILE_COMMANDS ON) -add_library(quantum SHARED ${quantum-src}memory/zone.c) +add_library(quantum SHARED ${quantum-src}memory/zone.c + ${quantum-src}logger/logger.c) add_executable(test_stuff ${test_stuff-src}main.c) target_link_libraries(test_stuff quantum) diff --git a/quantum/src/logger/logger.c b/quantum/src/logger/logger.c new file mode 100644 index 0000000..7f5080b --- /dev/null +++ b/quantum/src/logger/logger.c @@ -0,0 +1,60 @@ +#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) { + // 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); + } +} diff --git a/quantum/src/logger/logger.h b/quantum/src/logger/logger.h new file mode 100644 index 0000000..b487281 --- /dev/null +++ b/quantum/src/logger/logger.h @@ -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__); diff --git a/test_stuff/src/main.c b/test_stuff/src/main.c index 39054d7..169cb2f 100644 --- a/test_stuff/src/main.c +++ b/test_stuff/src/main.c @@ -1,4 +1,5 @@ #include +#include #include #include @@ -12,6 +13,12 @@ int main() { printf("Size of f32: %lu\n", sizeof(f32)); printf("Size of f64: %lu\n", sizeof(f64)); + QFATAL("Test"); + QERROR("Test"); + QWARN("Test"); + QINFO("Test"); + QDEBUG("Test"); + printf("\nCreating new zone 4K in size...\n"); Zone *test_zone = zoneCreate(4096);