Written by Markus Brueckner <dev@slash-me.net>
This code is public domain. Do whatever you want with it. I'm certainly not the one to blame for any damage this code may do. Though I've tried hard not to screw this up, you should probably not use this code in mission critical environments unless you really know what you're doing.
That said let's start the technical things. This header file is intended for use as a lightweight, yet flexible logging and tracing frontend. The objectives to be reached were:
The typical usage of the logger looks like this (error checking left aside):
Logging::Logger *logger = new Logging::Logger(std::cout, Logging::LEVEL_TRACE);
logger << Logging::LEVEL_DEBUG << "This is a debug message." << Logging::endl; logger << Logging::LEVEL_INFO << "This is a info message." << Logging::endl;
delete logger;
This initializes the logger to output messages with at least trace level to std::cout. In case of the debug version of the code, both messages will therefore be print. The release version will remove the first message. The logger is able to print each and every datatype the user defined a std::ostream-output-operator for. Logging::Logger actually is just a typedef for Logging::TLogStream<EmptyMutex> which implements a logger with no thread safety at all. To implement actual thread safety, you'd have to write a class offering the two methods void Lock() and Unlock() and use it as a template parameter for TLogStream. An object of this class will be instantiated when the logger is created and destroyed when the logger is deleted. The logger will call Lock() each time, a message starts and Unlock() when the message ends. The two methods must block until the underlying synchronisation object is aquired or released.
The inclusion of debug and trace messages in the resulting binary is governed by the standard preprocessor macro NDEBUG. Whenever this is defined, we're compiling a release version and need to remove the according messages. If this macro is undefined, we're compiling debug code. The actual removal of the code is done by an optimizing compiler step, eliminating empty method stubs. To make this work, you should compile release code with some optimization level set. In case of the gcc the parameter -O is sufficient.
1.4.4