|
#include "logging.h"
|
|
#include <stdio.h>
|
|
#include <stdarg.h>
|
|
#include <time.h>
|
|
|
|
static int g_debug_mode = 0;
|
|
|
|
void logging_set_debug(int enabled) {
|
|
g_debug_mode = enabled;
|
|
}
|
|
|
|
int logging_get_debug(void) {
|
|
return g_debug_mode;
|
|
}
|
|
|
|
void log_error(const char *msg) {
|
|
perror(msg);
|
|
}
|
|
|
|
static void log_message(const char *level, const char *format, va_list args) {
|
|
time_t now;
|
|
time(&now);
|
|
char buf[32];
|
|
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", localtime(&now));
|
|
printf("%s - %-5s - ", buf, level);
|
|
vprintf(format, args);
|
|
printf("\n");
|
|
fflush(stdout);
|
|
}
|
|
|
|
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);
|
|
}
|