Simple Logger

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:

  1. Fatal
  2. Error
  3. Warn
  4. Info
  5. Debug

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:

  • print message in printf style
  • assertion, which will cause the program to exit in debug model (with -DDEBUG) with your message printed, and defined to nothing otherwise.
  • confirmation, similar to assertion, but will not exit if failed
  • dump variables values and the statement.

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

Download

simplelogger/overview.txt · Last modified: 2009/12/01 15:03 by edwardgao
CC Attribution-Noncommercial-Share Alike 3.0 Unported www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0