Logging

by Stephan Pareigis

Logging

by Stephan Pareigis

This examples shows, how loggers are usally written. There are a lot of loggers to download for your project. Boost also has a logger.

Look at the #defines in lines 6 through 14. Here the global logging level is used to decide if output into the logfile should be made. Also the access to the logger - which is a Meyers Singleton - is made much easier for the application programmer, since it hides the toruous call the singleton instance.

Line 16 lets you set the global logging level easily. Line 17 lets you use the scoped logger, which makes output into your logfile when entering and exiting a function. The preprocessor FUNCTION is used, to indicate the function name which is being entered and exited.

In line 21 three levels of output verbosity are defined. You may define your own levels if you wish.

The Logger Class contains an ofstream logfile_ for output into a file. The member function log first prints a time stamp, the output level, and afterwards a custom message which may be defined by the application programmer.

The LogScope Class uses the scoped principle. It creates output in constructor and destructor. All that has to be done is, an object of type LogScope has to be created as the very first command in every function. See the respective preprocessor macros for this purpose.

Related