#include "logging.h" #include #include #include #include #include #include static int g_debug_mode = 0; static FILE *g_log_file = NULL; static pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER; void logging_set_debug(int enabled) { g_debug_mode = enabled; } int logging_get_debug(void) { return g_debug_mode; } int logging_set_file(const char *path) { pthread_mutex_lock(&log_mutex); if (g_log_file && g_log_file != stdout && g_log_file != stderr) { fclose(g_log_file); } if (path) { g_log_file = fopen(path, "a"); if (!g_log_file) { g_log_file = stdout; pthread_mutex_unlock(&log_mutex); return -1; } } else { g_log_file = stdout; } pthread_mutex_unlock(&log_mutex); return 0; } void logging_cleanup(void) { pthread_mutex_lock(&log_mutex); if (g_log_file && g_log_file != stdout && g_log_file != stderr) { fclose(g_log_file); } g_log_file = NULL; pthread_mutex_unlock(&log_mutex); } static void log_message(const char *level, const char *format, va_list args) { pthread_mutex_lock(&log_mutex); FILE *out = g_log_file ? g_log_file : stdout; time_t now; time(&now); struct tm *local = localtime(&now); char buf[32]; strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", local); fprintf(out, "%s - %-5s - ", buf, level); vfprintf(out, format, args); fprintf(out, "\n"); fflush(out); pthread_mutex_unlock(&log_mutex); } void log_error(const char *format, ...) { va_list args; va_start(args, format); int saved_errno = errno; char msg[1024]; vsnprintf(msg, sizeof(msg), format, args); va_end(args); pthread_mutex_lock(&log_mutex); FILE *out = g_log_file ? g_log_file : stderr; time_t now; time(&now); struct tm *local = localtime(&now); char buf[32]; strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", local); if (saved_errno != 0) { fprintf(out, "%s - ERROR - %s: %s\n", buf, msg, strerror(saved_errno)); } else { fprintf(out, "%s - ERROR - %s\n", buf, msg); } fflush(out); pthread_mutex_unlock(&log_mutex); } void log_info(const char *format, ...) { va_list args; va_start(args, format); log_message("INFO", format, args); va_end(args); } void log_debug(const char *format, ...) { if (!g_debug_mode) return; va_list args; va_start(args, format); log_message("DEBUG", format, args); va_end(args); }