libmemalloc  v3.0.00
Modern Memory Allocator
Loading...
Searching...
No Matches
Logs

Files

file  logs.h
 Logging macros and configuration for libmemalloc.
 

Macros

#define LOG_LEVEL   LOG_LEVEL_DEBUG
 Default log level if not defined.
 
#define _POSIX_C_SOURCE   200809UL
 Expose POSIX extensions for clock_gettime and pthread mutex initializer.
 
#define LOG_PRINTF(...)   fprintf(__VA_ARGS__)
 Compiler-specific printf abstraction (variadic).
 
#define LOG_VPRINTF(...)   vfprintf(__VA_ARGS__)
 Compiler-specific vprintf abstraction.
 
#define PREFIX_ERROR   "[ERROR]"
 
#define PREFIX_WARNING   "[WARNING]"
 
#define PREFIX_INFO   "[INFO]"
 
#define PREFIX_DEBUG   "[DEBUG]"
 
#define COLOR_RED   "\033[0;31m"
 
#define COLOR_YELLOW   "\033[0;33m"
 
#define COLOR_BLUE   "\033[0;34m"
 
#define COLOR_GREEN   "\033[0;32m"
 
#define COLOR_RESET   "\033[0m"
 
#define ATTR_PRINTF(fmt_idx, var_idx)
 Macro to apply printf-style format checking on custom functions when supported by the compiler.
 
#define LOG_ERROR(...)
 Logs an error message (red).
 
#define LOG_WARNING(...)
 Logs a warning message (yellow).
 
#define LOG_INFO(...)
 Logs an info message (blue).
 
#define LOG_DEBUG(...)
 Logs a debug message (green).
 

Enumerations

enum  log_level_t {
  LOG_LEVEL_NONE = (uint8_t)(0u) , LOG_LEVEL_ERROR = (uint8_t)(1u) , LOG_LEVEL_WARNING = (uint8_t)(2u) , LOG_LEVEL_INFO = (uint8_t)(3u) ,
  LOG_LEVEL_DEBUG = (uint8_t)(4u)
}
 Defines log levels for the logging system. More...
 

Functions

static int LOG_output (log_level_t level, const char *color, const char *prefix, const char *file, const char *func, int line, const char *fmt,...)
 Internal logging implementation: thread‐safe, prints timestamp, optional ANSI color, severity prefix, formatted message, and source location.
 

Detailed Description

Macro Definition Documentation

◆ _POSIX_C_SOURCE

#define _POSIX_C_SOURCE   200809UL

Expose POSIX extensions for clock_gettime and pthread mutex initializer.

Note
This macro should be defined before including system headers in translation units that require POSIX extensions.

◆ ATTR_PRINTF

#define ATTR_PRINTF (   fmt_idx,
  var_idx 
)

Macro to apply printf-style format checking on custom functions when supported by the compiler.

Expands to GCC/Clang's attribute((format(printf, ...))) when available, otherwise to nothing.

◆ COLOR_BLUE

#define COLOR_BLUE   "\033[0;34m"

◆ COLOR_GREEN

#define COLOR_GREEN   "\033[0;32m"

◆ COLOR_RED

#define COLOR_RED   "\033[0;31m"

◆ COLOR_RESET

#define COLOR_RESET   "\033[0m"

◆ COLOR_YELLOW

#define COLOR_YELLOW   "\033[0;33m"

◆ LOG_DEBUG

#define LOG_DEBUG (   ...)
Value:
__FILE__, \
__func__, \
__LINE__, \
__VA_ARGS__)
#define PREFIX_DEBUG
Definition logs.h:147
static int LOG_output(log_level_t level, const char *color, const char *prefix, const char *file, const char *func, int line, const char *fmt,...)
Internal logging implementation: thread‐safe, prints timestamp, optional ANSI color,...
Definition logs.h:260
#define COLOR_GREEN
Definition logs.h:157
@ LOG_LEVEL_DEBUG
Definition logs.h:69

Logs a debug message (green).

◆ LOG_ERROR

#define LOG_ERROR (   ...)
Value:
__FILE__, \
__func__, \
__LINE__, \
__VA_ARGS__)
#define PREFIX_ERROR
Definition logs.h:144
#define COLOR_RED
Definition logs.h:154
@ LOG_LEVEL_ERROR
Definition logs.h:66

