From fa3a0be69d7225cb028e0e33f47bcf8779d64e45 Mon Sep 17 00:00:00 2001 From: huangxin Date: Tue, 7 Feb 2023 09:22:54 +0800 Subject: [PATCH] =?UTF-8?q?OCT=201.=20Zlog=E4=BD=BF=E7=94=A8=E6=9C=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E6=A8=A1=E5=9D=97=E6=97=B6=E6=89=93=E5=8D=B0?= =?UTF-8?q?=E4=B8=BAUNKNOWN=202.=20=E5=A2=9E=E5=8A=A0=E6=9C=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E9=94=99=E8=AF=AF=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/zlog.conf | 2 +- srcs/libs/include/zlog_module.h | 59 +++++++++++++++++++---------- srcs/libs/misc/err.c | 4 +- srcs/libs/zlog_module/zlog_module.c | 27 ++++--------- 4 files changed, 52 insertions(+), 40 deletions(-) diff --git a/config/zlog.conf b/config/zlog.conf index ddbdfaf..31c4868 100644 --- a/config/zlog.conf +++ b/config/zlog.conf @@ -7,7 +7,7 @@ buffer max = 2MB #rotate lock file = /tmp/zlog.lock rotate lock file = self -default format = "%d(%F %T.%l) %-6V (%c:%F:%L) - %m%n" +default format = "%d(%F %T.%l) %-6V (%c:%f:%L) - %m%n" file perms = 600 fsync period = 1K diff --git a/srcs/libs/include/zlog_module.h b/srcs/libs/include/zlog_module.h index 118f974..75d756c 100644 --- a/srcs/libs/include/zlog_module.h +++ b/srcs/libs/include/zlog_module.h @@ -9,29 +9,50 @@ extern "C" { #endif #include -#define MAX_ZLOG_MOD_LEN (8) +#define DEF_ZLOG_MOD(ZLOG_MOD) \ + ZLOG_MOD(ZLOG_MOD_MAIN, "MAIN") \ + ZLOG_MOD(ZLOG_MOD_TASK, "TASK") \ + ZLOG_MOD(ZLOG_MOD_INIT, "INIT") \ + ZLOG_MOD(ZLOG_MOD_MISC, "MISC") \ + ZLOG_MOD(ZLOG_MOD_CONFIG, "CONFIG") \ + ZLOG_MOD(ZLOG_MOD_NET, "NET") \ + ZLOG_MOD(ZLOG_MOD_CRYPTO, "CRYPTO") \ + ZLOG_MOD(ZLOG_MOD_MQ, "MQ") \ + ZLOG_MOD(ZLOG_MOD_PROTO, "PROTO") \ + ZLOG_MOD(ZLOG_MOD_HTTPD, "HTTPD") \ + ZLOG_MOD(ZLOG_MOD_USER, "USER") \ + ZLOG_MOD(ZLOG_MOD_PPPOE, "PPPOE") \ + ZLOG_MOD(ZLOG_MOD_VXLAN, "VXLAN") \ + ZLOG_MOD(ZLOG_MOD_LWIP, "LWIP") \ + ZLOG_MOD(ZLOG_MOD_OPENDHCPD, "DHCPD") + +#define GENERATE_ZLOG_MOD_ENUM(ENUM, x) ENUM, typedef enum { - ZLOG_MOD_MAIN, - ZLOG_MOD_TASK, - ZLOG_MOD_INIT, - ZLOG_MOD_MISC, - ZLOG_MOD_CONFIG, - ZLOG_MOD_NET, - ZLOG_MOD_CRYPTO, - ZLOG_MOD_MQ, - ZLOG_MOD_PROTO, - ZLOG_MOD_HTTPD, -#ifdef USED_LWIP - ZLOG_MOD_USER, - ZLOG_MOD_PPPOE, - ZLOG_MOD_VXLAN, - ZLOG_MOD_LWIP, -#endif - ZLOG_MOD_OPENDHCPD, - ZLOG_MOD_MAX, + DEF_ZLOG_MOD(GENERATE_ZLOG_MOD_ENUM) } ZLOG_MOD_NAME; +//typedef enum { +// ZLOG_MOD_MAIN, +// ZLOG_MOD_TASK, +// ZLOG_MOD_INIT, +// ZLOG_MOD_MISC, +// ZLOG_MOD_CONFIG, +// ZLOG_MOD_NET, +// ZLOG_MOD_CRYPTO, +// ZLOG_MOD_MQ, +// ZLOG_MOD_PROTO, +// ZLOG_MOD_HTTPD, +//#ifdef USED_LWIP +// ZLOG_MOD_USER, +// ZLOG_MOD_PPPOE, +// ZLOG_MOD_VXLAN, +// ZLOG_MOD_LWIP, +//#endif +// ZLOG_MOD_OPENDHCPD, +// ZLOG_MOD_MAX, +//} ZLOG_MOD_NAME; + #define LOG_MSG(level, format, ...) \ do { \ zlog_##level(zlog_get_mod_cat(ZLOG_MOD_MAIN), format, ##__VA_ARGS__); \ diff --git a/srcs/libs/misc/err.c b/srcs/libs/misc/err.c index 339eda1..e7bba06 100644 --- a/srcs/libs/misc/err.c +++ b/srcs/libs/misc/err.c @@ -6,7 +6,9 @@ #define MAX_DESC_LENGTH (256) #define GENERATE_STRING(STRING, no, desc) {#STRING, desc}, -static const char *g_enumStrVal[][MAX_DESC_LENGTH] = {DEF_ERR_CODE(GENERATE_STRING)}; +static const char *g_enumStrVal[][MAX_DESC_LENGTH] = { + DEF_ERR_CODE(GENERATE_STRING) {"ERR_UNKNOWN", "未知错误"}, +}; const char *getErrorEnumNameString(int errCode) { return g_enumStrVal[errCode][0]; diff --git a/srcs/libs/zlog_module/zlog_module.c b/srcs/libs/zlog_module/zlog_module.c index d5cbda3..c21350e 100644 --- a/srcs/libs/zlog_module/zlog_module.c +++ b/srcs/libs/zlog_module/zlog_module.c @@ -2,6 +2,9 @@ // Created by xajhuang on 2023/2/6. // #include "zlog_module.h" +#include "misc.h" + +#define MAX_ZLOG_MOD_LEN (8) typedef struct { ZLOG_MOD_NAME logModule; @@ -9,29 +12,15 @@ typedef struct { char catName[MAX_ZLOG_MOD_LEN]; } ZLOG_MODULE; +#define GENERATE_ENUM_ARRAY(ENUM, desc) {ENUM, NULL, desc}, + static ZLOG_MODULE g_zlogModule[] = { - {ZLOG_MOD_MAIN, NULL, "MAIN" }, - {ZLOG_MOD_TASK, NULL, "TASK" }, - {ZLOG_MOD_INIT, NULL, "INIT" }, - {ZLOG_MOD_MISC, NULL, "MISC" }, - {ZLOG_MOD_CONFIG, NULL, "CONFIG"}, - {ZLOG_MOD_NET, NULL, "NET" }, - {ZLOG_MOD_CRYPTO, NULL, "CRYPTO"}, - {ZLOG_MOD_MQ, NULL, "MQ" }, - {ZLOG_MOD_PROTO, NULL, "PROTO" }, - {ZLOG_MOD_HTTPD, NULL, "HTTPD" }, -#ifdef USED_LWIP - {ZLOG_MOD_USER, NULL, "USER" }, - {ZLOG_MOD_PPPOE, NULL, "PPPOE" }, - {ZLOG_MOD_VXLAN, NULL, "VXLAN" }, - {ZLOG_MOD_LWIP, NULL, "LWIP" }, -#endif - {ZLOG_MOD_OPENDHCPD, NULL, "DHCPD" } + DEF_ZLOG_MOD(GENERATE_ENUM_ARRAY) {-1, NULL, "UNKNOWN"} }; zlog_category_t *zlog_get_mod_cat(ZLOG_MOD_NAME logMod) { - if (logMod >= ZLOG_MOD_MAX || logMod < 0) { - logMod = ZLOG_MOD_MAIN; + if (logMod >= ARRAY_SIZE(g_zlogModule) || logMod < 0) { + logMod = ARRAY_SIZE(g_zlogModule) - 1; } if (g_zlogModule[logMod].pCat == NULL) {