1
0
ain48/src/logging.c
2026-05-31 12:33:05 +02:00

53 lines
1.4 KiB
C

#include <common.h>
#include <stdio.h>
#include <errno.h>
#include <stdarg.h>
#include <string.h>
FILE *logfile;
bool logcolours = true;
[[gnu::constructor(101)]] void init_logging() {
logfile = stderr;
}
#ifndef DEBUG
void log_debug([[maybe_unused]] const char* format, ...) {}
#else
void log_debug(const char* format, ...) {
fprintf(logfile, "%s[DEBUG]%s ", logcolours ? "\033[1;44;97m" : "", logcolours ? "\033[0" : "");
va_list va;
va_start(va);
vfprintf(logfile, format, va);
va_end(va);
fprintf(logfile, "\n");
}
#endif
void log_warn(const char* format, ...) {
fprintf(logfile, "%s[WARN]%s ", logcolours ? "\033[1;43;97m": "", logcolours ? "\033[0m" : "");
va_list va;
va_start(va);
vfprintf(logfile, format, va);
va_end(va);
fprintf(logfile, "\n");
}
void log_err(const char* format, ...) {
fprintf(logfile, "%s[ERROR]%s ", logcolours ? "\033[1;41;97m": "", logcolours ? "\033[0m" : "");
va_list va;
va_start(va);
vfprintf(logfile, format, va);
va_end(va);
fprintf(logfile, "\n");
}
void log_err_errno(const char* format, ...) {
fprintf(logfile, "%s[ERROR]%s ", logcolours ? "\033[1;41;97m": "", logcolours ? "\033[0m" : "");
va_list va;
va_start(va);
vfprintf(logfile, format, va);
va_end(va);
fprintf(logfile, ": %s\n", strerror(errno));
}
void set_log_opts(FILE *f, bool colours) {
logfile = f;
logcolours = colours;
}