Windows和Linux上通用的日志类MyLogWindows和Linux上通用的日志类MyLog0.四个文件源码
下面是四个文件源代码,MyLog.h、MyLog.cpp、main.cpp、Makefile; 很实用的日志类,参考main.cpp中的调用方式,此源码Linux和Windows通用; *******************MyLog.h***************************** #ifndef MYLOG_H #define MYLOG_H #include <stdio.h> #include <stdarg.h> #include <time.h> #include <string.h> const int MAX_LOG_LENGTH = 1024; /* 日志报警等级 */ typedef enum { LEVEL_DEBUG = 0, //调试,级别最低,在系统实际运行过程中一般都是不输出的 LEVEL_INFO = 1, //信息,反馈系统的当前状态给最终用户的 LEVEL_WARN = 2, //警告,这时进行一些修复性的工作,系统能够恢复,可以继续运行下去 LEVEL_ERROR = 3, //错误,导致一个无法修复的错误 LEVEL_FATAL = 4 //致命 } LEVEL_TYPE; //不同等级对应的字符串 const char LOG_ERROR[][8] = { "Debug", " Info", " Warn", "Error", "Fatal" }; class CMyLog { public: CMyLog(); ~CMyLog(); void WriteLog(LEVEL_TYPE level, const char* szTitle, const char* format,...); // 日志打印接口,参数可变 protected: void PrintLog(const char* szMsg); // 打印输出信息 void GetDateString(char* szDate); // 获得日期, 实现省略 void GetTimeString(char* szTime); // 获得时间,实现省略 private: FILE *m_file; // 日志文件 LEVEL_TYPE m_iLevel; }; #endif *******************MyLog.h***************************** *******************MyLog.cpp***************************** #include <stdio.h> #include "MyLog.h" extern LEVEL_TYPE g_OutputLogLevel; CMyLog::CMyLog() { //初始化变量 m_iLevel = g_OutputLogLevel; //日志输出级别,应该是从配置文件中获得 m_file = NULL; } CMyLog::~CMyLog() { if (NULL != m_file) { fclose(m_file); m_file = NULL; } } void CMyLog::WriteLog(LEVEL_TYPE level, const char* szTitle, const char* format,...) { //[时间][等级][标题][文件:函数][内容] char chTmp[20] = {0}; //时间字符串 char szMsg[MAX_LOG_LENGTH + 1]; //日志信息字符串 // 输入检查 if (NULL == format) { return; } // 等级控制,例如选择日志级别LEVEL_ERROR,那么WARN信息不会输出日志 if (level < m_iLevel) { return; } // 日期时间 strcpy(szMsg,"["); struct tm *newtime; time_t long_time; time( &long_time ); newtime = localtime( &long_time ); sprintf(chTmp, "%04d%02d%02d %02d:%02d:%02d" ,newtime->tm_year + 1900, newtime->tm_mon + 1,newtime->tm_mday,newtime->tm_hour,newtime->tm_min,newtime->tm_sec); strcat(szMsg,chTmp); strcat(szMsg, "]"); // 等级 strcat(szMsg, "["); strcat(szMsg, LOG_ERROR[level]); strcat(szMsg, "]"); // 标题 if (NULL != szTitle) { strcat(szMsg,"["); strcat(szMsg, szTitle); strcat(szMsg,"]: "); } // 内容 va_list argp; va_start(argp,format); #ifdef WIN32 _vsnprintf(&szMsg[strlen(szMsg)], MAX_LOG_LENGTH - strlen(szMsg), format, argp); #else vsnprintf(&szMsg[strlen(szMsg)], MAX_LOG_LENGTH - strlen(szMsg), format, argp); #endif va_end(argp); // 输出日志 this->PrintLog(szMsg); } void CMyLog::PrintLog(const char* szMsg) { //以小时为单位生成日志文件 char logFileName[100] = {0}; struct tm *newtime; time_t long_time; time( &long_time ); newtime = localtime( &long_time ); sprintf(logFileName, "MyTestLog%04d%02d%02d%02d.txt" ,newtime->tm_year + 1900, newtime->tm_mon + 1,newtime->tm_mday,newtime->tm_hour); // 打开文件句柄 if (NULL == m_file) { m_file = fopen(logFileName, "a+"); }else { fclose(m_file); m_file = NULL; m_file = fopen(logFileName, "a+"); } if (NULL == m_file) { //ASSERT(false);//打开文件失败 }else { fprintf(m_file, "%s/n", szMsg); fflush(m_file); //将缓存区的内容立即写入磁盘 } } *******************MyLog.cpp***************************** *******************main.cpp***************************** #include "stdio.h" #include "MyLog.h" LEVEL_TYPE g_OutputLogLevel = LEVEL_DEBUG; int main() { printf("This is MyLog program!/n"); printf("log level:%d %s/n",LEVEL_WARN,LOG_ERROR[LEVEL_WARN]); CMyLog cml; //[时间][等级][标题][信息内容] for(int i=0;i<1000;i++) { cml.WriteLog(LEVEL_DEBUG,"main.cpp:main()","content-%d %s",i,"debug"); cml.WriteLog(LEVEL_INFO,"main.cpp:main()","content-%d %s",i,"info"); cml.WriteLog(LEVEL_WARN,"main.cpp:main()","content-%d %s",i,"warn"); cml.WriteLog(LEVEL_ERROR,"main.cpp:main()","content-%d %s",i,"error"); cml.WriteLog(LEVEL_FATAL,"main.cpp:main()","content-%d %s",i,"fatal"); } return 0; } *******************main.cpp***************************** *********************makefile************************************ TARGET=MyLog OBJS=main.o MyLog.o INC= LIBPATH= %.o:%.cpp g++ -Wall -c -g -I$(INC) -I. $< -o $@ $(TARGET):$(OBJS) g++ $(OBJS) -o $(TARGET) .PHONY:clean clean: rm *.o $(TARGET) ***************************************************************** 1.将上述四个文件放在同一个目录下,直接运行make |
|