Logger¶
Overview¶
Simple logging utility using plog.
The omdi::logger namespace provides a small wrapper around
the plog logging library, with convenience functions for:
initializing logging with a default severity,
generic formatted logging with a template severity parameter,
severity-specific helpers (
omdi::logger::Debug(),omdi::logger::Warning(),omdi::logger::Error(),omdi::logger::Fatal()).
Messages are formatted using omdi::fmt::vformat() with a
printf-style format string and a variable argument list.
Usage example¶
Basic initialization and logging:
// Initialize logging with default debug severity
omdi::logger::Init();
omdi::logger::Debug("Application started: version=%s", "1.0.0");
omdi::logger::Warning("Low memory: %d MB remaining", 128);
omdi::logger::Error("Failed to open file: %s", "data.txt");
// Generic logging with custom severity (template parameter)
omdi::logger::Log<plog::info>("User %s logged in", "alice");
// Fatal logs throw after logging
try {
omdi::logger::Fatal("Unrecoverable error: code=%d", -1);
} catch (const std::runtime_error& e) {
// Handle or report fatal error
}
Types¶
-
using Severity = plog::Severity¶
Alias for
plog::Severity, used to represent log severity levels such asplog::debug,plog::info,plog::warning,plog::error, andplog::fatal.
Initialization¶
-
void Init(const Severity &minSeverity = plog::debug)¶
Initialize the logging system.
- Parameters:
minSeverity – Minimum severity level for messages to be logged (defaults to
plog::debug).
This function sets up
plogsinks/appenders according to your application’s configuration (implementation-specific). It should typically be called once at application startup before any logging occurs.
Generic logging¶
-
template<Severity S = plog::debug>
void Log(const char *fmt, ...)¶ Log a formatted message at compile-time-selected severity S.
- Template Parameters:
S – Compile-time severity level (defaults to
plog::debug).- Parameters:
fmt –
printf-style format string.... – Arguments corresponding to fmt.
Example:
omdi::logger::Log<plog::info>("Loaded %d items from %s", count, filename);
Internally, this formats the message with
omdi::fmt::vformat()and sends it toplogviaPLOG(S).
Severity-specific helpers¶
-
void Debug(const char *fmt, ...)¶
Log a debug-level message.
- Parameters:
fmt –
printf-style format string.... – Arguments corresponding to fmt.
Equivalent to calling
omdi::logger::Log()withS = plog::debug.
-
void Warning(const char *fmt, ...)¶
Log a warning-level message.
- Parameters:
fmt –
printf-style format string.... – Arguments corresponding to fmt.
-
void Error(const char *fmt, ...)¶
Log an error-level message.
- Parameters:
fmt –
printf-style format string.... – Arguments corresponding to fmt.
-
[[noreturn]] void Fatal(const char *fmt, ...)¶
Log a fatal-level message and then throw an exception.
- Parameters:
fmt –
printf-style format string.... – Arguments corresponding to fmt.
This function:
formats the message using
omdi::fmt::vformat(),logs it with severity
plog::fatal,and then throws
std::runtime_errorcontaining the message.
Because it is marked
[[noreturn]], it is intended for unrecoverable errors where normal execution cannot continue.
Notes¶
All logging functions use a
printf-style interface. The number and types of arguments must match the format string.The actual configuration of sinks (console, file, etc.) is determined by the implementation of
omdi::logger::Init().omdi::logger::Fatal()always throws after logging; callers should be prepared to handlestd::runtime_errorif they wish to catch it.