HerrLog  v0.2
Header file logging library
Herr Log

A single header lightweight logging library.

Download

Simply download the project and import herrlog.hh. Or run the below given curl command in your directory.

curl -o herrlog.hh -L https://raw.githubusercontent.com/Saphereye/herr-log/main/herrlog.hh

Usage

Herr Log supports the following log levels.

Log Level Use Case
Trace Capture fine-grained details and trace specific actions, offering insights into the program's behavior (e.g., variable values, function calls).
Info Report significant events, milestones, or key actions in the program's execution (e.g., program start, major processes).
Debug Intended for temporary and detailed reporting during development or debugging phases to comprehend and analyze specific parts of the code. Use sparingly in production.
Warn Report errors or potential areas where errors have not been handled gracefully. Does not terminate the program.
Error Report critical errors requiring immediate attention. Results in program termination.
Fatal Report critical errors, terminate the program, and generate a crash dump (abort).

All these can be activated/deactivated independently by setting the style using Logger::set_type.

Basic usage

#include "herrlog.hh"
int main() {
Logger::trace("This is trace message number 1"); // Initialization is not required
}
static void trace(const char *format, Args... args)
Logs messages of type trace.
Definition: herrlog.hh:277
Header file only logging library.

By default, the style is LogStyle::All, i.e. all log types are enabled. The output is directed to the console using anse colors and the datetime format is "%Y-%m-%d %H:%M:%S".

Custom output stream (e.g. logfiles)

In place of log_file, any other stream can also be put.

#include "herrlog.hh"
int main() {
Logger::set_output_file_name("test.log"); // The output will be redirected to "test.log"
Logger::trace("This is trace message number 1");
}
static void set_output_file_name(std::string output_file_name)
Set the output file name object.
Definition: herrlog.hh:222

Custom output message types

For example, if only error and info messages are needed.

#include "herrlog.hh"
int main() {
Logger::set_type(LogType::Error | LogType::Debug); // Only Error and Debug logs will be shown
Logger::error("Erroneous error"); // This will be printed
Logger::trace("Tracing something"); // This wouldn't be printed
}
static void error(const char *format, Args... args)
Logs messages of type error. Furthermore closes the output file and exits the program.
Definition: herrlog.hh:327
static void set_type(LogType log_type)
Sets the log type, default is LogType::All.
Definition: herrlog.hh:215

Inbuilt formatting

#include "herrlog.hh"
int main() {
Logger::trace("This is {} message number {}", "trace", 1); // Will be printed as "[TRACE 2023-12-31 12:41:09] This is trace message number 1"
}

Custom datetime format

The default datetime format is Y-m-d H:M:S. These variables can then be used to specify a custom date time format.

#include "herrlog.hh"
int main() {
Logger::trace("Hello World"); // Will be printed as "[TRACE 12:35:24] Hello World"
}
static void set_datetime_format(const char *datetime_format)
Set the datetime format object, default is "%Y-%m-%d %H:%M:%S".
Definition: herrlog.hh:245