|
[工作日志]log file class |
//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); }
}
|
|
|
|

|
.: 公告
|
|
| « | Mar.2026 | » | | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | | | | | |
|
.: 我的分类(专题)
|
|

.: 最新日志
.: 最新回复
|
|

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

.: 留言板
|

.: 链接
|

|