|
oak - a modern logger 1.1.0
|
Welcome to the official Oak documentation. This guide provides comprehensive information and practical examples to help you get started with the Oak logging library.
Oak is a lightweight and robust logging library for C++23, designed to simplify logging in modern C++ applications.
You can simply copy oak.hpp and oak.cpp in your imports and sources respectively. Alternatively, you can add this repository as a git submodule and register it in cmake as a subdirectory, or fetch it using CPM:
There is a single header, oak.hpp:
You can create a local Logger object, its resources will be automatically cleaned when it goes out of scope.
There is also a global logger that is accessible thought static functions with the same signature as a local logger, like oak::log(...) instead of logger.log(...).
You have several logging levels of increasing priority: Debug, Info, Warn and Error. You can set which level is enabled, meaning that only logs that have the same or higher priority will be considered.
Example output:
To collect the file and line of where the log was generated, use the macro:
Other than log levels, oak provides an event API which lets you generate events of a certain type (identified by a number) which will be logged only if event logging for that type is enabled.
Here is an example
Output:
You can imagine how this can be used to trace all memory allocations/deallocations, or network usage / connections on demand. Since the output can be very noisy, it can be enabled and disabled based on what you need to debug.
You tune the logger via getter / setters for the various values. The most important ones are the level, which sets up the logger so that it processes only logs of a certain level or higher, and flags which configure the logger to log additional metadata or with special formatting (such as json or color).
With all flags enabled, the output would look like this:
You can also load the settings from a configuration file:
A config file looks like this:
When you log something, oak will properly format your string and send it to its writers. Conceptually, the logger is a single producer, and the writers are multiple consumers of the log data. Each writer runs on its own thread, and when data is sent to it, it will update a local queue of logs which will be written to some output. The specific output depends on the implementation of the writer. By default, oak provides the FileWriter and StdoutWriter classes, and only the StdoutWriter is enabled by default.
The logger does not wait for the writer to finish writing since each writer has its own log queue, which makes the logger really fast.
You can add and remove writers with the add_writer and remove_writer api:
Before sending the log to the writers, the log input needs to be formatted. For example, the string "Hello, I am {} form {}" needs to be completed, additional metadata needs to be added based on the flags, and other customization such as coloring.
The user can specify a formatter function which can be used instead of the default one, enabling greater customizability and flexilibty.