log文件的读写[转]
来源:互联网 发布:易语言互斥体多开源码 编辑:程序博客网 时间:2024/06/05 07:28
/** * 用于输出log文件的类. */ #ifndef LOG_H #define LOG_H //log文件路径 #define LOG_FILE_NAME "log.txt" //启用开关 #define LOG_ENABLE #include <fstream> #include <string> #include <ctime> using namespace std; class CLog { public: static void GetLogFilePath(CHAR* szPath) { GetModuleFileNameA( NULL, szPath, MAX_PATH ) ; ZeroMemory(strrchr(szPath,_T('\\')), strlen(strrchr(szPath,_T('\\') ) )*sizeof(CHAR)) ; strcat(szPath,"\\"); strcat(szPath,LOG_FILE_NAME); } //输出一个内容,可以是字符串(ascii)、整数、浮点数、布尔、枚举 //格式为:[2011-11-11 11:11:11] aaaaaaa并换行 template <class T> static void WriteLog(T x) { CHAR szPath[MAX_PATH] = {0}; GetLogFilePath(szPath); ofstream fout(szPath,ios::app); fout.seekp(ios::end); fout << GetSystemTime() << x <<endl; fout.close(); } //输出2个内容,以等号连接。一般用于前面是一个变量的描述字符串,后面接这个变量的值 template<class T1,class T2> static void WriteLog2(T1 x1,T2 x2) { CHAR szPath[MAX_PATH] = {0}; GetLogFilePath(szPath); ofstream fout(szPath,ios::app); fout.seekp(ios::end); fout << GetSystemTime() << x1 <<" = "<<x2<<endl; fout.close(); } //输出一行当前函数开始的标志,宏传入__FUNCTION__ template <class T> static void WriteFuncBegin(T x) { CHAR szPath[MAX_PATH] = {0}; GetLogFilePath(szPath); ofstream fout(szPath,ios::app); fout.seekp(ios::end); fout << GetSystemTime() << " --------------------"<<x<<" Begin--------------------" <<endl; fout.close(); } //输出一行当前函数结束的标志,宏传入__FUNCTION__ template <class T> static void WriteFuncEnd(T x) { CHAR szPath[MAX_PATH] = {0}; GetLogFilePath(szPath); ofstream fout(szPath,ios::app); fout.seekp(ios::end); fout << GetSystemTime() << "--------------------"<<x<<" End --------------------" <<endl; fout.close(); } private: //获取本地时间,格式如"[2011-11-11 11:11:11] "; static string GetSystemTime() { time_t tNowTime; time(&tNowTime); tm* tLocalTime = localtime(&tNowTime); char szTime[30] = {'\0'}; strftime(szTime, 30, "[%Y-%m-%d %H:%M:%S] ", tLocalTime); string strTime = szTime; return strTime; } }; #ifdef LOG_ENABLE //用下面这些宏来使用本文件 #define LOG(x) CLog::WriteLog(x); //括号内可以是字符串(ascii)、整数、浮点数、bool等 #define LOG2(x1,x2) CLog::WriteLog2(x1,x2); #define LOG_FUNC LOG(__FUNCTION__) //输出当前所在函数名 #define LOG_LINE LOG(__LINE__) //输出当前行号 #define LOG_FUNC_BEGIN CLog::WriteFuncBegin(__FUNCTION__); //形式如:[时间]"------------FuncName Begin------------" #define LOG_FUNC_END CLog::WriteFuncEnd(__FUNCTION__); //形式如:[时间]"------------FuncName End------------" #else #define LOG(x) #define LOG2(x1,x2) #define LOG_FUNC #define LOG_LINE #define LOG_FUNC_BEGIN #define LOG_FUNC_END #endif #endif
阅读全文
0 0
- log文件的读写[转]
- [Python]读写文件,处理log
- 2011-2 写log(涉及读写文件)
- tcltk实时读写文件【创建log-debug】
- build.prop的读写 && Android平台LOG输出规范 && Log写入文件 && 将异常(getStackTrace)转化成String
- build.prop的读写 && Android平台LOG输出规范 && Log写入文件 && 将异常(getStackTrace)转化成String
- 在Java中简单的读写log或者txt文件内容
- MFC 在一个项目中添加log文件的读写操作问题
- 【Log】文件的生成
- 串口的读写,文件读写
- 利用bin-log文件特点配置读写分离
- 转:磁盘文件的正常读写与异步读写
- 读写ini文件的类(转)
- (转)读写ini文件的方法
- 简单的XML文件读写 (转)
- INI文件的读写
- 读写文件的例子
- INI文件的读写
- 微信支付服务商星火计划政策报名
- 手风琴相册
- session和Cookies的区别
- 手机端html5触屏事件(touch事件)
- eclipse中debug的快捷键
- log文件的读写[转]
- directui界面库 Duilib是开源的
- cfA.Chess Tourney
- CSS中position绝对定位和相对定位
- Gym 100917FFind the Length
- Spring AOP底层原理
- Primer (十三)
- BSD协议
- sql之连接