一个追加写日至的类

//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(&ltime);
    strTime = ctime(&ltime);
}

 

 

int main()

{

string strErrorMessage = "  在制作过程中创建basebokinfo.xml出错";
      CErrorLog::WriteStrToLog("c://MakeDigibookError.log", strErrorMessage);

return 1;

}
      

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值