cbase 1.46.11
C/C++ Static Template
Loading...
Searching...
No Matches
test_log.c
1#include "base/base_log.h"
2#include "test_harness.h"
3#include <string.h>
4
5// State variables to track what the logger is doing internally
6static int lock_calls = 0;
7static int unlock_calls = 0;
8static int callback_hits = 0;
9static LogLevel last_level;
10static char last_msg[256];
11
12// Dummy lock function to verify thread-safety triggers
13static void
14test_lock_fn(void *user_data, b32 lock)
15{
16 if (lock) {
17 lock_calls++;
18 } else {
19 unlock_calls++;
20 }
21}
22
23// Dummy callback to intercept the log events
24static void
25test_log_callback(LogEvent *ev, void *user_data)
26{
27 callback_hits++;
28 last_level = ev->level;
29
30 // Safely format the variadic arguments into our test buffer
31 int r = vsnprintf(last_msg, sizeof(last_msg), ev->fmt, ev->ap);
32}
33
34TEST_CASE(logging_system)
35{
36 // Setup the logger for testing
37 log_set_quiet(1); // Mute standard console output so we don't spam the test runner UI
38 log_set_lock(test_lock_fn, NULL);
39 log_add_callback(test_log_callback, NULL, LOG_LEVEL_TRACE);
40
41 // Test Basic Logging & Callbacks
42 LOG_INFO("Hello %s", "World");
43 EXPECT(callback_hits == 1);
44 EXPECT(last_level == LOG_LEVEL_INFO);
45 EXPECT(strcmp(last_msg, "Hello World") == 0);
46
47 // Ensure the thread locks were acquired and released exactly once
48 EXPECT(lock_calls == 1);
49 EXPECT(unlock_calls == 1);
50
51 // Test Log Level Filtering
52 log_set_level(LOG_LEVEL_WARN); // Should now ignore TRACE, DEBUG, and INFO
53
54 LOG_INFO("This should be ignored entirely");
55 EXPECT(callback_hits == 1); // Hit count should NOT increase
56 EXPECT(lock_calls == 1); // Fast-path: locks shouldn't even be called if level is too low!
57
58 LOG_ERROR("This should pass %d", 42);
59 EXPECT(callback_hits == 2);
60 EXPECT(last_level == LOG_LEVEL_ERROR);
61 EXPECT(strcmp(last_msg, "This should pass 42") == 0);
62 EXPECT(lock_calls == 2);
63 EXPECT(unlock_calls == 2);
64
65 // Cleanup (Reset logger state so it doesn't affect future tests)
68 log_set_lock(NULL, NULL);
69}
static void lock(void)
Acquires the logger lock if a locking function is configured.
Definition base_log.c:78
Professional, thread-safe, leveled logging system.
#define LOG_INFO(...)
Logs an INFO level message (General application flow).
Definition base_log.h:122
#define LOG_ERROR(...)
Logs an ERROR level message (Recoverable operation failures).
Definition base_log.h:126
void log_set_level(LogLevel level)
Configures the active logging level. Messages below this level are ignored.
Definition base_log.c:141
void log_set_lock(LogLockFn fn, void *user_data)
Configures thread-safety by providing a custom locking mechanism.
Definition base_log.c:153
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 log_set_quiet(b32 enable)
Mutes or unmutes all console (stdout/stderr) output.
Definition base_log.c:147
@ LOG_LEVEL_ERROR
Definition base_log.h:47
@ LOG_LEVEL_TRACE
Definition base_log.h:43
@ LOG_LEVEL_WARN
Definition base_log.h:46
@ LOG_LEVEL_INFO
Definition base_log.h:45
int32_t b32
Definition base_types.h:75
A structured event containing all metadata for a single log message.
Definition base_log.h:53
LogLevel level
Definition base_log.h:58
const char * fmt
Definition base_log.h:55
va_list ap
Definition base_log.h:54
A lightweight, dependency-free unit testing harness.
#define EXPECT(cond)
Evaluates a condition. If it fails, prints the file/line and increments the fail count.
#define TEST_CASE(name)
Defines a test case function.