C++日志类
来源:互联网 发布:手机直播软件 编辑:程序博客网 时间:2024/06/14 23:42
//Log.h头文件
/*简单日志类 */#ifndef LOGGER_H_#define LOGGER_H_#include <string>#include <time.h>#include <Windows.h>using namespace std;//日志级别枚举 typedef enum LogLevel { 信息 = 0, //什么都不记录 错误, //只记录严重错误 警告, //记录严重错误,普通错误 致命, //记录严重错误,普通错误,警告 }; class CLog{public:CLog();~CLog();//写日志方法void Log(string msg,LogLevel level );protected://初始化void GetExeName();//获取当前系统时间string GetCurrentTime();string m_Name;string m_Path;};#endif
//Log.cpp文件
#include "stdafx.h"#include "Log.h"#include <fstream> CLog::CLog(){if (m_Name.size()>0) m_Name.clear();if (m_Path.size()>0) m_Path.clear();this->GetExeName();}CLog::~CLog(){}void CLog::GetExeName(){ // 取进程执行文件名称char szExeName[MAX_PATH ]={0}; //GetModuleFileNameA(NULL, szExeName, MAX_PATH);if ( ::GetModuleFileNameA(NULL, szExeName, MAX_PATH) > 0)//获取当前进程已加载模块的文件的完整路径{char* pBegin = szExeName;char* pTemp = szExeName;while ( *pTemp != 0 ){if ( *pTemp == '\\' ){pBegin = pTemp + 1;}pTemp++;}//(strrchr(szExeName, '\\'))[0] = 0; // 删除文件名,只获得路径字串 m_Path.insert(0,szExeName);m_Name.insert(0,pBegin);int a = m_Name.find('.');m_Name.erase(a);ZeroMemory(szExeName,sizeof(szExeName));sprintf_s(szExeName,"%s%s",m_Name.c_str(),".log");m_Name.clear();m_Name.insert(0,szExeName);}}void CLog::Log(string msg,LogLevel level ){ofstream outfile; outfile.open(m_Name.c_str(),ios::out|ios::app|ios::ate); switch(level) { case 信息: if (outfile.is_open()) outfile<<this->GetCurrentTime().c_str()<<m_Path.c_str()<<": "<<msg.c_str()<<" -->信息"<<endl; break; case 错误: if (outfile.is_open()) outfile<<this->GetCurrentTime().c_str()<<m_Path.c_str()<<": "<<msg.c_str()<<" -->错误"<<endl; break; case 警告: if (outfile.is_open()) outfile<<this->GetCurrentTime().c_str()<<m_Path.c_str()<<": "<<msg.c_str()<<" -->警告"<<endl; break; case 致命: if (outfile.is_open()) outfile<<this->GetCurrentTime().c_str()<<m_Path.c_str()<<": "<<msg.c_str()<<" -->致命"<<endl; break; } outfile.close();}string CLog::GetCurrentTime(){struct tm t; //tm结构指针time_t now; //声明time_t类型变量time(&now); //获取系统日期和时间localtime_s(&t, &now); //获取当地日期和时间char temp[50] = {0};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);std::string pTemp=temp;return pTemp;}
阅读全文
0 0
- C语言日志类
- 文件日志类(WIN32 , C++)
- Windows 日志记录类(c++)
- C程序的日志类
- 【C++】添加写日志类
- C语言日志操作类实例
- C语言封装线程与日志类
- [C/C++]简单的C++日志操作类
- 编程日志(C++)
- C/C++编程日志
- C/C++编程日志
- C语言编程日志
- 系统日志 c
- 2010c数据结构日志
- c sharp日志开端
- c语言日志函数
- 简易C/C++日志
- C#Log日志
- Logback联动ELK
- Glide与圆形图片CircleImageView的问题
- Apache是世界使用排名第一的Web服务器软件
- java设计模式之---工厂方法模式
- docker install and configure
- C++日志类
- Linux设备驱动--LCD平台设备与驱动(smdk2440)
- 8.css可见性
- 关于vue2.0的一些文章
- Linux入门
- 关于Lua的Unity UI面向过程编程模板
- insert 死锁
- Maven私服软件Nexus
- JQ复选框,选择后按钮触发后出现图片,可同时出现多个图片