Logger API Reference
Enumerations
LogLevel
Severity levels for log messages.
enum class LogLevel {
Trace = 0, ///< Trace level (very verbose details)
Debug = 1, ///< Debug level (debugging information)
Info = 2, ///< Information level (general information)
Warn = 3, ///< Warning level (important warnings)
Error = 4, ///< Error level (errors)
Critical = 5 ///< Critical level (critical errors)
};
Usage:
logger::Logger::setup(logger::LogLevel::Debug);
logger::Logger::getInstance().setLogLevel(logger::LogLevel::Info);
Structures
Settings
Configuration structure for the logger.
struct Settings {
LogLevel minimumLevel = LogLevel::Info;
bool showTimestamp = true;
bool showThreadInfo = true;
bool showSourceLocation = true;
bool showFullPath = false;
const char* timestampFormat = "[%Y-%m-%d %H:%M:%S.%f]";
const char* logFileName = "";
size_t maxFileSize = 0;
size_t maxBackupCount = 0;
std::vector<std::string> excludedCategories;
};
Members:
minimumLevel- Minimum log level to displayshowTimestamp- Display timestamp in logsshowThreadInfo- Display thread ID in logsshowSourceLocation- Display file and line numbershowFullPath- Show full path or just filenametimestampFormat- Strftime format string for timestamplogFileName- Log file path (empty = no file)maxFileSize- Max file size before rotation (0 = no rotation)maxBackupCount- Number of backup files to keepexcludedCategories- Categories to exclude from logging
Class: Logger
Singleton logger class with all logging functionality.
Static Methods
getInstance()
Gets the unique logger instance (Singleton pattern).
Returns: Reference to the logger instance
Example:
setup()
static void setup(
LogLevel level = LogLevel::Info,
const char* filename = "",
const std::vector<std::string>& excludedCategories = {},
bool showFullPath = false,
bool showTimestamp = true,
bool showThreadInfo = true,
bool showSourceLocation = true
);
Convenience method to setup and initialize the logger with custom settings.
Parameters:
level- Minimum log level (default: Info)filename- Log file path (default: "")excludedCategories- Categories to exclude (default: {})showFullPath- Show full file path (default: false)showTimestamp- Show timestamp (default: true)showThreadInfo- Show thread info (default: true)showSourceLocation- Show source location (default: true)
Example:
getFileName()
Extracts filename from a full path (used internally by macros).
Parameters:
fullPath- Full file path
Returns: Pointer to filename (after last / or )
Example:
Member Methods
initialize()
Initializes the logger with the given settings.
Parameters:
settings- Configuration object
Example:
logger::Settings settings;
settings.minimumLevel = logger::LogLevel::Debug;
logger::Logger::getInstance().initialize(settings);
isInitialized()
Checks if the logger is currently initialized.
Returns: true if initialized, false otherwise
Example:
setLogLevel()
Changes the minimum log level at runtime.
Parameters:
level- New minimum log level
Example:
getLogLevel()
Gets the current minimum log level.
Returns: Current log level
Example:
if (logger::Logger::getInstance().getLogLevel() <= logger::LogLevel::Debug) {
LOG_DEBUG("Debug info: {}", expensive_calculation());
}
flush()
Flushes all pending logs to disk and console.
Example:
shutdown()
Shuts down the logger cleanly, flushing all pending logs.
Example:
isCategoryExcluded()
Checks if a category is excluded from logging.
Parameters:
category- Category name to check
Returns: true if excluded, false otherwise
Example:
if (!logger::Logger::getInstance().isCategoryExcluded("network")) {
LOG_INFO_CAT("network", "Network message");
}
Logging Macros
Basic Macros
LOG_TRACE
Logs a TRACE level message (most verbose).
Example:
LOG_DEBUG
Logs a DEBUG level message.
Example:
LOG_INFO
Logs an INFO level message.
Example:
LOG_WARN
Logs a WARN level message.
Example:
LOG_ERROR
Logs an ERROR level message.
Example:
LOG_CRITICAL
Logs a CRITICAL level message.
Example:
Category Macros
LOG_TRACE_CAT
Logs a TRACE message with a category.
Parameters:
category- Category name...- Message format and arguments
Example:
LOG_DEBUG_CAT
Logs a DEBUG message with a category.
Example:
LOG_INFO_CAT
Logs an INFO message with a category.
Example:
LOG_WARN_CAT
Logs a WARN message with a category.
Example:
LOG_ERROR_CAT
Logs an ERROR message with a category.
Example:
LOG_CRITICAL_CAT
Logs a CRITICAL message with a category.
Example:
Utility Functions
logLevelToString()
Converts a LogLevel to its string representation.
Parameters:
level- Log level to convert
Returns: String representation ("TRACE", "DEBUG", "INFO", "WARN", "ERROR", "CRITICAL")
Example:
Thread Safety
All logger operations are thread-safe and can be safely called from multiple threads simultaneously.
Example:
std::thread t1([]() { LOG_INFO("From thread 1"); });
std::thread t2([]() { LOG_INFO("From thread 2"); });
t1.join();
t2.join();
// Both messages logged correctly without race conditions
Format String Syntax
The logger uses std::format compatible formatting:
| Format | Description | Example |
|---|---|---|
{} |
Default | LOG_INFO("Value: {}", 42) |
{:d} |
Decimal integer | LOG_INFO("{:d}", 255) |
{:x} |
Hexadecimal | LOG_INFO("{:x}", 255) → ff |
{:#x} |
Hex with prefix | LOG_INFO("{:#x}", 255) → 0xff |
{:.2f} |
Float precision | LOG_INFO("{:.2f}", 3.14159) → 3.14 |
{:>10} |
Right align | LOG_INFO("{:>10}", "hi") → " hi" |
{:<10} |
Left align | LOG_INFO("{:<10}", "hi") → "hi " |
Compilation
Include the logger header:
Link against the logger library in CMakeLists.txt:
Error Handling
The logger is designed to be robust. If initialization fails:
logger::Logger::setup(logger::LogLevel::Info, "invalid/path/file.log");
// Logger will handle the error gracefully and continue with console output
Platform Support
- Linux: Full support
- Windows: Full support with Visual C++