//CDigiBookLog.h文件
/*********************
写日至的类
以追加的方式记录信息的
created by bzero.lee 20050611
MSN:navyblue1982@hotmail.com
*********************/
#if !defined(MY_CDIGIBOOKLOG_H)
#define MY_CDIGIBOOKLOG_H
#include <fstream>
#include<string>
using namespace std;
class CErrorLog
{
public:
static bool WriteStrToLog(const string& strLogFile, const string& strContents);
private:
static bool OpenLogFile(std::fstream& fLog, const string& strLogFile);
static bool WriteErrorLog(std::fstream& fLog, const string& strErrorMessage);
static bool CloseLogFile(std::fstream& fLog);
static void GetCurrentStrTime(string& strTime);//传回strTime时间
};
#endif //MY_CDIGIBOOKLOG_H
///===============================================
//CDigiBookLog.cpp文件
#include "StdAfx.h"
#include "CDigiBookLog.h"
bool CErrorLog::WriteStrToLog(const string& strLogFile, const string& strContents)
{
string strTime;
CErrorLog::GetCurrentStrTime(strTime);
string strErrorMessage = strTime + strContents;
std::fstream fLog;
if (false == CErrorLog::OpenLogFile(fLog,strLogFile))
return false;
CErrorLog::WriteErrorLog(fLog, strErrorMessage);
if(false == CErrorLog::CloseLogFile(fLog))
return false;
return true;
}
//写入日至信息
bool CErrorLog::WriteErrorLog(std::fstream& fLog,const string& strErrorMessage)
{
fLog << strErrorMessage.c_str() <<endl;
return true;
}
//追加方式打开日至文件
bool CErrorLog::OpenLogFile(std::fstream& fLog, const string& strLogFile)
{
fLog.open(strLogFile.c_str(), ios::out|ios::app);
if(!fLog)
return false;
return true;
}
//关闭文件
bool CErrorLog::CloseLogFile(std::fstream& fLog)
{
fLog.close();
return true;
}
//取得当前的时间并转换为字符串
//strTime为返回当前时间的字符串
void CErrorLog::GetCurrentStrTime(string& strTime)
{
time_t ltime;
time(<ime);
strTime = ctime(<ime);
}
int main()
{
string strErrorMessage = " 在制作过程中创建basebokinfo.xml出错";
CErrorLog::WriteStrToLog("c://MakeDigibookError.log", strErrorMessage);
return 1;
}