fix: log is now threadsafe

This commit is contained in:
Asger Gitz-Johansen 2024-08-03 12:58:33 +02:00
parent 121643be45
commit e35dbdb1f3

View File

@ -2,7 +2,9 @@
#include <stdbool.h> #include <stdbool.h>
#include <assert.h> #include <assert.h>
#include <time.h> #include <time.h>
#include <pthread.h>
pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER;
log_settings g_log_settings; log_settings g_log_settings;
bool g_log_initialized = false; bool g_log_initialized = false;
@ -64,6 +66,7 @@ void log_log(const char* file, int line, int level, const char* fmt, ...) {
const char* level_color = get_level_color(level); const char* level_color = get_level_color(level);
const char* level_name = get_level_name(level); const char* level_name = get_level_name(level);
pthread_mutex_lock(&log_mutex);
if(g_log_settings.use_colors) if(g_log_settings.use_colors)
fprintf(g_log_settings.out_file, "%s %s%-5s"COLOR_RESET" "COLOR_FILE"%s:%d:"COLOR_RESET" ", timestamp, fprintf(g_log_settings.out_file, "%s %s%-5s"COLOR_RESET" "COLOR_FILE"%s:%d:"COLOR_RESET" ", timestamp,
level_color, level_name, file, line); level_color, level_name, file, line);
@ -75,4 +78,5 @@ void log_log(const char* file, int line, int level, const char* fmt, ...) {
va_end(args); va_end(args);
fprintf(g_log_settings.out_file, "\n"); fprintf(g_log_settings.out_file, "\n");
fflush(g_log_settings.out_file); fflush(g_log_settings.out_file);
pthread_mutex_unlock(&log_mutex);
} }