13#define MAX_CALLBACKS 4
35static const char *
level_strings[] = {
"TRACE",
"DEBUG",
"INFO",
"WARN",
"ERROR",
"FATAL"};
40# define COLOR_TRACE ""
42# define COLOR_DEBUG ""
48# define COLOR_ERROR ""
50# define COLOR_FATAL ""
52# define COLOR_RESET ""
55# define COLOR_TRACE "\x1b[94m"
57# define COLOR_DEBUG "\x1b[36m"
59# define COLOR_INFO "\x1b[32m"
61# define COLOR_WARN "\x1b[33m"
63# define COLOR_ERROR "\x1b[31m"
65# define COLOR_FATAL "\x1b[35m"
67# define COLOR_RESET "\x1b[0m"
81 L.lock_fn(
L.lock_data, 1);
90 L.lock_fn(
L.lock_data, 0);
102 time_t t = time(NULL);
103 buf[strftime(buf,
sizeof(buf),
"%H:%M:%S", localtime(&t))] =
'\0';
109 "%s %s%-5s\x1b[0m \x1b[90m%s:%d:\x1b[0m ",
116 vfprintf(out, ev->
fmt, ev->
ap);
129 FILE *fp = (FILE *)user_data;
131 time_t t = time(NULL);
132 buf[strftime(buf,
sizeof(buf),
"%Y-%m-%d %H:%M:%S", localtime(&t))] =
'\0';
135 vfprintf(fp, ev->
fmt, ev->
ap);
156 L.lock_data = user_data;
169 if (
L.callbacks[i].fn == NULL) {
170 L.callbacks[i].fn = fn;
171 L.callbacks[i].user_data = user_data;
172 L.callbacks[i].min_level = min_level;
182 if (level <
L.level) {
196 va_start(ev.
ap, fmt);
204 va_start(ev.
ap, fmt);
#define COLOR_INFO
ANSI Green for INFO.
#define COLOR_DEBUG
ANSI Cyan for DEBUG.
static const char * level_strings[]
Human-readable string representations of the log levels.
static void log_stdout(LogEvent *ev)
Formats and writes a log event to standard output or standard error.
static const char * level_colors[]
Array mapping log levels to their respective ANSI color codes.
static void lock(void)
Acquires the logger lock if a locking function is configured.
#define COLOR_WARN
ANSI Yellow for WARN.
#define COLOR_TRACE
ANSI Light Blue for TRACE.
static Logger L
The single global logger instance state.
static void unlock(void)
Releases the logger lock if a locking function is configured.
static void file_callback(LogEvent *ev, void *user_data)
Default callback used to write log events to a standard C FILE*.
#define COLOR_ERROR
ANSI Red for ERROR.
#define COLOR_FATAL
ANSI Magenta for FATAL.
#define MAX_CALLBACKS
Maximum number of custom log callbacks supported simultaneously.
Professional, thread-safe, leveled logging system.
b32 log_add_fp(FILE *fp, LogLevel min_level)
Adds a standard C FILE* pointer as a logging destination.
void log_set_level(LogLevel level)
Configures the active logging level. Messages below this level are ignored.
void(* LogLockFn)(void *user_data, b32 lock)
Signature for the thread-synchronization callback.
void log_set_lock(LogLockFn fn, void *user_data)
Configures thread-safety by providing a custom locking mechanism.
void log_message(LogLevel level, const char *file, int line, const char *fmt,...)
Internal function that actually processes the log. DO NOT call directly.
LogLevel
The severity level of a log message.
b32 log_add_callback(LogFn fn, void *user_data, LogLevel min_level)
Adds a custom callback function as a logging destination.
void(* LogFn)(LogEvent *ev, void *user_data)
Signature for custom logging destination callbacks.
void log_set_quiet(b32 enable)
Mutes or unmutes all console (stdout/stderr) output.
Internal structure to hold a logging callback and its configuration.
A structured event containing all metadata for a single log message.
Internal state structure for the global logger.