编程语言

img icecools

实现一个基于xml的log类

发表于2004/10/14 22:37:00  784人阅读

分类: 技术

很多程序都有个log功能,可以把运行时的一些信息存到一个文件中,这样可以在Release的时候查看信息,也可以帮助调试,下面这个类接受一个文件名为参数,可以将信息生成为xml文件,然后借助xsl文件直接可以看到html格式的信息
applog.h

#include <iostream>
#include <fstream>
#include <string>

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;
};

实现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"
                  "<LogInfo>/n/t<AppName>AppName</AppName>/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<Message>/r/n";
    
    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<time>";
    msgTime += sTime;
    os<<msgHead<<msgTime; 
    msgTime = "</time>/r/n/t/t<content>";
    os<<msgTime;
    return;
}
void CAppLog::endXMLRecord(ofstream& os)
{    
    string msgEnd = "</content>/r/n/t</Message>/r/n";
    os<<msgEnd;
}

使用的时候如下:
CAppLog appLog("c://log.xml");
char* s = "hello";
int n = 100;
appLog.LogMessage("My log info %d %s", n, s);


将这个log.xsl文件放在同一目录下
<?xml version="1.0" encoding="GB2312"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">

<html>
 <head>
  <title><xsl:value-of select="LogFile/AppName" /> ÈÕÖ¾Îļþ</title>
 </head>
 <body>
  <h1><center><xsl:value-of select="LogFile/AppName" /> ÔËÐÐÈÕÖ¾Îļþ</center></h1>

  <table border="0" cellspacing="1" cellpadding="0">
   <tr>
    <td bgcolor="#dddddd">
     <table border="1" cellpadding="3" cellspacing="1">

      <xsl:for-each select="LogInfo/Message">
       <tr onMouseover="">
        <td bgcolor="#ffffff" valign="top"><xsl:number count="Message" />.</td>
        <td bgcolor="#ffffff" valign="top"><nobr><xsl:value-of select="time" /></nobr><xsl:value-of select="Time" /></td>
        <td bgcolor="#ffffff" valign="top"><xsl:value-of select="content" /></td>
       </tr>
      </xsl:for-each>
     </table>
    </td>
   </tr>
  </table>

 </body>
</html>

</xsl:template>
</xsl:stylesheet>

阅读全文
0 0

相关文章推荐

img
取 消
img