一個小巧的C++Log輸出到文件類 (轉)


 

http://blog.csdn.net/dpsying/article/details/17122739

有時候需要輸出一些程序運行的信息,供我們不需要調試就可以直接查看程序運行狀態。所以我們需要在程序中加入一些LOG輸出。

適合涉及到虛擬機調試一些關於驅動等的程序時,或進行遠程調試時。


搜了些log工具,不夠輕……還是簡單實現下吧

貼上來,可能有用的上:

Log.h

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



使用:

直接在需要輸出日志的地方使用宏LOG(text)就可以了,記得包含頭文件Log.h。

  1. #include "Log.h"  

 

    1. BOOL   
    2.   
    3.   
    4.   
    5. int float BOOL enum )  
    6. return  
    7. 效果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM