How-To create log output
There are so many logging libraries for C++, the choice is just too great. So of course, liberate adds another one!
Just kidding.
Instead of writing yet another logging library or simply locking everything we do into one, we provide logging via one of the following options:
The reason for these is that they are popular, and available as meson
subprojects, so compiling them in is pretty easy. We provide a uniform
interface for logging at the following log levels:
Liberate |
loguru |
plog |
spdlog |
---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Writing to the log is simple. Use the LIBLOG
macro, provide a log level
and a message. The message is assembled via a std::ostream
interface, so
you can use everything you know from there.
1#include <liberate/logging.h>
2
3LIBLOG(LIBLOG_LEVEL_DEBUG, "This is a message: " << 42);
4
5// or use a convenience macro
6
7LIBLOG_FATAL("Uh-oh!");
Note
This interface was great for getting started, but we may change it in future. Long story short, it doesn’t make it very easy to switch different log levels for different sub-projects, and we’re starting to need that.
Compile-Time Configuration
You can select the backend to use by providing the log_backend meson option, or setting e.g. -DLIBERATE_LOG_BACKEND=stderr when configuring liberate.
If your build has -DDEBUG
configured, these log messages then get produced.
If you use one of the logging libraries, you can use its interface to configure
the log level you desire. Should you provide -DNDEBUG
on the other hand,
all log macros get optimized away and no output is produced.
Additional Macros
The
LIBLOG_ERR
takes an error code and a message. It outputs the message followed by the message the system associates with the error code.The
LIBLOG_ERRNO
just takes a message, and uses the last system error code to pass on toLIBLOG_ERR
.The
LIBLOG_EXC
takes an exception and a message, outputs the message followed by the exception’swhat()
message.