This repository contains an implementation example of an abstract logger in a C++ library.
Table of contents :
Implementation example requires at least C++ 11 standard
No dependencies needed
This repository contains an example of an Abstract logger that can be useful when developping a library. This allow to not be tied to a specific log dependency/api, allowing users to choose their own (spdlog, glog, etc...).
Tip
If needed, a library template repository also exist to help for the library creation
Abstract Logger behaviour can be found inside folder lib/logs, some tweaks are needed and directly dependant to developped library:
- Set proper namespace in all 3 classes(in our example, libra is the name of our library)
- Set log macros names (replace
#define LIBRA_LOG_FATAL(msg)
byYOURLIBNAME_LOG_FATAL(msg)
)
That is all, now to you use log, caller just need to call proper macro (example can be found inside mock class Person).
Tip
For easier integration, a single pair header/source exists at lib/logs_single_file folder
By default, no logs will be printed or written, users of library must explicitly define a class which inherit from ILogger
class.
This repository provide some examples of ILogger
subclasses inside example/sinks:
- Logger Qt: subclass implementation to be compatible with Qt logging behaviour
Once a sink class has been implemented, we simply have to register this sink for the library via:
int main(int argc, char *argv[])
{
/* Set logs */
MyCustomSink libSink;
libra::LogManager::setLogger(&libSink);
/* Then proceed to the other "main" tasks */
}
Tip
Example can be found inside example/main.cpp file
All methods has been documented with Doxygen utility, documentation can be generated by using:
# Run documentation generation
doxygen ./Doxyfile
# Under Windows OS, maybe doxygen is not added to the $PATH
"C:\Program Files\doxygen\bin\doxygen.exe" ./Doxyfile
Note: You can also load the Doxyfile into Doxywizard (Doxygen GUI) and run generation.
This library is licensed under MIT license.