简单的LOG公共操作类设计
#ifndef OPP_LOGING_H #define OPP_LOGING_H #define LOG_ERR(fmt,...)\ Logger::instance()->Log(Logger::Error, fmt, __VA_ARGS__); #define LOG_WARN(fmt, ...)\ Logger::instance()->Log(Logger::Warn, fmt, __VA_ARGS__); #define LOG_INFO(fmt, ...)\ Logger::instance()->Log(Logger::Info, fmt, __VA_ARGS__); #define LOG_TRACE(fmt, ...)\ Logger::instance()->Log(Logger::Trace, fmt, __VA_ARGS__); #define LOG_DEBUG(fmt, ...)\ Logger::instance()->Log(Logger::Debug, fmt, __VA_ARGS__); class Logger { public: static const int Error = 0; static const int Warn = 1; static const int Info = 2; static const int Trace = 3; static const int Debug = 4; static const char * const LOG_LEVEL_NAME[5]; static int const LOG_SUFFIX[7]; virtual ~Logger(); static Logger * instance(); void SetLevel(int nLevel); void Log(int nLevel, const char * pszFmt, ...); void LogToFile(const char * pFileName, const char * pLog, const size_t nSize, bool bTruncate = false); void LogToStdout(const char * pLog, const size_t nSize); private: Logger(); private: int m_nFile;//File handle int m_nLevel;//Log level }; #endif
#include "StdAfx.h" #include "Logger.h" #include <time.h> #include <direct.h> #include <stdarg.h> #include <Windows.h> #define MAX_LOG_SIZE 1024 const char * const Logger::LOG_LEVEL_NAME[5]={ "Error", "Warn", "Info", "Trace", "Debug" }; int const Logger::LOG_SUFFIX[7] = {1,2,3,1,2,3,2}; Logger * Logger::instance() { static Logger log; return &log; } Logger::Logger() { m_nFile = -1; m_nLevel = Trace; } Logger::~Logger() { //logic code } void Logger::SetLevel(int nLevel) { m_nLevel = nLevel; } void Logger::Log( int nLevel, const char * pszFmt, ... ) { static int lastLogSuffix = -1; if ((nLevel < 0) || (nLevel > m_nLevel)) { return; } time_t nNow = time(NULL); struct tm * pLocalTime = localtime(&nNow); unsigned short year = pLocalTime->tm_year+1900; unsigned short month = pLocalTime->tm_mon+1; unsigned short day = pLocalTime->tm_mday; unsigned short hour = pLocalTime->tm_hour; unsigned short minute = pLocalTime->tm_min; unsigned short second = pLocalTime->tm_sec; unsigned short weekDay = pLocalTime->tm_wday; int tempLogSuffix = LOG_SUFFIX[weekDay];//1,2,3,1,2,3,2 char szLogFile[255] = {0}; char szTheLog[MAX_LOG_SIZE+2] = {0};//2 for \n \0 _mkdir("log"); //Log file Name sprintf(szLogFile, "log\\log_%d.log", tempLogSuffix); //Log content int nBytesWrite = 0; int ret = sprintf(szTheLog, "[%s][%d-%02d-%02d %02d:%02d:%02d]", LOG_LEVEL_NAME[nLevel], year, month, day, hour, minute, second); if (ret < 0) { return; } nBytesWrite += ret; va_list ap; va_start(ap,pszFmt); ret = vsnprintf_s(szTheLog+nBytesWrite, MAX_LOG_SIZE-nBytesWrite, MAX_LOG_SIZE-nBytesWrite, pszFmt, ap); if (ret <= 0) { return; } va_end(ap); nBytesWrite +=ret; if (nBytesWrite == MAX_LOG_SIZE) { szTheLog[MAX_LOG_SIZE] = '\n'; szTheLog[MAX_LOG_SIZE+1] = '\0'; nBytesWrite += 2; } else { szTheLog[nBytesWrite] = '\n'; szTheLog[nBytesWrite+1] = '\0'; nBytesWrite += 2; } if (lastLogSuffix != tempLogSuffix) { lastLogSuffix = tempLogSuffix; LogToFile(szLogFile, szTheLog, nBytesWrite, true); } else { LogToFile(szLogFile, szTheLog, nBytesWrite); } LogToStdout(szTheLog, nBytesWrite); return; } void Logger::LogToFile( const char * pFileName, const char * pLog, const size_t nSize, bool bTruncate /* = false */ ) { FILE * pFile = NULL; if (bTruncate) { pFile = fopen(pFileName, "w"); } else { pFile = fopen(pFileName, "a"); } if (pFile != NULL) { fwrite(pLog, 1, nSize, pFile); } fclose(pFile); return ; } void Logger::LogToStdout(const char *pLog, const size_t nSize) { printf("log to stdout:%s\n", pLog); }
日志保留三天.