本站首页    管理页面    写新日志    退出

The Neurotic Fishbowl

[工作日志]log file class
莹莹 发表于 2006/8/2 10:40:12

//CLog.h #include <string> using namespace std; //日志文件类//用来把系统运行过程中的状态信息保存在文件中以方便用户跟踪的类class CLog{public: CLog();//默认情况下日志的保存地点为当前目录,日志名称info.log  CLog(const string dir);//用户自定义日志文件的保存目录及文件名  void log(const string title, const string desc);//用来向日志文件输出信息的成员函数     void log(const string &text);  CLog &operator << ( const char * text);     CLog &operator << ( const string &text);     CLog &operator << ( const int value);       CLog &operator << ( const unsigned int value);       CLog &operator << ( const float value);      CLog &operator << ( const double value);private:    string m_LogDir;    string m_FullName;    bool m_IsStdout;    bool m_IsFileOk;      const string getTimeStr(const bool isFileName = false); }; //CLog.cpp #include <time.h>#include "Clog.h" #define TIMESTAMP(t) ("[" + (t) + "]: ") CLog::CLog(){ m_FullName = "./info.log";    m_IsFileOk=true;    m_IsStdout=false;    FILE* fi;    fi = fopen(m_FullName.c_str(), "a");}CLog::CLog(const string dir){ m_FullName=dir;    m_IsFileOk=true;    m_IsStdout=false;    FILE* fi;    fi = fopen(m_FullName.c_str(), "w");} const string CLog::getTimeStr(const bool isFileName){  time_t now;  struct tm * ti; // timeinfo  now = time(NULL);  ti = localtime(&now);    char buf[200];  /// the string in format "YYYY-MM-dd_hh-mm-ss"  sprintf(buf, isFileName?("%d-%02d-%02d_%02d-%02d-%02d"):("%d-%02d-%02d %02d:%02d:%02d"),                        ti->tm_year + 1900,                        ti->tm_mon + 1,                       ti->tm_mday,                       ti->tm_hour,                       ti->tm_min,                        ti->tm_sec );  return string(buf);}void CLog::log(const string title, const string desc){   log(title + " -- " + desc); }void CLog::log(const string &text){  if( m_FullName != "" && !m_IsFileOk ) return;  (*this) << text;}CLog &CLog::operator << ( const char *text){  if( m_FullName != "")  {      if(!m_IsFileOk ) return *this;      FILE* fi;      fi = fopen(m_FullName.c_str(), "a");      if (fi != NULL)    {        char buf[500];        strcpy(buf, TIMESTAMP(getTimeStr()).c_str());        strcat(buf, text);        strcat(buf, "\n");        fwrite(buf, sizeof(char), strlen(buf), fi);        fclose(fi);      }  }  else   {    fprintf(m_IsStdout?stdout:stderr, "%s%s\n", TIMESTAMP(getTimeStr()).c_str(), text);  }  return *this;}CLog &CLog::operator << ( const string &text){  return (*this)<< text.c_str();}CLog &CLog::operator << ( const int value){  char tmp[100];  sprintf(tmp, "%d", value);  return (*this)<< tmp;}CLog &CLog::operator << ( const unsigned int value){  char tmp[100];  sprintf(tmp, "%d", value);  return (*this)<< tmp;}CLog &CLog::operator << ( const float value){  char tmp[100];  sprintf(tmp, "%-f", value);  return (*this)<< tmp;}CLog &CLog::operator << ( const double value){  char tmp[100];  sprintf(tmp, "%-f", value);  return (*this)<< tmp;} //test.cpp #include "Clog.h"#include <iostream> int main(){ CLog lg; const string m="c:\\book\\test.cpp"; const string n="connect error"; for(int i=0;i<10;i++) {  lg.log(m,n);  }  cin >> i; for (int j=0;j<i;j++) {  lg.log(m,n); } }

阅读全文(1237) | 回复(0) | 编辑 | 精华

 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)

 
 



The Neurotic Fishbowl

.: 公告

暂无公告...


Bloginess

«Mar.2026»
1234567
891011121314
15161718192021
22232425262728
293031

.: 我的分类(专题)

首页(49)
我的相册(9)
工作日志(2)
爱的痕迹(3)
好文转载(27)
成长记录(3)
我的音乐(0)
news(3)
综合娱乐(2)


In the Bowl

.: 最新日志

大起大落
他的来信
伤逝
wi_fi
不想说毕业
微微疼痛
100 Best Places to W
ini file class
log file class
类的继承与组合


.: 最新回复

回复:构造函数与析构函数
回复:C++从零开始之结构(9)


The Fishkeeper
blog名称:Natural Pink
日志总数:49
评论数量:12
留言数量:0
访问次数:121268
建立时间:2006年7月25日



Text Me

.: 留言板

签写新留言


Other Fish in the Sea

.: 链接


喜欢的博客:

http://nfox.spaces.live.com/
http://gaowei.cnblogs.com/

http://wang2fan2.spaces.live.com/

http://www.itepub.com/html/xiazaizhongxin/index.html




站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.031 second(s), page refreshed 144819521 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号