ulogger
athreadsafeloggerwithcoloredoutput
|
ulog
(uber log) is a lightweight and threadsafe logging library written in C, with support for C++. It features color coded output, with the ability to send logs to stdout and a file. File and line information indicating what fired the log is also included. It has INFO, WARN, ERROR, and DEBUG log levels, and is thoroughly tested with cmocka and valgrind.
If not using debug logging then any DEBUG level log calls are silently skipped. The logger is threadsafe in that multiple threads can't log at the same time. In practice there is very little lock contention and in all honesty you will probably never have to worry about it.
In terms of memory usage, the only memory allocations conducted by this library are when initializing the logger. During actual logging there is no memory allocations whatsoever, as we use stack allocated variables. In practice logger initialization consumes arounds 7.4 KiB of memory, while regular logger usage general consumes no more than 3 -> 3.4 KiB of memory at any one time.
Please be aware that after calling clear_thread_logger
or clear_file_logger
using the logger results in undefined behavior, likely a panic causing the program to exit. Having one or more threads initiate a log invocation while concurrently calling clear_thread_logger
or clear_file_logger
results in undefined behavior. When clearing the logger you must be certain no other threads will attempt to use the logger.
I wanted a simple logging library that didnt leak memory, was well tested, and capable of color coded output. All logging libraries I found were complex code bases, leaked memory, and relied on global variables. Because of that, and as a way to better learn C development ulog
was born.
Interested in reading more about how ulog
was born? I published a blog post detailing the creation.
The library follows the semver versioning scheme. Additionally a version.h
header file has the current release version listed as a macro.
Copy and paste the logger.h
, colors.h
, version.h
, logger.c
, and colors.c
files into whatever project you are working on. You will need to make sure that you have pthreads available to link with as the logger library has a pthreads dependency.
If you use the clib package manager then you can install ulog
into your project with a single command:
Before testing you'll need to compile the code, for which you have two options
Release mode
Debug mode
Running either of the following two commands will build a test executable logger-test
within the build
folder. You can either invoke this manually or use ctest
.
Regular unit testing:
Valgrind dynamic analysis and unit testing:
The primary method of interacting with ulog is by using macros. The macros allow you to emit logs at various levels, minimizing the amount of typing required to do so. There are a total of four macros that can be used, the base macros are denoted in the form of LOG_<LEVEL>
and LOGF_<LEVEL>
which provide the capabilities to emit logs to standard out. The LOG_
macros can be used to emit a log message as is, that is to say you provide a single message to emit, while the LOGF_
macros can be used to emit a log message formatted according to the printf formatting rules leveraging variadic arguments.
There are two additional macros that mimic LOG_<LEVEL>
and LOGF_<LEVEL>
however they will also log to a given file, while also logging to stdout. They are fLOG_<LEVEL>
and fLOG_<LEVEL>
. The main difference between these, other than the fact that file logging capabilities are provided, is that the LOG_<LEVEL>
and LOGF_<LEVEL>
macros take in an instance of thread_logger
while the fLOG_<LEVEL>
and fLOGF_<LEVEL>
macros take in an instance of file_logger
.
Below you'll find examples that showcase how to generate the logs that were captured in the screenshot displayed at the beginning of this readme.
AGPLv3 licensed, although if you want commercial license under MIT that can be aranged for a small fee.