Many programs have a log feature, you can save some information at runtime to a file, so you can view the information at Release, you can also help debug, the following class accepts a file name as a parameter, you can make information XML file, then you can see the information of the HTML format with the XSL file AppLog.h
#include
#include
#include
Using namespace std;
Class capplog
{
PUBLIC:
CAPPLOG (Char * filename);
Bool LogMessage (char * msg, ...);
Virtual ~ CAPPLOG ();
Private:
Void Writexmlhead ();
Void WriteXMlend ();
Void BeginxmlRecord (OFStream & OS);
Void EndxmlRecord (OFSTream & OS);
String m_sfilename;
}
Implement AppLog.cpp
Capplog :: CAPPLOG (Char * filename)
{
IF (filename)
This-> m_sfilename = filename;
Else
m_sfilename = "app.xml";
Writexmlhead ();
}
Capplog :: ~ CAPPLOG ()
{
Writexmlend ();
}
Bool capplog :: LogMessage (char * msg, ...)
{
OFSTREAM OS (m_sfilename.c_str (), iOS_BASE :: App | iOS_BASE :: OUT;
IF (OS)
{
BeginxmlRecord (OS);
VA_LIST Argp;
VA_START (Argp, MSG);
Char sztmp [1024];
vSprintf (SZTMP, MSG, Argp);
VA_END (ARGP);
OS << SZTMP;
ENDXMLRECORD (OS);
Os.Close ();
Return True;
}
Return False;
}
Void Capplog :: Writexmlhead ()
{
String head = " XML Version = /" 1.0 / "encoding = /" GB2312 / "?> / n"
" xml-stylesheet href = /" log.xsl / "type = /" text / xsl / "?> / n"
OFSTREAM OS (m_sfilename.c_str (), iOS_BASE :: App | iOS_BASE :: OUT;
IF (OS)
{
OS << Head << Endl;
Os.Close ();
}
}
Void Capplog :: WriteXmlend ()
{
String end = " logInfo>";
OFSTREAM OS (m_sfilename.c_str (), iOS_BASE :: App | iOS_BASE :: OUT;
IF (OS)
{
OS << End << Endl;
Os.Close ();
}
}
Void Capplog :: BeginxmlRecord (OFStream & OS) {
String msghead = "/ t
Time_t now;
Now = Time (null);
String Stime = CTIME (& now);
INT n = stime.find ('/ n');
IF (n> 0)
{
Stime [n] = '/ 0';
}
String msgtime = "/ t / t
Msgtime = STIME;
OS << msghead << msgtime;
Msgtime = " TIME> / R / N / T / T
OS << msgtime;
Return;
}
Void Capplog :: EndxmlRecord (OFStream & OS)
{
String msgend = " content> / r / n / t message> / r / n";
OS << msgend;
}
When using:
CAPPLOG Applog ("C: //log.xml");
Char * s = "hello";
INT n = 100;
Applog.LogMessage ("My Log Info% D% S", N, S);
Place this log.xsl file in the same directory XML Version = "1.0" Encoding = "GB2312"?>
|