libmemalloc  v4.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(file, ...)   fprintf(file, __VA_ARGS__)
 Compiler-specific printf abstraction.
 
#define LOG_VPRINTF(file, fmt, args)   vfprintf(file, fmt, 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)    __attribute__((format(printf, fmt_idx, var_idx)))
 Macro to apply GCC printf-style format checking on custom functions.
 
#define LOG_ERROR(fmt, ...)
 Logs an error message (red).
 
#define LOG_WARNING(fmt, ...)
 Logs a warning message (yellow).
 
#define LOG_INFO(fmt, ...)
 Logs an info message (blue).
 
#define LOG_DEBUG(fmt, ...)
 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,...) __attribute__((format(printf
 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.

◆ ATTR_PRINTF

#define ATTR_PRINTF (   fmt_idx,
  var_idx 
)     __attribute__((format(printf, fmt_idx, var_idx)))

Macro to apply GCC printf-style format checking on custom functions.

◆ 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 (   fmt,
  ... 
)
Value:
__FILE__, \
__func__, \
__LINE__, \
fmt, \
##__VA_ARGS__)
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,...) __attribute__((format(printf
Internal logging implementation: thread‐safe, prints timestamp, optional ANSI color,...
Definition logs.h:214
#define PREFIX_DEBUG
Definition logs.h:115
#define COLOR_GREEN
Definition logs.h:125
@ LOG_LEVEL_DEBUG
Definition logs.h:63

Logs a debug message (green).

◆ LOG_ERROR

#define LOG_ERROR (   fmt,
  ... 
)
Value:
__FILE__, \
__func__, \
__LINE__, \
fmt, \
##__VA_ARGS__)
#define PREFIX_ERROR
Definition logs.h:112
#define COLOR_RED
Definition logs.h:122
@ LOG_LEVEL_ERROR
Definition logs.h:60

Logs an error message (red).

◆ LOG_INFO

#define LOG_INFO (   fmt,
  ... 
)
Value:
__FILE__, \
__func__, \
__LINE__, \
fmt, \
##__VA_ARGS__)
#define PREFIX_INFO
Definition logs.h:114
#define COLOR_BLUE
Definition logs.h:124
@ LOG_LEVEL_INFO
Definition logs.h:62

Logs an info message (blue).

◆ LOG_LEVEL

#define LOG_LEVEL   LOG_LEVEL_DEBUG

Default log level if not defined.

◆ LOG_PRINTF

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

Compiler-specific printf abstraction.

◆ LOG_VPRINTF

#define LOG_VPRINTF (   file,
  fmt,
  args 
)    vfprintf(file, fmt, args)

Compiler-specific vprintf abstraction.

◆ LOG_WARNING

#define LOG_WARNING (   fmt,
  ... 
)
Value:
__FILE__, \
__func__, \
__LINE__, \
fmt, \
##__VA_ARGS__)
#define COLOR_YELLOW
Definition logs.h:123
#define PREFIX_WARNING
Definition logs.h:113
@ LOG_LEVEL_WARNING
Definition logs.h:61

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 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.