Logger is a general purpose logging system for events, simulation, errors, etc.
More...
#include <cstdio>
#include <cstdarg>
#include <string>
Go to the source code of this file.
Detailed Description
Logger is a general purpose logging system for events, simulation, errors, etc.
It provides a standard formated output to allow for easy parsing (for automation). It also allows the for the filtering of log entries (by priority) and the easy redirection of where the output goes (file, stdout, stderr). Logger also provides a segmentation feature which will auto-split the logs if the files grow beyond a specified size.
The output format is:
YYYY-MM-DD_HH:MM:SS.mmm Keyword: Subkeyword: PRI=n
User text goes here.
Could be multiple lines.
END:
Sample Usage:
Logger logger;
logger.Init("myoutput.log");
logger.LogEvent(5, "Error", "SDL_image::SDL_glLoadImage",
"Error found: %s", SDL_GetError());
logger.Close();
Sample Output:
2002-10-29_19:59:32.321 Error: SDL_image::SDL_glLoadImage(): PRI=5
Error found: Error in SDL_GLhelper. Unsupported bit depth(=2)
END:
- Note:
- This is a C++ adaptation of a version of my logging system written in Perl. It was then ported again to C and added basic thread safety. Those changes have been backported to this library along with changes to conform to our new coding and documentation guidelines. This version continues to use standard C streams instead of C++ streams because of performance and portability reasons as well as laziness issues. I'm actually unsure what a C++ stream interface would like like given that the different types of parameters (keywords, text) would effectively require the >> operator to be aware of a state machine. This version attempts to provide (very) basic thread safety if the define's are enabled. It will lock logger member variables when accessed and lock the output file when writing to. The user must still ensure that the strings and other data passed into logger are properly locked. There is also currently no safety for creating and/or destroying instances of Logger while trying to use it, so don't create or destroy when there is possible contention. Further optimization probably could be achieved by providing finer grain locks and smarter locking procedures.
- Warning:
- Logger internal error reporting sucks (which is somewhat ironic). I currently either silently fail or print directly to stderr. I might want to do a string based method, but would need to make it thread safe. This means I would need to create a map that placed a different error message in a different thread. Of course, that requires more work as the thread APIs are not universal. Perhaps returning an error number with a system that converts the error number would be easier, but gets hard to deal with when return values are already being used.
- Author:
- Eric Wing
Variable Documentation
A global instance pointer of logger.
Allow for a global instance of Logger so everybody can use simply by including include "Logger.hpp" and defining in the global scope somewhere (like before main()). Don't forget to call Create to actually create the instance.