libcmutils 0.6.5
Multi-platform C99 utility library
Loading...
Searching...
No Matches
Log system, loggers and appenders.

Data Structures

struct  CMUTIL_ConfLogger
 Logger configuration object. More...
 
struct  CMUTIL_Logger
 Logger object. More...
 
struct  CMUTIL_LogAppender
 Log appender object. More...
 
struct  CMUTIL_LogSystem
 Log system object. More...
 

Macros

#define CMUTIL_LogDefine(name)
 Define a logger for the current source file.
 
#define CMLogIsEnabled(level)   CMUTIL_LogIsEnabled(g__CMUTIL_GetLogger(),level)
 Macro to check if logging is enabled for the specified log level.
 
#define CMUTIL_Log__(level, stack, f, ...)
 Log a message with the specified log level and stack trace option.
 
#define CMUTIL_Log2__(level, stack, f, ...)
 Log a message with the specified log level and stack trace option.
 
#define CMLogTrace(f, ...)   CMUTIL_Log__(Trace,False,f,##__VA_ARGS__)
 Log a message at the Trace level.
 
#define CMLogTraceS(f, ...)   CMUTIL_Log__(Trace,True ,f,##__VA_ARGS__)
 Log a message at the Trace level with stack trace.
 
#define CMLogDebug(f, ...)   CMUTIL_Log__(Debug,False,f,##__VA_ARGS__)
 Log a message at the Debug level.
 
#define CMLogDebugS(f, ...)   CMUTIL_Log__(Debug,True ,f,##__VA_ARGS__)
 Log a message at the Debug level with stack trace.
 
#define CMLogInfo(f, ...)   CMUTIL_Log__(Info ,False,f,##__VA_ARGS__)
 Log a message at the Info level.
 
#define CMLogInfoS(f, ...)   CMUTIL_Log__(Info ,True ,f,##__VA_ARGS__)
 Log a message at the Info level with stack trace.
 
#define CMLogWarn(f, ...)   CMUTIL_Log__(Warn ,False,f,##__VA_ARGS__)
 Log a message at the Warn level.
 
#define CMLogWarnS(f, ...)   CMUTIL_Log__(Warn ,True ,f,##__VA_ARGS__)
 Log a message at the Warn level with stack trace.
 
#define CMLogError(f, ...)   CMUTIL_Log__(Error,False,f,##__VA_ARGS__)
 Log a message at the Error level.
 
#define CMLogErrorS(f, ...)   CMUTIL_Log__(Error,True ,f,##__VA_ARGS__)
 Log a message at the Error level with stack trace.
 
#define CMLogFatal(f, ...)   CMUTIL_Log__(Fatal,False,f,##__VA_ARGS__)
 Log a message at the Fatal level.
 
#define CMLogFatalS(f, ...)   CMUTIL_Log__(Fatal,True ,f,##__VA_ARGS__)
 Log a message at the Fatal level with stack trace.
 
#define CMLog(l, f, ...)   CMUTIL_Log2__(l,False,f,##__VA_ARGS__)
 Log a message with the specified log level.
 
#define CMLogS(l, f, ...)   CMUTIL_Log2__(l,True ,f,##__VA_ARGS__)
 Log a message with the specified log level and stack trace.
 

Enumerations

enum  CMLogLevel {
  CMLogLevel_Trace = 0 , CMLogLevel_Debug , CMLogLevel_Info , CMLogLevel_Warn ,
  CMLogLevel_Error , CMLogLevel_Fatal
}
 Log severity levels. More...
 
enum  CMLogTerm {
  CMLogTerm_Year = 0 , CMLogTerm_Month , CMLogTerm_Date , CMLogTerm_Hour ,
  CMLogTerm_Minute
}
 Log rolling terms. More...
 

Functions

CMUTIL_LogAppenderCMUTIL_LogConsoleAppenderCreate (const char *name, const char *pattern, CMBool use_stderr)
 Create a console log appender.
 
CMUTIL_LogAppenderCMUTIL_LogFileAppenderCreate (const char *name, const char *fpath, const char *pattern)
 Create a file log appender.
 
CMUTIL_LogAppenderCMUTIL_LogRollingFileAppenderCreate (const char *name, const char *fpath, CMLogTerm logterm, const char *rollpath, const char *pattern)
 Create a rolling file log appender.
 
CMUTIL_LogAppenderCMUTIL_LogSocketAppenderCreate (const char *name, const char *accept_host, int listen_port, const char *pattern)
 Create a socket log appender.
 
void CMUTIL_LogFallback (CMLogLevel level, const char *file, int line, const char *fmt,...)
 Fallback logging function when no logger is available.
 
