一个小巧的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. 效果:
posted on 2014-12-03 10:43  3D入魔  阅读(11976)  评论(0编辑  收藏  举报