开箱即用的clogger
开发中,经常要记录各种日记,经常要先开发日记模块,现提供一个开箱即用的clogger,在一些简单的项目中可以快速进行集成开发。clogger提供win32动态库和静态库两个版本。具体调用见demo,使用中如有问题欢迎指出。
1 namespace libtomato { 2 3 class NS_API NsLog 4 { 5 public: 6 NsLog(); 7 NsLog(const char* filePath, int logLevel = LEVEL_DEBUG, int fileMaxCount = 65, int safeFlag = 1); 8 NsLog(const char* saveDir, const char* fileName, const char* fileSuffix = ".txt", int logLevel = LEVEL_DEBUG, 9 int fileMaxCount = 65, int fileMaxSize = 0, int safeFlag = 1, int logHeader = 1, int logDate = 0); 10 virtual ~NsLog(); 11 12 void setLoggerLevel(int logLevel); 13 void setLoggerMaxCount(int maxCount); 14 void setLoggerMaxSize(int fileSize); 15 void setLoggerSafe(int safeflag); 16 void setLoggerHeader(int logHeader); 17 void setLoggerHeaderDate(int dateHeader); 18 void setLoggerName(const char* logName); 19 void setLoggerSuffix(const char* fileSuffix); 20 void setLoggerDir(const char* saveDir); 21 void setLoggerPath(const char* filepath); 22 char* printLoggerInfo(const char* file, int line); 23 char* getLoggerPath(); 24 25 public: 26 void trace(const char* file, int line, const char* fmt, ...); 27 void debug(const char* file, int line, const char* fmt, ...); 28 void info(const char* file, int line, const char* fmt, ...); 29 void warn(const char* file, int line, const char* fmt, ...); 30 void error(const char* file, int line, const char* fmt, ...); 31 void byte(int level, const char* file, int line, const char* pbuf, int bufLen, const char* fmt, ...); 32 void dump(int level, const char* file, int line, const char* pbuf, int bufLen, const char* fmt, ...); 33 34 public: 35 static int MakeDirs(const char *path); 36 static char* GetCurModulePath(char* pModulePath); 37 38 private: 39 void initLogger(const char* saveDir, const char* fileName, 40 const char* fileSuffix = ".txt", 41 int logLevel = LEVEL_DEBUG, 42 int fileMaxCount = 65, 43 int fileMaxSize = 0, 44 int safeFlag = 1, 45 int logHeader = 1, 46 int logDate = 0); 47 int getFilePathInfo(const char* filepath, char* pOutDir, 48 char* pFileName, char* pFileSuffix); 49 void validateSaveCount(int count); 50 void validateSaveDir(const char* filepath = NULL); 51 int validateFileName(char* outFileName, const int outFileNameSize); 52 void writeLog(int level, const char* file, const int line, const char *fmt, ...); 53 void log(int level, const char* file, int line, const char* format, va_list arg_ptr); 54 55 private: 56 static const char* s_level[LEVEL_COUNT]; 57 int m_loggerLevel; 58 int m_loggerMaxCount; 59 int m_loggerMaxSize; 60 int m_loggerSafe; 61 int m_loggerHeader; 62 int m_loggerHeaderDate; 63 char m_loggerName[256]; 64 char m_loggerSuffix[16]; 65 char m_loggerDir[256]; // endwith /\ 66 #ifdef _WIN32 67 HANDLE m_lock; 68 #else 69 pthread_mutex_t m_lock; 70 #endif 71 72 };
调用示例:
1 include "libtomato/libtomato_log.h" 2 3 #ifdef _DEBUG 4 #pragma comment(lib, "./libtomato/libtomatost.lib") 5 #else 6 #pragma comment(lib,"./libtomato/libtomato.lib") 7 #endif 8 9 using namespace libtomato; 10 11 void testNslog() 12 { 13 char szLogPath[260]; 14 NsLog::GetCurModulePath(szLogPath); 15 strcat(szLogPath,"\\log\\log.txt"); 16 MsgBox(szLogPath); 17 printf("msgbox=%s\n", szLogPath); 18 19 #if 1 20 NsLog logger; // 当前模块路径下生成日记 21 logger.setLoggerName("hello"); 22 #elif 0 23 NsLog logger(szLogPath); 24 logger.setLoggerName("test"); 25 logger.setLoggerSuffix(".log"); 26 #else 27 NsLog logger; 28 char saveDir[300]; 29 sprintf(saveDir,"%s\\log\\",NsLog::GetCurModulePath(szLogPath)); 30 logger.setLoggerDir(saveDir); 31 logger.setLoggerName("logs"); 32 #endif 33 34 logger.printLoggerInfo(LOG_FILE_LINE); 35 logger.trace(LOG_FILE_LINE,"this is tracelog ..."); 36 logger.debug(LOG_FILE_LINE,"this is debuglog ..."); 37 logger.info(LOG_FILE_LINE,"this is infolog ..."); 38 logger.warn(LOG_FILE_LINE,"this is warnlog ..."); 39 logger.error(LOG_FILE_LINE,"this is warnlog ..."); 40 logger.info(LOG_FILE_LINE,"GetCurModulePath=%s",logger.GetCurModulePath(0)); 41 logger.info(LOG_FILE_LINE,"getLoggerPath=%s",logger.getLoggerPath()); 42 logger.error(LOG_FILE_LINE,"GetLibtomatoVer=%s",GetLibtomatoVer()); 43 char szBuff[10]; 44 sprintf(szBuff,"地球人"); 45 logger.byte(LEVEL_INFO,LOG_FILE_LINE,szBuff,sizeof(szBuff),"[%s]=%d", szBuff, strlen(szBuff)); 46 logger.dump(LEVEL_INFO,LOG_FILE_LINE,szBuff,sizeof(szBuff),"[%s]=%d", szBuff, strlen(szBuff)); 47 logger.info(LOG_FILE_LINE,"虫子重来没被战胜过!"); 48 } 49 51 int main(int argc, char* argv[]) 52 { 53 printf("Hello World!\n"); 54 55 testNslog(); 56 57 printf("go valid loginfo ...\n"); 58 return 0; 59 }
下载连接:https://files.cnblogs.com/files/zhangmo/libtomatoDemo.zip?t=1678205411

浙公网安备 33010602011771号