libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
Logger Class Reference

Asynchronous thread-safe logging facility. More...

#include <logger.h>

Classes

struct  LogEntry
 A single log entry. More...
 
struct  LogFormat
 Context passed to formatters, combining entry data with resolved thread name. More...
 

Public Types

enum  LogLevel {
  Force = 0 , Debug = 1 , Info = 2 , Warn = 3 ,
  Err = 4
}
 Severity levels for log messages. More...
 
using LogFormatter = std::function< String(const LogFormat &fmt)>
 Function type for formatting log messages.
 

Public Member Functions

 Logger ()
 Constructs a Logger and starts the worker thread.
 
 ~Logger ()
 Destructor. Signals the worker thread to terminate and waits for it to finish.
 
int level () const
 Returns the current minimum log level.
 
void log (LogLevel loglevel, const char *file, int line, const String &msg)
 Enqueues a single log message.
 
void log (LogLevel loglevel, const char *file, int line, const StringList &lines)
 Enqueues multiple log messages with the same timestamp.
 
void setLogFile (const String &filename)
 Sets the log output file.
 
void setLogLevel (LogLevel level)
 Changes the minimum log level.
 
void setConsoleLoggingEnabled (bool val)
 Enables or disables console (stderr) log output.
 
LogFormatter fileFormatter () const
 Returns the current file log formatter.
 
LogFormatter consoleFormatter () const
 Returns the current console log formatter.
 
void setFileFormatter (LogFormatter formatter)
 Sets a custom formatter for file log output.
 
void setConsoleFormatter (LogFormatter formatter)
 Sets a custom formatter for console log output.
 
Error sync (unsigned int timeoutMs=0)
 Blocks until all queued log commands have been processed.
 

Static Public Member Functions

static LoggerdefaultLogger ()
 Returns the singleton default Logger instance.
 
static char levelToChar (LogLevel level)
 Converts a LogLevel to its single character representation.
 
static void setThreadName (const String &name)
 Updates the cached thread name used in log output.
 
static LogFormatter defaultFileFormatter ()
 Returns the default file log formatter.
 
static LogFormatter defaultConsoleFormatter ()
 Returns the default console log formatter.
 

Detailed Description

Asynchronous thread-safe logging facility.

All log messages are enqueued and written by a dedicated worker thread.

Example
// Use convenience macros (most common)
promekiInfo("Processing frame %d", frameNum);
promekiWarn("Buffer underrun at %s", tc.toString().first.cStr());
promekiErr("Failed to open %s", path.cStr());
// Configure the logger
Logger::defaultLogger().setLogFile("/tmp/app.log");
Dynamic array container wrapping std::vector.
Definition list.h:40
static Logger & defaultLogger()
Returns the singleton default Logger instance.
@ Debug
Debug-level messages.
Definition logger.h:107
Supports multiple log levels, optional console output, and file logging.

Member Typedef Documentation

◆ LogFormatter

Function type for formatting log messages.

A formatter receives a LogFormat and returns a fully formatted string ready for output.

Member Enumeration Documentation

◆ LogLevel

Severity levels for log messages.

Enumerator
Force 

Forced messages are always logged.

Debug 

Debug-level messages.

Info 

Informational messages.

Warn 

Warning messages.

Err 

Error messages.

Member Function Documentation

◆ defaultConsoleFormatter()

static LogFormatter Logger::defaultConsoleFormatter ( )
static

Returns the default console log formatter.

Produces ANSI-colored lines suitable for terminal output.

◆ defaultFileFormatter()

static LogFormatter Logger::defaultFileFormatter ( )
static

Returns the default file log formatter.

Produces plain text lines in the format: TIMESTAMP SOURCE:LINE [LEVEL] MESSAGE

◆ defaultLogger()

static Logger & Logger::defaultLogger ( )
static

Returns the singleton default Logger instance.

Returns
A reference to the default Logger.

◆ level()

int Logger::level ( ) const
inline

Returns the current minimum log level.

Returns
The log level as an integer.

◆ levelToChar()

static char Logger::levelToChar ( LogLevel  level)
static

Converts a LogLevel to its single character representation.

Parameters
levelThe log level to convert.
Returns
A character such as D (debug), W (warning), I (info), etc

◆ log() [1/2]

void Logger::log ( LogLevel  loglevel,
const char file,
int  line,
const String msg 
)

Enqueues a single log message.

Captures the calling thread's native ID and name automatically.

Parameters
loglevelThe severity level of the message.
fileThe source file name (typically FILE).
lineThe source line number (typically LINE).
msgThe log message text.

◆ log() [2/2]

void Logger::log ( LogLevel  loglevel,
const char file,
int  line,
const StringList lines 
)

Enqueues multiple log messages with the same timestamp.

Captures the calling thread's native ID and name automatically.

Parameters
loglevelThe severity level of the messages.
fileThe source file name (typically FILE).
lineThe source line number (typically LINE).
linesA StringList where each entry becomes a separate log line.

◆ setConsoleFormatter()

void Logger::setConsoleFormatter ( LogFormatter  formatter)
inline

Sets a custom formatter for console log output.

Parameters
formatterThe formatter function. Pass an empty std::function to restore the default.

◆ setConsoleLoggingEnabled()

void Logger::setConsoleLoggingEnabled ( bool  val)
inline

Enables or disables console (stderr) log output.

Parameters
valtrue to enable console logging, false to disable.

◆ setFileFormatter()

void Logger::setFileFormatter ( LogFormatter  formatter)
inline

Sets a custom formatter for file log output.

Parameters
formatterThe formatter function. Pass an empty std::function to restore the default.

◆ setLogFile()

void Logger::setLogFile ( const String filename)
inline

Sets the log output file.

Parameters
filenamePath to the log file. The file is opened by the worker thread.

◆ setLogLevel()

void Logger::setLogLevel ( LogLevel  level)
inline

Changes the minimum log level.

Parameters
levelThe new minimum log level. Messages below this are discarded.

◆ setThreadName()

static void Logger::setThreadName ( const String name)
static

Updates the cached thread name used in log output.

The logger caches the calling thread's name on first use. Call this to update the cached name if the thread is renamed after its first log message. Thread::setName() calls this automatically.

Parameters
nameThe new thread name.

◆ sync()

Error Logger::sync ( unsigned int  timeoutMs = 0)
inline

Blocks until all queued log commands have been processed.

Parameters
timeoutMsMaximum time to wait in milliseconds. A value of zero (the default) waits indefinitely.
Returns
Error::Ok if the sync completed, Error::Timeout if the timeout elapsed first.

The documentation for this class was generated from the following file: