Simple Logger is a very simple logger I used in my C++ programs, it contains only one header and one cpp file. The logger, similar to other logging systems, organize the information in five levels:
And information of the five levels can be piped into different files.
In addition to these five levels, Info and Debug messages can have a “Verbose level” specified, which make it easier to turn them on and off.
Multiple macros are defined to make formatting logging information easy, you can:
Here is the simple usage of the tool, first you need to add SimpleLogger.cpp to your project, and then include SimpleLogger.h. In main program, before calling any logging functions, you call:
init_logging(0);
which will direct all logging level to stderr. You can pipe different level to different file, please refer to the comments inside the code.
And below is sample program that calls the logging functions:
Filename: testLogging.cxx
#include "SimpleLogger.h" int main(int argc, const char* argv[]){ init_logging(0); int i=0; disable_logger_debug; // If you call this, debug information will // be disabled, similarly there are // disable_logger_info etc P_FATAL("Fatal!%s","abcdefg"); P_ERROR("Error!%s","abcdefg"); P_WARN("Warn!%s","abcdefg"); P_INFO("Info!%s","abcdefg"); P_DEBUG("Debug!%s","abcdefg"); D_INFO("%d",++i); D_INFO("%d",++i); D_FATAL("%d",++i); D_DEBUG("%d",++i); D_FATAL("%d",++i); A_FATAL(++i==0); A_FATAL(++i!=0); return 0; }
Running the code above will give you :
[Fatal][09-12-01 16:52:50]testLogging.cxx:30(main): Fatal!abcdefg [Error][09-12-01 16:52:50]testLogging.cxx:31(main): Error!abcdefg [Warn ][09-12-01 16:52:50]testLogging.cxx:32(main): Warn!abcdefg [Info ][09-12-01 16:52:50]testLogging.cxx:33(main): Info!abcdefg [Dump(Info )][09-12-01 16:52:50]testLogging.cxx:35(main):"++i" ==> 1 [Dump(Info )][09-12-01 16:52:50]testLogging.cxx:36(main):"++i" ==> 2 [Dump(Fatal)][09-12-01 16:52:50]testLogging.cxx:37(main):"++i" ==> 3 [Dump(Fatal)][09-12-01 16:52:50]testLogging.cxx:39(main):"++i" ==> 4