有时候需要输出一些程序运行的信息,供我们不需要调试就可以直接查看程序运行状态。所以我们需要在程序中加入一些LOG输出。

适合涉及到虚拟机调试一些关于驱动等的程序时,或进行远程调试时。


搜了些log工具,不够轻……还是简单实现下吧

贴上来,可能有用的上:

Log.h

​​


  1. /**  
  2.  * 用于输出log文件的类.  
  3.  */    


  4. #ifndef LOG_H    
  5. #define LOG_H    


  6. //log文件路径  
  7. #define LOG_FILE_NAME "log.txt"  

  8. //启用开关  
  9. #define LOG_ENABLE  

  10. #include <fstream>    
  11. #include <string>    
  12. #include <ctime>    

  13. using namespace std;    

  14. class CLog    
  15. {    
  16. public:    
  17.     static void GetLogFilePath(CHAR* szPath)  
  18.     {  
  19.         GetModuleFileNameA( NULL, szPath, MAX_PATH ) ;  
  20.         ZeroMemory(strrchr(szPath,_T('\\')), strlen(strrchr(szPath,_T('\\') ) )*sizeof(CHAR)) ;  
  21.         strcat(szPath,"\\");  
  22.         strcat(szPath,LOG_FILE_NAME);  
  23.     }  
  24.     //输出一个内容,可以是字符串(ascii)、整数、浮点数、布尔、枚举  
  25.     //格式为:[2011-11-11 11:11:11] aaaaaaa并换行  
  26.     template <class T>  
  27.     static void WriteLog(T x)  
  28.     {  
  29.         CHAR szPath[MAX_PATH] = {0};  
  30.         GetLogFilePath(szPath);  

  31.         ofstream fout(szPath,ios::app);  
  32.         fout.seekp(ios::end);  
  33.         fout << GetSystemTime() << x <<endl;  
  34.         fout.close();  
  35.     }  

  36.     //输出2个内容,以等号连接。一般用于前面是一个变量的描述字符串,后面接这个变量的值  
  37.     template<class T1,class T2>   
  38.     static void WriteLog2(T1 x1,T2 x2)  
  39.     {  
  40.         CHAR szPath[MAX_PATH] = {0};  
  41.         GetLogFilePath(szPath);  
  42.         ofstream fout(szPath,ios::app);  
  43.         fout.seekp(ios::end);  
  44.         fout << GetSystemTime() << x1 <<" = "<<x2<<endl;  
  45.         fout.close();  
  46.     }  

  47.     //输出一行当前函数开始的标志,宏传入__FUNCTION__  
  48.     template <class T>  
  49.     static void WriteFuncBegin(T x)  
  50.     {  
  51.         CHAR szPath[MAX_PATH] = {0};  
  52.         GetLogFilePath(szPath);  
  53.         ofstream fout(szPath,ios::app);  
  54.         fout.seekp(ios::end);  
  55.         fout << GetSystemTime() << "    --------------------"<<x<<"  Begin--------------------" <<endl;  
  56.         fout.close();  
  57.     }  

  58.     //输出一行当前函数结束的标志,宏传入__FUNCTION__  
  59.     template <class T>  
  60.     static void WriteFuncEnd(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<<"  End  --------------------" <<endl;  
  67.         fout.close();  
  68.     }  


  69. private:  
  70.     //获取本地时间,格式如"[2011-11-11 11:11:11] ";   
  71.     static string GetSystemTime()    
  72.     {    
  73.         time_t tNowTime;    
  74.         time(&tNowTime);    
  75.         tm* tLocalTime = localtime(&tNowTime);    
  76.         char szTime[30] = {'\0'};    
  77.         strftime(szTime, 30, "[%Y-%m-%d %H:%M:%S] ", tLocalTime);    
  78.         string strTime = szTime;    
  79.         return strTime;    
  80.     }    

  81. };    

  82. #ifdef LOG_ENABLE  

  83. //用下面这些宏来使用本文件  
  84. #define LOG(x)              CLog::WriteLog(x);          //括号内可以是字符串(ascii)、整数、浮点数、bool等  
  85. #define LOG2(x1,x2)     CLog::WriteLog2(x1,x2);  
  86. #define LOG_FUNC        LOG(__FUNCTION__)               //输出当前所在函数名  
  87. #define LOG_LINE        LOG(__LINE__)                       //输出当前行号  
  88. #define LOG_FUNC_BEGIN  CLog::WriteFuncBegin(__FUNCTION__);     //形式如:[时间]"------------FuncName  Begin------------"  
  89. #define LOG_FUNC_END     CLog::WriteFuncEnd(__FUNCTION__);      //形式如:[时间]"------------FuncName  End------------"  

  90. #else  

  91. #define LOG(x)                
  92. #define LOG2(x1,x2)       
  93. #define LOG_FUNC          
  94. #define LOG_LINE          
  95. #define LOG_FUNC_BEGIN    
  96. #define LOG_FUNC_END      

  97. #endif  

  98. #endif    




使用:

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


  1. #include "Log.h"  




    1. BOOL   



    2. int float BOOL enum )  
    3. return  
    4. 效果: 一个小巧的C++Log输出到文件类 (转)_#define