cbase 1.46.11
C/C++ Static Template
Loading...
Searching...
No Matches
base_log.c File Reference

Implementation of the leveled logging subsystem. More...

#include "base/base_log.h"
#include <string.h>
#include <time.h>
Include dependency graph for base_log.c:

Go to the source code of this file.

Classes

struct  Callback
 Internal structure to hold a logging callback and its configuration. More...
struct  Logger
 Internal state structure for the global logger. More...

Macros

#define MAX_CALLBACKS   4
 Maximum number of custom log callbacks supported simultaneously.
#define COLOR_TRACE   "\x1b[94m"
 ANSI Light Blue for TRACE.
#define COLOR_DEBUG   "\x1b[36m"
 ANSI Cyan for DEBUG.
#define COLOR_INFO   "\x1b[32m"
 ANSI Green for INFO.
#define COLOR_WARN   "\x1b[33m"
 ANSI Yellow for WARN.
#define COLOR_ERROR   "\x1b[31m"
 ANSI Red for ERROR.
#define COLOR_FATAL   "\x1b[35m"
 ANSI Magenta for FATAL.
#define COLOR_RESET   "\x1b[0m"
 ANSI color reset sequence.

Typedefs

typedef struct Callback Callback
 Internal structure to hold a logging callback and its configuration.
typedef struct Logger Logger
 Internal state structure for the global logger.

Functions

static void lock (void)
 Acquires the logger lock if a locking function is configured.
static void unlock (void)
 Releases the logger lock if a locking function is configured.
static void log_stdout (LogEvent *ev)
 Formats and writes a log event to standard output or standard error.
static void file_callback (LogEvent *ev, void *user_data)
 Default callback used to write log events to a standard C FILE*.
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.

Variables

static Logger L = {.level = LOG_LEVEL_TRACE}
 The single global logger instance state.
static const char * level_strings [] = {"TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL"}
 Human-readable string representations of the log levels.
static const char * level_colors []
 Array mapping log levels to their respective ANSI color codes.

Detailed Description

Implementation of the leveled logging subsystem.

Definition in file base_log.c.

Macro Definition Documentation

◆ COLOR_DEBUG

#define COLOR_DEBUG   "\x1b[36m"

ANSI Cyan for DEBUG.

Definition at line 57 of file base_log.c.

◆ COLOR_ERROR

#define COLOR_ERROR   "\x1b[31m"

ANSI Red for ERROR.

Definition at line 63 of file base_log.c.

◆ COLOR_FATAL

#define COLOR_FATAL   "\x1b[35m"

ANSI Magenta for FATAL.

Definition at line 65 of file base_log.c.

◆ COLOR_INFO

#define COLOR_INFO   "\x1b[32m"

ANSI Green for INFO.

Definition at line 59 of file base_log.c.

◆ COLOR_RESET

#define COLOR_RESET   "\x1b[0m"

ANSI color reset sequence.

Definition at line 67 of file base_log.c.

◆ COLOR_TRACE

#define COLOR_TRACE   "\x1b[94m"

ANSI Light Blue for TRACE.

Definition at line 55 of file base_log.c.

◆ COLOR_WARN

#define COLOR_WARN   "\x1b[33m"

ANSI Yellow for WARN.

Definition at line 61 of file base_log.c.

◆ MAX_CALLBACKS

#define MAX_CALLBACKS   4

Maximum number of custom log callbacks supported simultaneously.

Definition at line 13 of file base_log.c.

Function Documentation

◆ file_callback()

void file_callback ( LogEvent * ev,
void * user_data )
static

Default callback used to write log events to a standard C FILE*.

Parameters
evThe log event to process.
user_dataAn active FILE* pointer cast to void*.

Definition at line 127 of file base_log.c.

Here is the caller graph for this function:

◆ lock()

void lock ( void )
static

Acquires the logger lock if a locking function is configured.

Definition at line 78 of file base_log.c.

Here is the caller graph for this function:

◆ log_stdout()

void log_stdout ( LogEvent * ev)
static

Formats and writes a log event to standard output or standard error.

Parameters
evThe log event to process.

Definition at line 99 of file base_log.c.

Here is the caller graph for this function:

◆ unlock()

void unlock ( void )
static

Releases the logger lock if a locking function is configured.

Definition at line 87 of file base_log.c.

Here is the caller graph for this function:

Variable Documentation

◆ L

Logger L = {.level = LOG_LEVEL_TRACE}
static

The single global logger instance state.

Definition at line 32 of file base_log.c.

◆ level_colors

const char* level_colors[]
static
Initial value:
=
{ "\x1b[94m" , "\x1b[36m" , "\x1b[32m" , "\x1b[33m" , "\x1b[31m" , "\x1b[35m" }

Array mapping log levels to their respective ANSI color codes.

Definition at line 71 of file base_log.c.

◆ level_strings

const char* level_strings[] = {"TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL"}
static

Human-readable string representations of the log levels.

Definition at line 35 of file base_log.c.