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 Logger & | defaultLogger () |
| 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. | |
Asynchronous thread-safe logging facility.
All log messages are enqueued and written by a dedicated worker thread.
Function type for formatting log messages.
A formatter receives a LogFormat and returns a fully formatted string ready for output.
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. |
|
static |
Returns the default console log formatter.
Produces ANSI-colored lines suitable for terminal output.
|
static |
Returns the default file log formatter.
Produces plain text lines in the format: TIMESTAMP SOURCE:LINE [LEVEL] MESSAGE
|
inline |
Returns the current minimum log level.
Converts a LogLevel to its single character representation.
| level | The log level to convert. |
Enqueues a single log message.
Captures the calling thread's native ID and name automatically.
| loglevel | The severity level of the message. |
| file | The source file name (typically FILE). |
| line | The source line number (typically LINE). |
| msg | The log message text. |
Enqueues multiple log messages with the same timestamp.
Captures the calling thread's native ID and name automatically.
| loglevel | The severity level of the messages. |
| file | The source file name (typically FILE). |
| line | The source line number (typically LINE). |
| lines | A StringList where each entry becomes a separate log line. |
|
inline |
Sets a custom formatter for console log output.
| formatter | The formatter function. Pass an empty std::function to restore the default. |
Enables or disables console (stderr) log output.
| val | true to enable console logging, false to disable. |
|
inline |
Sets a custom formatter for file log output.
| formatter | The formatter function. Pass an empty std::function to restore the default. |
Sets the log output file.
| filename | Path to the log file. The file is opened by the worker thread. |
Changes the minimum log level.
| level | The new minimum log level. Messages below this are discarded. |
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.
| name | The new thread name. |
Blocks until all queued log commands have been processed.
| timeoutMs | Maximum time to wait in milliseconds. A value of zero (the default) waits indefinitely. |