CMBool CMUTIL_LogIsEnabled (CMUTIL_Logger *logger, CMLogLevel level)
 Check if logging is enabled for the specified log level.
 
CMUTIL_LogSystemCMUTIL_LogSystemCreate (void)
 Create a new log system object.
 
CMUTIL_LogSystemCMUTIL_LogSystemConfigureFomJson (const char *jsonfile)
 Configure the log system from a JSON configuration file.
 
CMUTIL_LogSystemCMUTIL_LogSystemGet (void)
 Get the current global log system.
 
void CMUTIL_LogSystemSet (CMUTIL_LogSystem *lsys)
 Set the global log system.
 

Detailed Description

Macro Definition Documentation

◆ CMUTIL_LogDefine

#define CMUTIL_LogDefine (   name)
Value:
static CMUTIL_Logger *l___logger = NULL; \
static CMUTIL_LogSystem *s__lsys = NULL; \
static CMUTIL_Logger *g__CMUTIL_GetLogger() { \
if (l___logger == NULL || s__lsys != CMUTIL_LogSystemGet()) { \
s__lsys = CMUTIL_LogSystemGet(); \
if (s__lsys) l___logger = CMCall(s__lsys, GetLogger, name); \
else l___logger = NULL; \
} \
return l___logger; \
}
#define CMCall
Method caller for this library.
Definition libcmutils.h:475
CMUTIL_LogSystem * CMUTIL_LogSystemGet(void)
Get the current global log system.
Log system object.
Definition libcmutils.h:4357
Logger object.
Definition libcmutils.h:3923

Define a logger for the current source file.

This macro defines a static logger instance for the current source file with the specified name. It provides a function to retrieve the logger instance, creating it if it does not already exist or if the log system has changed.

Parameters
nameThe name of the logger.

◆ CMLogIsEnabled

#define CMLogIsEnabled (   level)    CMUTIL_LogIsEnabled(g__CMUTIL_GetLogger(),level)

Macro to check if logging is enabled for the specified log level.

This macro checks whether logging is enabled for the given log level in the default logger.

Parameters
levelThe log level to check.
Returns
CMTrue if logging is enabled for the specified level, CMFalse otherwise.

◆ CMUTIL_Log__

