cbase 1.50.0
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  BaseLogCallback
 Internal structure to hold a logging callback and its configuration. More...
struct  BaseLogger
 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"
#define COLOR_DEBUG   "\x1b[36m"
#define COLOR_INFO   "\x1b[32m"
#define COLOR_WARN   "\x1b[33m"
#define COLOR_ERROR   "\x1b[31m"
#define COLOR_FATAL   "\x1b[35m"
#define COLOR_RESET   "\x1b[0m"

Typedefs

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

Functions

static const char * color_for_stream (b32 is_stderr, const char *color)
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 (BaseLogEvent *ev)
 Formats and writes a log event to standard output or standard error.
static void file_callback (BaseLogEvent *ev, void *user_data)
 Default callback used to write log events to a standard C FILE*.
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.

Variables

static BaseLogger L = {.level = BASE_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"

Definition at line 39 of file base_log.c.

◆ COLOR_ERROR

#define COLOR_ERROR   "\x1b[31m"

Definition at line 42 of file base_log.c.

◆ COLOR_FATAL

#define COLOR_FATAL   "\x1b[35m"

Definition at line 43 of file base_log.c.

◆ COLOR_INFO

#define COLOR_INFO   "\x1b[32m"

Definition at line 40 of file base_log.c.

◆ COLOR_RESET

#define COLOR_RESET   "\x1b[0m"

Definition at line 44 of file base_log.c.

◆ COLOR_TRACE

#define COLOR_TRACE   "\x1b[94m"

Definition at line 38 of file base_log.c.

◆ COLOR_WARN

#define COLOR_WARN   "\x1b[33m"

Definition at line 41 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

◆ color_for_stream()

const char * color_for_stream ( b32 is_stderr,
const char * color )
static

Definition at line 77 of file base_log.c.

◆ file_callback()

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

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

Definition at line 139 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 92 of file base_log.c.

Here is the caller graph for this function:

◆ log_stdout()

void log_stdout ( BaseLogEvent * ev)
static

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

Definition at line 112 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 101 of file base_log.c.

Here is the caller graph for this function:

Variable Documentation

◆ L

BaseLogger L = {.level = BASE_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 85 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.