Update logging to also print to stdout + replace printf to logging.

This commit is contained in:
2024-10-15 01:36:02 +08:00
parent 988bc52e57
commit f9cf93eaee
6 changed files with 53 additions and 38 deletions
+18 -3
View File
@@ -1,5 +1,8 @@
// Chat GPT code
#include "log.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
const char* get_current_time()
{
@@ -10,7 +13,7 @@ const char* get_current_time()
return buffer;
}
void log_message(LogLevel level, const char* message)
void log_message(LogLevel level, const char* format, ...)
{
const char* level_strings[] = { "INFO", "WARNING", "ERROR" };
@@ -21,8 +24,20 @@ void log_message(LogLevel level, const char* message)
return;
}
va_list args;
// Write the log message to the file
fprintf(log_file, "[%s] [%s] %s\n", get_current_time(), level_strings[level], message);
va_start(args, format);
fprintf(log_file, "[%s] [%s] ", get_current_time(), level_strings[level]);
vfprintf(log_file, format, args);
fprintf(log_file, "\n");
va_end(args);
// Write to stdout
va_start(args, format);
printf("[%s] ", level_strings[level]);
vprintf(format, args);
printf("\n");
va_end(args);
// Close the log file
fclose(log_file);