Logging #7
@@ -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)
|
||||
|
||||
60
quantum/src/logger/logger.c
Normal file
60
quantum/src/logger/logger.c
Normal 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);
|
||||
}
|
||||
}
|
||||
17
quantum/src/logger/logger.h
Normal file
17
quantum/src/logger/logger.h
Normal 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__);
|
||||
@@ -1,4 +1,5 @@
|
||||
#include <defines.h>
|
||||
#include <logger/logger.h>
|
||||
#include <memory/zone.h>
|
||||
#include <stdio.h>
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user