Logs an error message (red).

Usage: LOG_ERROR("Message\n"); LOG_ERROR("Message: %d\n", code);

◆ LOG_INFO

#define LOG_INFO (   ...)
Value:
__FILE__, \
__func__, \
__LINE__, \
__VA_ARGS__)
#define PREFIX_INFO
Definition logs.h:146
#define COLOR_BLUE
Definition logs.h:156
@ LOG_LEVEL_INFO
Definition logs.h:68

Logs an info message (blue).

◆ LOG_LEVEL

#define LOG_LEVEL   LOG_LEVEL_DEBUG

Default log level if not defined.

◆ LOG_PRINTF

#define LOG_PRINTF (   ...)    fprintf(__VA_ARGS__)

Compiler-specific printf abstraction (variadic).

Uses __builtin_fprintf when available; falls back to fprintf otherwise. Intended usage matches fprintf:

LOG_PRINTF(stream, "fmt %d\n", value);

◆ LOG_VPRINTF

#define LOG_VPRINTF (   ...)    vfprintf(__VA_ARGS__)

Compiler-specific vprintf abstraction.

Uses __builtin_vfprintf when available; falls back to vfprintf otherwise. Intended usage matches vfprintf:

LOG_VPRINTF(stream, "fmt %d\n", args);

◆ LOG_WARNING

#define LOG_WARNING (   ...)
Value:
__FILE__, \
__func__, \
__LINE__, \
__VA_ARGS__)
#define COLOR_YELLOW
Definition logs.h:155
#define PREFIX_WARNING
Definition logs.h:145
@ LOG_LEVEL_WARNING
Definition logs.h:67

Logs a warning message (yellow).

◆ PREFIX_DEBUG

#define PREFIX_DEBUG   "[DEBUG]"

◆ PREFIX_ERROR

#define PREFIX_ERROR   "[ERROR]"

◆ PREFIX_INFO

#define PREFIX_INFO   "[INFO]"

◆ PREFIX_WARNING

#define PREFIX_WARNING   "[WARNING]"

Enumeration Type Documentation

◆ log_level_t

Defines log levels for the logging system.

Fields:
  • LOG_LEVEL_NONE – Disables all logs
  • LOG_LEVEL_ERROR – Enables only error logs
  • LOG_LEVEL_WARNING – Enables error and warning logs
  • LOG_LEVEL_INFO – Enables info, warning, and error logs
  • LOG_LEVEL_DEBUG – Enables all logs
Enumerator
LOG_LEVEL_NONE 

Disables all logs

LOG_LEVEL_ERROR 

Enables error logs

LOG_LEVEL_WARNING 

Enables warning logs

LOG_LEVEL_INFO 

Enables info logs

LOG_LEVEL_DEBUG 

Enables all logs

Function Documentation

◆ LOG_output()

static int LOG_output ( log_level_t  level,
const char *  color,
const char *  prefix,
const char *  file,
const char *  func,
int  line,
const char *  fmt,
  ... 
)
inlinestatic

Internal logging implementation: thread‐safe, prints timestamp, optional ANSI color, severity prefix, formatted message, and source location.

This function:

  • Checks if the given level is within the configured LOG_LEVEL. Returns -EIO if the level is too verbose.
  • Locks a static mutex to serialize access from multiple threads.
  • Retrieves the current time (seconds and nanoseconds) and formats a timestamp [HH:MM:SS.mmm].
  • Selects stderr for WARNING or ERROR levels, stdout otherwise.
  • If the output is a TTY, emits the provided color codes around the prefix; otherwise emits only the prefix text.
  • Formats the user message using fmt and variadic arguments.
  • Appends the source location (at file:line:func()).
  • Unlocks the mutex and returns status.
Parameters
[in]levelLog severity (ERROR, WARNING, INFO, DEBUG).
[in]colorANSI color sequence for interactive terminals.
[in]prefixText prefix for the log level (e.g. "[ERROR]").
[in]fileSource file name (FILE) of the call.
[in]funcFunction name (func) of the call.
[in]lineLine number (LINE) of the call.
[in]fmtprintf-style format string for the log message.
[in]...Arguments corresponding to fmt.
Returns
Integer status code indicating initialization result.
Return values
EXIT_SUCCESSMessage was successfully logged.
-EIOThe specified level is more verbose than LOG_LEVEL.