The LogLib library project is a collection of libraries and programs that can be used to add log file functionality to a program and offer tools to use and analyze the log files.
At the core of this is the LogFiles library, a C++ library that provides a class that are used to maintain a log file for a program. The class will manage the log file internally. The class supports different log levels that can be changed on the fly.
There are tools planned that can be used to view and analyze the log file easily, as well as tools to manage the log files on a computer, to ensure that disks pace is not used up and archives are kept.
The class allows you to specify a base name for the log file, the path to where the log file should be created, the log level to use, whether a new file should be created at midnight and whether an existing file should be replaced or appended to.
The file name is then made up of the base name with a date-stamp appended to it and a .log extension, like so:
[base name]_[YYYYMMDD].log
E.g.: testlog_20021226.log
By default, a new log file will be created the first time a message is written to the log after midnight.
Log levels
The following log severities can be used:
CRITICAL
ERROR
WARNING
INFO
TRACE
DEBUG
COMMS
The log level that is specified for the log file is a combination of these severities. When a message is written to the log, it is assigned one of these log severities. Only messages with a severity that is part of the log file's log level combination is written.
This enables you to only log messages of certain levels. The log file's log level can be changed to a different combination, so that other messages will be written.
For example:
You would normally only want to see messages that is Critical, Error and Warning. Then you set the log level to CRITICAL || ERROR || WARNING.
Then, for instance, your program is not behaving as expected or it is processing data wrong. Now you want to see the Trace and Debug messages too. Just change the log file's log level to include TRACE and DEBUG too. Then you will start seeing your Debug and Trace messages as well.
Log File Format
Messages are written to the log file with a time stamp and it's severity, like so:
[timestamp] [severity] : [message]
E.g.:
10:12:34 DEBUG : This is a test message
Logging Messages
To use the log file, you include the library in your C++ project and instantiate the LogFiles class. Call the Open function to open the log file and the Close function to close the file. The Open and Close functions can be used to enable or disable logging in your program on the fly. When the log file is closed, the Write functions will just be ignored.
Use the Write function to write messages to the log file. The Write function takes 3 parameters:
unsigned int msglevel
- The message severity
char *outStr
- The message format or text (same as printf)
...
- The message parameters (same as printf)
E.g.:
int i = 0;
char theStr[] = "The String";
myLogFile->Write( MSG_DEBUG, "This is test no %d, and string is: %s", i, theStr );
The message in the log file will look like this:
15:44:45 DEBUG : This is test no 0, and string is: The String