[C++][Logging] 项目中写日志模块的实现
一提到log,相信很多人就知道我要写些什么东西了,在所有的项目中都有自己一套写日志的模块。下面呢,我就根据平时的项目的需求写了一个比较简单使用的logger,方便输出各种等级的log。
事先声明一下,代码仅供大家学习参考之用,有什么不足的地方还请大家提出来。
在实际使用中可以考虑把log放入一个list,然后用一个线程专门负责写log.
下面logger的结构,有需要的源代码的,索取一下密码,谢谢!
class Logger{public: static void InitLoggerConfig(void); static void StringLog(LogLevel logLevel, const string& strFuctionName, int lineNumber, string strFileName, const char* strInfo); static void Log(LogLevel logLevel, const string& strFuctionName, int lineNumber, string strFileName, const char* strFormat, ...); static void Log(LogLevel logLevel, StringBuilder& stringBuilder, const string& strFuctionName, int lineNumber, string strFileName); static void RetureLog(LogLevel logLevel, const string& strFuctionName, int lineNumber, string strFileName, const char* strInfo); static void LogPrinter(LogLevel logLevel, const char* strInfo, ...);private: static string GetFileMainPath();private: static string mStrLog; static string mStrFileName; static string mStrFileMainPath; static time_t mTimeValue; static LogLevel mLogLevel; static tm mDateTime; static LogManager mLogManager;};//测试代码int main(){ const char *pInfo = "strInfo"; const char *pWarringInfo = "Waring Log"; const char *pErrorInfo = "Error Log"; const char *pFatalInfo = "Fatal Log"; const char *pDebugInfo = "Debug Log"; INIT_LOG(); INFO_LOG(pInfo); WARN_LOG(pWarringInfo); ERR_LOG(pErrorInfo); FATAL_LOG(pFatalInfo); DEBUG_LOG(pDebugInfo); INFO("%s", pInfo); WARN("%s",pWarringInfo); ERR (" %s",pErrorInfo); FATAL(" %s",pFatalInfo); DEBUG(" %s",pDebugInfo); int i = 0; XX_INFO_LOG( pInfo << &i); XX_WARN_LOG( pWarringInfo << 1 ); XX_ERR_LOG( pErrorInfo << 2 ); XX_FATAL_LOG( pFatalInfo << 3 ); XX_DEBUG_LOG( pDebugInfo << 4 ); LOG_PRINTER(INFO,"%d %f %c %s", 1, 1.2, 'c', "123"); LOG_PRINTER(FATAL,"%d %f %c", 1, 1.2, 'c'); LOG_PRINTER(ERR,"%d %f %c", 1, 1.2, 'c'); return 0;}