cbase 1.46.11
C/C++ Static Template
Loading...
Searching...
No Matches
base_log.c
Go to the documentation of this file.
1
7
8#include "base/base_log.h"
9#include <string.h>
10#include <time.h>
11
13#define MAX_CALLBACKS 4
14
21
30
32static Logger L = {.level = LOG_LEVEL_TRACE};
33
35static const char *level_strings[] = {"TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL"};
36
37// Standard ANSI terminal colors
38#if OS_WINDOWS
40# define COLOR_TRACE ""
42# define COLOR_DEBUG ""
44# define COLOR_INFO ""
46# define COLOR_WARN ""
48# define COLOR_ERROR ""
50# define COLOR_FATAL ""
52# define COLOR_RESET ""
53#else
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"
68#endif
69
73
74// Internal Helpers
75
77static void
78lock(void)
79{
80 if (L.lock_fn) {
81 L.lock_fn(L.lock_data, 1);
82 }
83}
84
86static void
87unlock(void)
88{
89 if (L.lock_fn) {
90 L.lock_fn(L.lock_data, 0);
91 }
92}
93
98static void
100{
101 char buf[16];
102 time_t t = time(NULL);
103 buf[strftime(buf, sizeof(buf), "%H:%M:%S", localtime(&t))] = '\0';
104
105 // Write to stderr for warnings/errors, stdout for standard info
106 FILE *out = (ev->level >= LOG_LEVEL_WARN) ? stderr : stdout;
107
108 fprintf(out,
109 "%s %s%-5s\x1b[0m \x1b[90m%s:%d:\x1b[0m ",
110 buf,
111 level_colors[ev->level],
112 level_strings[ev->level],
113 ev->file,
114 ev->line);
115
116 vfprintf(out, ev->fmt, ev->ap);
117 fprintf(out, "\n");
118 fflush(out);
119}
120
126static void
127file_callback(LogEvent *ev, void *user_data)
128{
129 FILE *fp = (FILE *)user_data;
130 char buf[64];
131 time_t t = time(NULL);
132 buf[strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", localtime(&t))] = '\0';
133
134 fprintf(fp, "%s %-5s %s:%d: ", buf, level_strings[ev->level], ev->file, ev->line);
135 vfprintf(fp, ev->fmt, ev->ap);
136 fprintf(fp, "\n");
137 fflush(fp);
138}
139
140void
142{
143 L.level = level;
144}
145
146void
148{
149 L.quiet = enable;
150}
151
152void
153log_set_lock(LogLockFn fn, void *user_data)
154{
155 L.lock_fn = fn;
156 L.lock_data = user_data;
157}
158
159b32
160log_add_fp(FILE *fp, LogLevel min_level)
161{
162 return log_add_callback(file_callback, fp, min_level);
163}
164
165b32
166log_add_callback(LogFn fn, void *user_data, LogLevel min_level)
167{
168 for (int i = 0; i < MAX_CALLBACKS; i++) {
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;
173 return 1;
174 }
175 }
176 return 0; // No free callback slot
177}
178
179void
180log_message(LogLevel level, const char *file, int line, const char *fmt, ...)
181{
182 if (level < L.level) {
183 return;
184 }
185
186 LogEvent ev = {
187 .fmt = fmt,
188 .file = file,
189 .line = line,
190 .level = level,
191 };
192
193 lock();
194
195 if (!L.quiet) {
196 va_start(ev.ap, fmt);
197 log_stdout(&ev);
198 va_end(ev.ap);
199 }
200
201 for (int i = 0; i < MAX_CALLBACKS; i++) {
202 Callback *cb = &L.callbacks[i];
203 if (cb->fn != NULL && level >= cb->min_level) {
204 va_start(ev.ap, fmt);
205 cb->fn(&ev, cb->user_data);
206 va_end(ev.ap);
207 }
208 }
209
210 unlock();
211}
#define COLOR_INFO
ANSI Green for INFO.
Definition base_log.c:59
#define COLOR_DEBUG
ANSI Cyan for DEBUG.
Definition base_log.c:57
static const char * level_strings[]
Human-readable string representations of the log levels.
Definition base_log.c:35
static void log_stdout(LogEvent *ev)
Formats and writes a log event to standard output or standard error.
Definition base_log.c:99
static const char * level_colors[]
Array mapping log levels to their respective ANSI color codes.
Definition base_log.c:71
static void lock(void)
Acquires the logger lock if a locking function is configured.
Definition base_log.c:78
#define COLOR_WARN
ANSI Yellow for WARN.
Definition base_log.c:61
#define COLOR_TRACE
ANSI Light Blue for TRACE.
Definition base_log.c:55
static Logger L
The single global logger instance state.
Definition base_log.c:32
static void unlock(void)
Releases the logger lock if a locking function is configured.
Definition base_log.c:87
static void file_callback(LogEvent *ev, void *user_data)
Default callback used to write log events to a standard C FILE*.
Definition base_log.c:127
#define COLOR_ERROR
ANSI Red for ERROR.
Definition base_log.c:63
#define COLOR_FATAL
ANSI Magenta for FATAL.
Definition base_log.c:65
#define MAX_CALLBACKS
Maximum number of custom log callbacks supported simultaneously.
Definition base_log.c:13
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.
Definition base_log.c:160
void log_set_level(LogLevel level)
Configures the active logging level. Messages below this level are ignored.
Definition base_log.c:141
void(* LogLockFn)(void *user_data, b32 lock)
Signature for the thread-synchronization callback.
Definition base_log.h:65
void log_set_lock(LogLockFn fn, void *user_data)
Configures thread-safety by providing a custom locking mechanism.
Definition base_log.c:153
void log_message(LogLevel level, const char *file, int line, const char *fmt,...)
Internal function that actually processes the log. DO NOT call directly.
Definition base_log.c:180
LogLevel
The severity level of a log message.
Definition base_log.h:42
b32 log_add_callback(LogFn fn, void *user_data, LogLevel min_level)
Adds a custom callback function as a logging destination.
Definition base_log.c:166
void(* LogFn)(LogEvent *ev, void *user_data)
Signature for custom logging destination callbacks.
Definition base_log.h:62
void log_set_quiet(b32 enable)
Mutes or unmutes all console (stdout/stderr) output.
Definition base_log.c:147
@ LOG_LEVEL_TRACE
Definition base_log.h:43
@ LOG_LEVEL_WARN
Definition base_log.h:46
int32_t b32
Definition base_types.h:75
Internal structure to hold a logging callback and its configuration.
Definition base_log.c:16
void * user_data
Definition base_log.c:18
LogLevel min_level
Definition base_log.c:19
LogFn fn
Definition base_log.c:17
A structured event containing all metadata for a single log message.
Definition base_log.h:53
const char * file
Definition base_log.h:56
LogLevel level
Definition base_log.h:58
const char * fmt
Definition base_log.h:55
va_list ap
Definition base_log.h:54
int line
Definition base_log.h:57
Internal state structure for the global logger.
Definition base_log.c:23
b32 quiet
Definition base_log.c:27
LogLevel level
Definition base_log.c:26
void * lock_data
Definition base_log.c:24
LogLockFn lock_fn
Definition base_log.c:25
Callback callbacks[4]
Definition base_log.c:28