cbase 1.50.0
C/C++ Static Template
Loading...
Searching...
No Matches
test_log.c
1#include "base/base_log.h"
2#include "test/test.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 BaseLogLevel 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(BaseLogEvent *ev, void *user_data)
26{
27 callback_hits++;
28 last_level = ev->level;
29
30 // Format into our test buffer and assert it wasn't truncated
31 int r = vsnprintf(last_msg, sizeof(last_msg), ev->fmt, ev->ap);
32 EXPECT(r > 0 && r < (int)sizeof(last_msg));
33}
34
35TEST_CASE(logging_system)
36{
37 // Setup the logger for testing
38 base_log_set_quiet(1); // Mute standard console output so we don't spam the test runner UI
39 base_log_set_lock(test_lock_fn, NULL);
40 base_log_add_callback(test_log_callback, NULL, BASE_LOG_LEVEL_TRACE);
41
42 // Test Basic Logging & Callbacks
43 LOG_INFO("Hello %s", "World");
44 EXPECT(callback_hits == 1);
45 EXPECT(last_level == BASE_LOG_LEVEL_INFO);
46 EXPECT(strcmp(last_msg, "Hello World") == 0);
47
48 // Ensure the thread locks were acquired and released exactly once
49 EXPECT(lock_calls == 1);
50 EXPECT(unlock_calls == 1);
51
52 // Test Log Level Filtering
53 base_log_set_level(BASE_LOG_LEVEL_WARN); // Should now ignore TRACE, DEBUG, and INFO
54
55 LOG_INFO("This should be ignored entirely");
56 EXPECT(callback_hits == 1); // Hit count should NOT increase
57 EXPECT(lock_calls == 1); // Fast-path: locks shouldn't even be called if level is too low!
58
59 LOG_ERROR("This should pass %d", 42);
60 EXPECT(callback_hits == 2);
61 EXPECT(last_level == BASE_LOG_LEVEL_ERROR);
62 EXPECT(strcmp(last_msg, "This should pass 42") == 0);
63 EXPECT(lock_calls == 2);
64 EXPECT(unlock_calls == 2);
65
66 // Cleanup (Reset logger state so it doesn't affect future tests)
69 base_log_set_lock(NULL, NULL);
70}
static void lock(void)
Acquires the logger lock if a locking function is configured.
Definition base_log.c:92
Professional, thread-safe, leveled logging system.
#define LOG_INFO(...)
Logs an INFO level message (General application flow).
Definition base_log.h:244
#define LOG_ERROR(...)
Logs an ERROR level message (Recoverable operation failures).
Definition base_log.h:248
void base_log_set_quiet(b32 enable)
Mutes or unmutes all console (stdout/stderr) output.
Definition base_log.c:159
void base_log_set_lock(BaseLogLockFn fn, void *user_data)
Configures thread-safety by providing a custom locking mechanism.
Definition base_log.c:165
void base_log_set_level(BaseLogLevel level)
Configures the active logging level. Messages below this level are ignored.
Definition base_log.c:153
b32 base_log_add_callback(BaseLogFn fn, void *user_data, BaseLogLevel min_level)
Adds a custom callback function as a logging destination.
Definition base_log.c:178
BaseLogLevel
The severity level of a log message.
Definition base_log.h:69
@ BASE_LOG_LEVEL_ERROR
Definition base_log.h:74
@ BASE_LOG_LEVEL_INFO
Definition base_log.h:72
@ BASE_LOG_LEVEL_WARN
Definition base_log.h:73
@ BASE_LOG_LEVEL_TRACE
Definition base_log.h:70
int32_t b32
Definition base_types.h:111
A structured event containing all metadata for a single log message.
Definition base_log.h:80
const char * fmt
Definition base_log.h:82
va_list ap
Definition base_log.h:81
BaseLogLevel level
Definition base_log.h:85
Single-header testing framework with pretty-printed output.
#define EXPECT(cond)
Evaluates a condition. Records and counts the failure if false.
Definition test.h:161
#define TEST_CASE(name)
Declares a test case function.
Definition test.h:205