cbase 1.46.11
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  LogEvent
 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 LogLevel LogLevel
 The severity level of a log message.
typedef struct LogEvent LogEvent
 A structured event containing all metadata for a single log message.
typedef void(* LogFn) (LogEvent *ev, void *user_data)
 Signature for custom logging destination callbacks.
typedef void(* LogLockFn) (void *user_data, b32 lock)
 Signature for the thread-synchronization callback.

Enumerations

enum  LogLevel {
  LOG_LEVEL_TRACE , LOG_LEVEL_DEBUG , LOG_LEVEL_INFO , LOG_LEVEL_WARN ,
  LOG_LEVEL_ERROR , LOG_LEVEL_FATAL , LOG_LEVEL_COUNT
}
 The severity level of a log message. More...

Functions

void log_set_level (LogLevel level)
 Configures the active logging level. Messages below this level are ignored.
void log_set_quiet (b32 enable)
 Mutes or unmutes all console (stdout/stderr) output.
void log_set_lock (LogLockFn fn, void *user_data)
 Configures thread-safety by providing a custom locking mechanism.
b32 log_add_fp (FILE *fp, LogLevel min_level)
 Adds a standard C FILE* pointer as a logging destination.
b32 log_add_callback (LogFn fn, void *user_data, LogLevel min_level)
 Adds a custom callback function as a logging destination.
void log_message (LogLevel 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
// Default: all levels print to stdout/stderr, no setup required.
LOG_INFO("Server started on port %d", 8080);
LOG_WARN("Config file not found, using defaults");
LOG_ERROR("Failed to open file: %s", path);
// Add a persistent file sink:
FILE *log_file = fopen("app.log", "a");
log_add_fp(log_file, LOG_LEVEL_WARN); // only WARN and above go to file
// Suppress console output (e.g., during tests):
#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
#define LOG_WARN(...)
Logs a WARN level message (Abnormal but non-fatal).
Definition base_log.h:124
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_quiet(b32 enable)
Mutes or unmutes all console (stdout/stderr) output.
Definition base_log.c:147
@ LOG_LEVEL_WARN
Definition base_log.h:46

Definition in file base_log.h.

Macro Definition Documentation

◆ LOG_DEBUG

#define LOG_DEBUG ( ...)
Value:
log_message(LOG_LEVEL_DEBUG, __FILE__, __LINE__, __VA_ARGS__)
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
@ LOG_LEVEL_DEBUG
Definition base_log.h:44

Logs a DEBUG level message (Diagnostic info).

Definition at line 120 of file base_log.h.

◆ LOG_ERROR

#define LOG_ERROR ( ...)
Value:
log_message(LOG_LEVEL_ERROR, __FILE__, __LINE__, __VA_ARGS__)
@ LOG_LEVEL_ERROR
Definition base_log.h:47

Logs an ERROR level message (Recoverable operation failures).

Definition at line 126 of file base_log.h.

◆ LOG_FATAL

#define LOG_FATAL ( ...)
Value:
log_message(LOG_LEVEL_FATAL, __FILE__, __LINE__, __VA_ARGS__)
@ LOG_LEVEL_FATAL
Definition base_log.h:48

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

Definition at line 128 of file base_log.h.

◆ LOG_INFO

#define LOG_INFO ( ...)
Value:
log_message(LOG_LEVEL_INFO, __FILE__, __LINE__, __VA_ARGS__)
@ LOG_LEVEL_INFO
Definition base_log.h:45

Logs an INFO level message (General application flow).

Definition at line 122 of file base_log.h.

◆ LOG_TRACE

#define LOG_TRACE ( ...)
Value:
log_message(LOG_LEVEL_TRACE, __FILE__, __LINE__, __VA_ARGS__)
@ LOG_LEVEL_TRACE
Definition base_log.h:43

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

Definition at line 118 of file base_log.h.

◆ LOG_WARN

#define LOG_WARN ( ...)
Value:
log_message(LOG_LEVEL_WARN, __FILE__, __LINE__, __VA_ARGS__)

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

Definition at line 124 of file base_log.h.