zoukankan      html  css  js  c++  java
  • 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 }
    
    
    
     
  • 相关阅读:
    安装IDM扩展
    Go_数组&切片
    Mycat概念&安装
    IDEA自定义主题
    完全卸载Oracle11g
    创建型模式——单例模式(Singleton)
    设计模式统计
    PHP解压带密码的zip文件
    Win推荐软件
    如何设置线程池的线程数?
  • 原文地址:https://www.cnblogs.com/AquaGot/p/7241289.html
Copyright © 2011-2022 走看看