c++日志類


 1 /*
 2 簡單日志類
 3 */
 4 #ifndef LOGGER_H_
 5 #define LOGGER_H_
 6 #include <string>
 7 #include <time.h>
 8 #include <Windows.h>
 9 using namespace std;
10 //日志級別枚舉  
11 typedef enum LogLevel  
12 {  
13     信息 = 0,  //什么都不記錄  
14     錯誤,     //只記錄嚴重錯誤  
15     警告,     //記錄嚴重錯誤,普通錯誤  
16     致命,   //記錄嚴重錯誤,普通錯誤,警告  
17 };  
18 class CLog{
19 
20 public:
21     CLog();
22     ~CLog();
23     //寫日志方法
24     void Log(string msg,LogLevel level );
25 protected:
26     //初始化
27     void GetExeName();
28     //獲取當前系統時間
29     string GetCurrentTime();
30     string m_Name;
31     string m_Path;
32 };
33 #endif

 1 #include "stdafx.h"
 2 #include "Log.h"
 3 #include <fstream>  
 4 CLog::CLog()
 5 {
 6     if (m_Name.size()>0) m_Name.clear();
 7     if (m_Path.size()>0) m_Path.clear();
 8     this->GetExeName();
 9     
10 }
11 CLog::~CLog()
12 {
13     
14 }
15 void CLog::GetExeName()
16 {
17     // 取進程執行文件名稱
18     char szExeName[MAX_PATH ]={0};  
19     //GetModuleFileNameA(NULL, szExeName, MAX_PATH);
20     if ( ::GetModuleFileNameA(NULL, szExeName, MAX_PATH) > 0)//獲取當前進程已加載模塊的文件的完整路徑
21     {
22         char* pBegin = szExeName;
23         char* pTemp  = szExeName;
24         
25 
26         while ( *pTemp != 0 )
27         {
28             if ( *pTemp == '\\' )
29             {
30                 pBegin = pTemp + 1;
31 
32             }
33             pTemp++;
34 
35         }
36         //(strrchr(szExeName, '\\'))[0] = 0; // 刪除文件名,只獲得路徑字串  
37         m_Path.insert(0,szExeName);
38         m_Name.insert(0,pBegin);
39         int a = m_Name.find('.');
40         m_Name.erase(a);
41         ZeroMemory(szExeName,sizeof(szExeName));
42         sprintf_s(szExeName,"%s%s",m_Name.c_str(),".log");
43         m_Name.clear();
44         m_Name.insert(0,szExeName);
45         
46     }
47 }
48 void CLog::Log(string msg,LogLevel level )
49 {
50     ofstream outfile;  
51     outfile.open(m_Name.c_str(),ios::out|ios::app|ios::ate);
52   switch(level)
53   {
54       case 信息:
55           if (outfile.is_open()) outfile<<this->GetCurrentTime().c_str()<<m_Path.c_str()<<": "<<msg.c_str()<<" -->信息"<<endl;
56           break;
57       case 錯誤:
58           if (outfile.is_open()) outfile<<this->GetCurrentTime().c_str()<<m_Path.c_str()<<": "<<msg.c_str()<<" -->錯誤"<<endl;
59           break;
60       case 警告:
61           if (outfile.is_open()) outfile<<this->GetCurrentTime().c_str()<<m_Path.c_str()<<": "<<msg.c_str()<<" -->警告"<<endl;
62           break;
63       case 致命:
64           if (outfile.is_open()) outfile<<this->GetCurrentTime().c_str()<<m_Path.c_str()<<": "<<msg.c_str()<<" -->致命"<<endl;
65           break;
66   }
67   outfile.close();
68 }
69 
70 string CLog::GetCurrentTime()
71 {
72     struct tm t;   //tm結構指針
73     time_t now;  //聲明time_t類型變量
74     time(&now);      //獲取系統日期和時間
75     localtime_s(&t, &now);   //獲取當地日期和時間
76     char temp[50] = {0};
77     sprintf_s(temp,"[%d-%d-%d %d:%d:%d]" ,t.tm_year + 1900,t.tm_mon + 1,t.tm_mday,t.tm_hour,t.tm_min,t.tm_sec);    
78     std::string  pTemp=temp;
79     return pTemp;
80 }
 
         

 

 

 


免責聲明!

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



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