cbase 1.50.0
C/C++ Static Template
Loading...
Searching...
No Matches
base_log.h File Reference

Professional, thread-safe, leveled logging system. More...

#include "base_macros.h"
#include "base_types.h"
#include <stdarg.h>
#include <stdio.h>
Include dependency graph for base_log.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  BaseLogEvent
 A structured event containing all metadata for a single log message. More...

Macros

#define LOG_TRACE(...)
 Logs a TRACE level message (Fine-grained step-by-step debug info).
#define LOG_DEBUG(...)
 Logs a DEBUG level message (Diagnostic info).
#define LOG_INFO(...)
 Logs an INFO level message (General application flow).
#define LOG_WARN(...)
 Logs a WARN level message (Abnormal but non-fatal).
#define LOG_ERROR(...)
 Logs an ERROR level message (Recoverable operation failures).
#define LOG_FATAL(...)
 Logs a FATAL level message (Critical crash or shutdown).

Typedefs

typedef enum BaseLogLevel BaseLogLevel
 The severity level of a log message.
typedef struct BaseLogEvent BaseLogEvent
 A structured event containing all metadata for a single log message.
typedef void(* BaseLogFn) (BaseLogEvent *ev, void *user_data)
 Signature for custom logging destination callbacks.
typedef void(* BaseLogLockFn) (void *user_data, b32 lock)
 Signature for the thread-synchronization callback.

Enumerations

enum  BaseLogLevel {
  BASE_LOG_LEVEL_TRACE , BASE_LOG_LEVEL_DEBUG , BASE_LOG_LEVEL_INFO , BASE_LOG_LEVEL_WARN ,
  BASE_LOG_LEVEL_ERROR , BASE_LOG_LEVEL_FATAL , BASE_LOG_LEVEL_COUNT
}
 The severity level of a log message. More...

Functions

void base_log_set_level (BaseLogLevel level)
 Configures the active logging level. Messages below this level are ignored.
void base_log_set_quiet (b32 enable)
 Mutes or unmutes all console (stdout/stderr) output.
void base_log_set_lock (BaseLogLockFn fn, void *user_data)
 Configures thread-safety by providing a custom locking mechanism.
b32 base_log_add_fp (FILE *fp, BaseLogLevel min_level)
 Adds a standard C FILE* pointer as a logging destination.
b32 base_log_add_callback (BaseLogFn fn, void *user_data, BaseLogLevel min_level)
 Adds a custom callback function as a logging destination.
b32 base_log_remove_callback (BaseLogFn fn)
 Removes a previously registered callback from the logger.
void base_log_message (BaseLogLevel level, const char *file, int line, const char *fmt,...)
 Internal function that actually processes the log. DO NOT call directly.

Detailed Description

Professional, thread-safe, leveled logging system.

Features multiple sinks (console, files, callbacks), thread-safety via custom locks, and zero-overhead debug logging in release builds.

Basic Usage
// No setup required — all levels print to stdout/stderr by default.
LOG_INFO("Server started on port %d", 8080);
LOG_WARN("Config file not found, using defaults");
LOG_ERROR("Failed to open file: %s", path);
#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
#define LOG_WARN(...)
Logs a WARN level message (Abnormal but non-fatal).
Definition base_log.h:246
Adding a File Sink
FILE *log_file = fopen("app.log", "a");
base_log_add_fp(log_file, BASE_LOG_LEVEL_WARN); // WARN and above to file
LOG_WARN("This goes to console AND app.log");
LOG_DEBUG("This only goes to console");
// Remove before closing to prevent a dangling pointer:
base_log_remove_callback(NULL); // see base_log_remove_callback for correct usage
fclose(log_file);
#define LOG_DEBUG(...)
Logs a DEBUG level message (Diagnostic info).
Definition base_log.h:242
b32 base_log_add_fp(FILE *fp, BaseLogLevel min_level)
Adds a standard C FILE* pointer as a logging destination.
Definition base_log.c:172
b32 base_log_remove_callback(BaseLogFn fn)
Removes a previously registered callback from the logger.
Definition base_log.c:192
@ BASE_LOG_LEVEL_WARN
Definition base_log.h:73
Custom Callback Sink
void my_sink(BaseLogEvent *ev, void *user_data) {
MyLogger *logger = (MyLogger *)user_data;
// vsnprintf(ev->fmt, ev->ap) to format the message
}
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
@ BASE_LOG_LEVEL_TRACE
Definition base_log.h:70
A structured event containing all metadata for a single log message.
Definition base_log.h:80
Thread Safety
// Provide a custom lock backed by your threading library:
void my_lock_fn(void *user_data, b32 lock) {
MyMutex *m = (MyMutex *)user_data;
if (lock) mutex_lock(m); else mutex_unlock(m);
}
base_log_set_lock(my_lock_fn, &my_mutex);
static void lock(void)
Acquires the logger lock if a locking function is configured.
Definition base_log.c:92
void base_log_set_lock(BaseLogLockFn fn, void *user_data)
Configures thread-safety by providing a custom locking mechanism.
Definition base_log.c:165
int32_t b32
Definition base_types.h:111

Definition in file base_log.h.

Macro Definition Documentation

◆ LOG_DEBUG

#define LOG_DEBUG ( ...)
Value:
base_log_message(BASE_LOG_LEVEL_DEBUG, __FILE__, __LINE__, __VA_ARGS__)
void base_log_message(BaseLogLevel level, const char *file, int line, const char *fmt,...)
Internal function that actually processes the log. DO NOT call directly.
Definition base_log.c:206
@ BASE_LOG_LEVEL_DEBUG
Definition base_log.h:71

Logs a DEBUG level message (Diagnostic info).

Definition at line 242 of file base_log.h.

◆ LOG_ERROR

#define LOG_ERROR ( ...)
Value:
base_log_message(BASE_LOG_LEVEL_ERROR, __FILE__, __LINE__, __VA_ARGS__)
@ BASE_LOG_LEVEL_ERROR
Definition base_log.h:74

Logs an ERROR level message (Recoverable operation failures).

Definition at line 248 of file base_log.h.

◆ LOG_FATAL

#define LOG_FATAL ( ...)
Value:
base_log_message(BASE_LOG_LEVEL_FATAL, __FILE__, __LINE__, __VA_ARGS__)
@ BASE_LOG_LEVEL_FATAL
Definition base_log.h:75

Logs a FATAL level message (Critical crash or shutdown).

Definition at line 250 of file base_log.h.

◆ LOG_INFO

#define LOG_INFO ( ...)
Value:
base_log_message(BASE_LOG_LEVEL_INFO, __FILE__, __LINE__, __VA_ARGS__)
@ BASE_LOG_LEVEL_INFO
Definition base_log.h:72

Logs an INFO level message (General application flow).

Definition at line 244 of file base_log.h.

◆ LOG_TRACE

#define LOG_TRACE ( ...)
Value:
base_log_message(BASE_LOG_LEVEL_TRACE, __FILE__, __LINE__, __VA_ARGS__)

Logs a TRACE level message (Fine-grained step-by-step debug info).

Definition at line 240 of file base_log.h.

◆ LOG_WARN

#define LOG_WARN ( ...)
Value:
base_log_message(BASE_LOG_LEVEL_WARN, __FILE__, __LINE__, __VA_ARGS__)

Logs a WARN level message (Abnormal but non-fatal).

Definition at line 246 of file base_log.h.