Almost everybody needs sometimes something like a logging class. Me too. That is why I wrote such a class to do my logging although there are already some very convenient logging classes like CProtocol
by jazper. But I thought of some general approach: Logging with XML.
The probably most interesting point is the possibility to log into a structure. Now you can log in a fairly natural way: simply go down one level if you call a function and after that go up and you will get a log structure that resembles your program's calling tree. All log entries will automatically get the right place.
Using LogXML is not very difficult. First of all:
<PRE lang=c++> #include "LogXML.h"
Now you can instantiate LogXML by:
<PRE lang=c++> LogXML* log = new LogXML("LogFileName.xml", "ApplicationName");
There are several ways to write a Log Entry, some kind of Log Record can be written by:
<PRE lang=c++> log->Log("Error: Something is not correct. Please Fix.", // Entry
__FILE__, // Filename of Source File<
"myLoggingFunction()"); // Function Name
Or you can write only one line by:
<PRE lang=c++> log->WriteLine("TagName", "This is your Log Entry");
The output (log file) of these examples is:
<Entry>Log File opened</Entry>
<Entry>Error: Something is not correct. Please Fix.</Entry>
<TagName>This is your Log Entry</TagName>
<Entry>Log File closed</Entry>
To use such a log file you can use a XML parser like
or everything else you want. To convert a file produced by LogXML to something human-readable you can use for example XSL. Since my knowledge of XSL is not the best, I only managed to write a file named Log.XSL
that shows Log Records of Level 1, i.e. do not use
and use only
to write entries if you want to use it. And this is our sample Log File shown by Internet Explorer (6.0):
- May 12, 2003 - First Release 1.0.0