log工具

  1. /**  
  2.  * 用于输出log文件的类.  
  3.  */    
  4.   
  5.   
  6. #ifndef LOG_H    
  7. #define LOG_H    
  8.   
  9.   
  10. //log文件路径  
  11. #define LOG_FILE_NAME "log.txt"  
  12.   
  13. //启用开关  
  14. #define LOG_ENABLE  
  15.     
  16. #include <fstream>    
  17. #include <string>    
  18. #include <ctime>    
  19.     
  20. using namespace std;    
  21.     
  22. class CLog    
  23. {    
  24. public:    
  25.     static void GetLogFilePath(CHAR* szPath)  
  26.     {  
  27.         GetModuleFileNameA( NULL, szPath, MAX_PATH ) ;  
  28.         ZeroMemory(strrchr(szPath,_T('\\')), strlen(strrchr(szPath,_T('\\') ) )*sizeof(CHAR)) ;  
  29.         strcat(szPath,"\\");  
  30.         strcat(szPath,LOG_FILE_NAME);  
  31.     }  
  32.     //输出一个内容,可以是字符串(ascii)、整数、浮点数、布尔、枚举  
  33.     //格式为:[2011-11-11 11:11:11] aaaaaaa并换行  
  34.     template <class T>  
  35.     static void WriteLog(T x)  
  36.     {  
  37.         CHAR szPath[MAX_PATH] = {0};  
  38.         GetLogFilePath(szPath);  
  39.   
  40.         ofstream fout(szPath,ios::app);  
  41.         fout.seekp(ios::end);  
  42.         fout << GetSystemTime() << x <<endl;  
  43.         fout.close();  
  44.     }  
  45.   
  46.     //输出2个内容,以等号连接。一般用于前面是一个变量的描述字符串,后面接这个变量的值  
  47.     template<class T1,class T2>   
  48.     static void WriteLog2(T1 x1,T2 x2)  
  49.     {  
  50.         CHAR szPath[MAX_PATH] = {0};  
  51.         GetLogFilePath(szPath);  
  52.         ofstream fout(szPath,ios::app);  
  53.         fout.seekp(ios::end);  
  54.         fout << GetSystemTime() << x1 <<" = "<<x2<<endl;  
  55.         fout.close();  
  56.     }  
  57.   
  58.     //输出一行当前函数开始的标志,宏传入__FUNCTION__  
  59.     template <class T>  
  60.     static void WriteFuncBegin(T x)  
  61.     {  
  62.         CHAR szPath[MAX_PATH] = {0};  
  63.         GetLogFilePath(szPath);  
  64.         ofstream fout(szPath,ios::app);  
  65.         fout.seekp(ios::end);  
  66.         fout << GetSystemTime() << "    --------------------"<<x<<"  Begin--------------------" <<endl;  
  67.         fout.close();  
  68.     }  
  69.   
  70.     //输出一行当前函数结束的标志,宏传入__FUNCTION__  
  71.     template <class T>  
  72.     static void WriteFuncEnd(T x)  
  73.     {  
  74.         CHAR szPath[MAX_PATH] = {0};  
  75.         GetLogFilePath(szPath);  
  76.         ofstream fout(szPath,ios::app);  
  77.         fout.seekp(ios::end);  
  78.         fout << GetSystemTime() << "--------------------"<<x<<"  End  --------------------" <<endl;  
  79.         fout.close();  
  80.     }  
  81.       
  82.       
  83. private:  
  84.     //获取本地时间,格式如"[2011-11-11 11:11:11] ";   
  85.     static string GetSystemTime()    
  86.     {    
  87.         time_t tNowTime;    
  88.         time(&tNowTime);    
  89.         tm* tLocalTime = localtime(&tNowTime);    
  90.         char szTime[30] = {'\0'};    
  91.         strftime(szTime, 30, "[%Y-%m-%d %H:%M:%S] ", tLocalTime);    
  92.         string strTime = szTime;    
  93.         return strTime;    
  94.     }    
  95.   
  96. };    
  97.   
  98. #ifdef LOG_ENABLE  
  99.   
  100. //用下面这些宏来使用本文件  
  101. #define LOG(x)              CLog::WriteLog(x);          //括号内可以是字符串(ascii)、整数、浮点数、bool等  
  102. #define LOG2(x1,x2)     CLog::WriteLog2(x1,x2);  
  103. #define LOG_FUNC        LOG(__FUNCTION__)               //输出当前所在函数名  
  104. #define LOG_LINE        LOG(__LINE__)                       //输出当前行号  
  105. #define LOG_FUNC_BEGIN  CLog::WriteFuncBegin(__FUNCTION__);     //形式如:[时间]"------------FuncName  Begin------------"  
  106. #define LOG_FUNC_END     CLog::WriteFuncEnd(__FUNCTION__);      //形式如:[时间]"------------FuncName  End------------"  
  107.   
  108. #else  
  109.   
  110. #define LOG(x)                
  111. #define LOG2(x1,x2)       
  112. #define LOG_FUNC          
  113. #define LOG_LINE          
  114. #define LOG_FUNC_BEGIN    
  115. #define LOG_FUNC_END      
  116.   
  117. #endif  
  118.   
  119. #endif    

猜你喜欢

转载自www.cnblogs.com/chenzishun/p/10103005.html