#define CMUTIL_Log__ (   level,
  stack,
  f,
  ... 
)
Value:
do { \
CMUTIL_Logger *g__logger = g__CMUTIL_GetLogger(); \
if (g__logger) \
g__logger->LogEx(g__logger, CMLogLevel_##level,__FILE__,__LINE__, \
CM##stack,f,##__VA_ARGS__); \
else \
CMUTIL_LogFallback(CMLogLevel_##level, __FILE__, __LINE__, \
f, ##__VA_ARGS__); \
} while(0)
void(* LogEx)(CMUTIL_Logger *logger, CMLogLevel level, const char *file, int line, CMBool printStack, const char *fmt,...)
Log a message with extended information.
Definition libcmutils.h:3939

Log a message with the specified log level and stack trace option.

This macro logs a message at the specified log level, including the source file name, line number, and optionally a stack trace.

Parameters
levelThe simple log level for the message.
stackA boolean indicating whether to include a stack trace.
fThe format string for the log message.
...Additional arguments for the format string.

◆ CMUTIL_Log2__

#define CMUTIL_Log2__ (   level,
  stack,
  f,
  ... 
)
Value:
do { \
CMUTIL_Logger *g__logger = g__CMUTIL_GetLogger(); \
if (g__logger) \
g__logger->LogEx(g__logger, level,__FILE__,__LINE__, \
CM##stack,f,##__VA_ARGS__); \
else \
CMUTIL_LogFallback(level, __FILE__, __LINE__, \
f, ##__VA_ARGS__); \
} while(0)

Log a message with the specified log level and stack trace option.

This macro logs a message at the specified log level, including the source file name, line number, and optionally a stack trace.

Parameters
levelThe full log level name for the message.
stackA boolean indicating whether to include a stack trace.
fThe format string for the log message.
...Additional arguments for the format string.

◆ CMLogTrace

#define CMLogTrace (   f,
  ... 
)    CMUTIL_Log__(Trace,False,f,##__VA_ARGS__)

Log a message at the Trace level.

This macro logs a message at the Trace level, including the source file name and line number.

Parameters
fThe format string for the log message.
...Additional arguments for the format string.

◆ CMLogTraceS

#define CMLogTraceS (   f,
  ... 
)    CMUTIL_Log__(Trace,True ,f,##__VA_ARGS__)

Log a message at the Trace level with stack trace.

This macro logs a message at the Trace level, including the source file name, line number, and a stack trace.

Parameters
fThe format string for the log message.
...Additional arguments for the format string.

◆ CMLogDebug

#define CMLogDebug (   f,
  ... 
)    CMUTIL_Log__(Debug,False,f,##__VA_ARGS__)

Log a message at the Debug level.

This macro logs a message at the Debug level, including the source file name and line number.

Parameters
fThe format string for the log message.
...Additional arguments for the format string.

◆ CMLogDebugS

#define CMLogDebugS (   f,
  ... 
)    CMUTIL_Log__(Debug,True ,f,##__VA_ARGS__)

Log a message at the Debug level with stack trace.

This macro logs a message at the Debug level, including the source file name, line number, and a stack trace.

Parameters
fThe format string for the log message.
...Additional arguments for the format string.

◆ CMLogInfo

#define CMLogInfo (   f,
  ... 
)    CMUTIL_Log__(Info ,False,f,##__VA_ARGS__)

Log a message at the Info level.

This macro logs a message at the Info level, including the source file name and line number.

Parameters
fThe format string for the log message.
...Additional arguments for the format string.

◆ CMLogInfoS

#define CMLogInfoS (   f,
  ... 
)    CMUTIL_Log__(Info ,True ,f,##__VA_ARGS__)

Log a message at the Info level with stack trace.

This macro logs a message at the Info level, including the source file name, line number, and a stack trace.

Parameters
fThe format string for the log message.
...Additional arguments for the format string.

◆ CMLogWarn

#define CMLogWarn (   f,
  ... 
)    CMUTIL_Log__(Warn ,False,f,##__VA_ARGS__)

Log a message at the Warn level.

This macro logs a message at the Warn level, including the source file name and line number.

Parameters
fThe format string for the log message.
...Additional arguments for the format string.

◆ CMLogWarnS

#define CMLogWarnS (   f,
  ... 
)    CMUTIL_Log__(Warn ,True ,f,##__VA_ARGS__)

Log a message at the Warn level with stack trace.

This macro logs a message at the Warn level, including the source file name, line number, and a stack trace.

Parameters
fThe format string for the log message.
...Additional arguments for the format string.

◆ CMLogError

#define CMLogError (   f,
  ... 
)    CMUTIL_Log__(Error,False,f,##__VA_ARGS__)

Log a message at the Error level.

This macro logs a message at the Error level, including the source file name and line number.

Parameters
fThe format string for the log message.
...Additional arguments for the format string.

◆ CMLogErrorS

#define CMLogErrorS (   f,
  ... 
)    CMUTIL_Log__(Error,True ,f,##__VA_ARGS__)

Log a message at the Error level with stack trace.

This macro logs a message at the Error level, including the source file name, line number, and a stack trace.

Parameters
fThe format string for the log message.
...Additional arguments for the format string.

◆ CMLogFatal

#define CMLogFatal (   f,
  ... 
)    CMUTIL_Log__(Fatal,False,f,##__VA_ARGS__)

Log a message at the Fatal level.

This macro logs a message at the Fatal level, including the source file name and line number.

Parameters
fThe format string for the log message.
...Additional arguments for the format string.

◆ CMLogFatalS

#define CMLogFatalS (   f,
  ... 
)    CMUTIL_Log__(Fatal,True ,f,##__VA_ARGS__)

Log a message at the Fatal level with stack trace.

This macro logs a message at the Fatal level, including the source file name, line number, and a stack trace.

Parameters
fThe format string for the log message.
...Additional arguments for the format string.

◆ CMLog

#define CMLog (   l,
  f,
  ... 
)    CMUTIL_Log2__(l,False,f,##__VA_ARGS__)

Log a message with the specified log level.

This macro logs a message at the specified log level, including the source file name, line number.

Parameters
lThe full log level for the message.
fThe format string for the log message.
...Additional arguments for the format string.

◆ CMLogS

#define CMLogS (   l,
  f,
  ... 
)    CMUTIL_Log2__(l,True ,f,##__VA_ARGS__)

Log a message with the specified log level and stack trace.

This macro logs a message at the specified log level, including the source file name, line number, and stack trace.

Parameters
lThe full log level for the message.
fThe format string for the log message.
...Additional arguments for the format string.

Enumeration Type Documentation

◆ CMLogLevel

enum CMLogLevel

Log severity levels.

Enumerator
CMLogLevel_Trace 

Trace level for detailed debugging information.

CMLogLevel_Debug 

Debug level for general debugging information.

CMLogLevel_Info 

Info level for informational messages.

CMLogLevel_Warn 

Warn level for warning messages.

CMLogLevel_Error 

Error level for error messages.

CMLogLevel_Fatal 

Fatal level for critical error messages.

◆ CMLogTerm

enum CMLogTerm

Log rolling terms.

Enumerator
CMLogTerm_Year 

Yearly log rolling.

CMLogTerm_Month 

Monthly log rolling.

CMLogTerm_Date 

Daily log rolling.

CMLogTerm_Hour 

Hourly log rolling.

CMLogTerm_Minute 

Minutely log rolling.

Function Documentation

◆ CMUTIL_LogConsoleAppenderCreate()

CMUTIL_LogAppender * CMUTIL_LogConsoleAppenderCreate ( const char *  name,
const char *  pattern,
CMBool  use_stderr 
)

Create a console log appender.

Parameters
nameThe name of the log appender.
patternThe log message pattern.
use_stderrWhether to use stderr instead of stdout for logging.
Returns
A pointer to the newly created CMUTIL_LogAppender object, or NULL on failure.

◆ CMUTIL_LogFileAppenderCreate()

CMUTIL_LogAppender * CMUTIL_LogFileAppenderCreate ( const char *  name,
const char *  fpath,
const char *  pattern 
)

Create a file log appender.

Parameters
nameThe name of the log appender.
fpathThe file path for the log file.
patternThe log message pattern.
Returns
A pointer to the newly created CMUTIL_LogAppender object, or NULL on failure.

◆ CMUTIL_LogRollingFileAppenderCreate()

CMUTIL_LogAppender * CMUTIL_LogRollingFileAppenderCreate ( const char *  name,
const char *  fpath,
CMLogTerm  logterm,
const char *  rollpath,
const char *  pattern 
)

Create a rolling file log appender.

Parameters
nameThe name of the log appender.
fpathThe file path for the log file.
logtermThe log rolling term.
rollpathThe roll file path pattern.
patternThe log message pattern.
Returns
A pointer to the newly created CMUTIL_LogAppender object, or NULL on failure.

◆ CMUTIL_LogSocketAppenderCreate()

CMUTIL_LogAppender * CMUTIL_LogSocketAppenderCreate ( const char *  name,
const char *  accept_host,
int  listen_port,
const char *  pattern 
)

Create a socket log appender.

Parameters
nameThe name of the log appender.
accept_hostListen host address.(0.0.0.0 for any address)
listen_portListen port number.
patternThe log message pattern.
Returns
A pointer to the newly created CMUTIL_LogAppender object, or NULL on failure.

◆ CMUTIL_LogFallback()

void CMUTIL_LogFallback ( CMLogLevel  level,
const char *  file,
int  line,
const char *  fmt,
  ... 
)

Fallback logging function when no logger is available.

Parameters
levelLog level.
fileSource file name.
lineLine number in source file.
fmtFormat string for the log message.
...Additional arguments for the format string.

◆ CMUTIL_LogIsEnabled()

CMBool CMUTIL_LogIsEnabled ( CMUTIL_Logger logger,
CMLogLevel  level 
)

Check if logging is enabled for the specified log level.

This function checks whether logging is enabled for the given log level in the specified logger.

Parameters
loggerA pointer to the CMUTIL_Logger object.
levelThe log level to check.
Returns
CMTrue if logging is enabled for the specified level, CMFalse otherwise.

◆ CMUTIL_LogSystemCreate()

CMUTIL_LogSystem * CMUTIL_LogSystemCreate ( void  )

Create a new log system object.

Returns
A pointer to the newly created CMUTIL_LogSystem object, or NULL on failure.

◆ CMUTIL_LogSystemConfigureFomJson()

CMUTIL_LogSystem * CMUTIL_LogSystemConfigureFomJson ( const char *  jsonfile)

Configure the log system from a JSON configuration file.

The result is installed as the global log system, replacing and destroying whatever was installed before, so the return value does not have to be passed to CMUTIL_LogSystemSet - and passing it there does nothing.

A missing or invalid file is not a failure: a built-in console configuration is installed instead, so logging never breaks a program that was configured badly.

Parameters
jsonfileThe path to the JSON configuration file.
Returns
A pointer to the configured CMUTIL_LogSystem object, or NULL on failure.

◆ CMUTIL_LogSystemGet()

CMUTIL_LogSystem * CMUTIL_LogSystemGet ( void  )

Get the current global log system.

Returns
A pointer to the current CMUTIL_LogSystem object, or NULL if no log system is set.

◆ CMUTIL_LogSystemSet()

void CMUTIL_LogSystemSet ( CMUTIL_LogSystem lsys)

Set the global log system.

The log system already installed is destroyed, and ownership of lsys passes to the library. Setting the one that is already installed does nothing, which matters because the configure functions install their own result: passing what one of them returned back to this function would otherwise destroy the live log system.

Parameters
lsysA pointer to the CMUTIL_LogSystem object to set as the global log system.