惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
F
Fortinet All Blogs
爱范儿
爱范儿
博客园 - Franky
MyScale Blog
MyScale Blog
罗磊的独立博客
博客园 - 三生石上(FineUI控件)
B
Blog
P
Proofpoint News Feed
IT之家
IT之家
宝玉的分享
宝玉的分享
D
DataBreaches.Net
S
SegmentFault 最新的问题
Microsoft Azure Blog
Microsoft Azure Blog
GbyAI
GbyAI
M
MIT News - Artificial intelligence
L
LangChain Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
月光博客
月光博客
雷峰网
雷峰网
Stack Overflow Blog
Stack Overflow Blog
量子位
V
V2EX
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻

博客园 - 晨光静默

centos 命令 curl用到的一些命令 nginx记录 html select qq作文 mfc开启控制台打印日记 mfc右键菜单(包括系统托盘菜单)弹窗后如何消失 angular单页应用点击button自动刷新页面 setTimeout中调用angular的作用域生效 mfc模态对话框和非模态对话框注意点 c/c++元转分的bug c/c++中的崩溃代码片段 查看网络端口连接情况 win7x64安装python运行报错:api-ms-win-crt-runtime-|1-1-0.dll SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes qt导入头文件报错 python记录 (转)Curl命令详解 异步重叠IO linux xshell 命令上传下载文件 png转CBitmap
开箱即用的clogger
晨光静默 · 2023-03-08 · via 博客园 - 晨光静默

开发中,经常要记录各种日记,经常要先开发日记模块,现提供一个开箱即用的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