OCT 1. Zlog使用未定义模块时打印为UNKNOWN

2. 增加未定义错误信息
This commit is contained in:
huangxin 2023-02-07 09:22:54 +08:00
parent be5a43a8af
commit fa3a0be69d
4 changed files with 52 additions and 40 deletions

View File

@ -7,7 +7,7 @@ buffer max = 2MB
#rotate lock file = /tmp/zlog.lock #rotate lock file = /tmp/zlog.lock
rotate lock file = self 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 file perms = 600
fsync period = 1K fsync period = 1K

View File

@ -9,29 +9,50 @@ extern "C" {
#endif #endif
#include <zlog.h> #include <zlog.h>
#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 { typedef enum {
ZLOG_MOD_MAIN, DEF_ZLOG_MOD(GENERATE_ZLOG_MOD_ENUM)
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; } 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, ...) \ #define LOG_MSG(level, format, ...) \
do { \ do { \
zlog_##level(zlog_get_mod_cat(ZLOG_MOD_MAIN), format, ##__VA_ARGS__); \ zlog_##level(zlog_get_mod_cat(ZLOG_MOD_MAIN), format, ##__VA_ARGS__); \

View File

@ -6,7 +6,9 @@
#define MAX_DESC_LENGTH (256) #define MAX_DESC_LENGTH (256)
#define GENERATE_STRING(STRING, no, desc) {#STRING, desc}, #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) { const char *getErrorEnumNameString(int errCode) {
return g_enumStrVal[errCode][0]; return g_enumStrVal[errCode][0];

View File

@ -2,6 +2,9 @@
// Created by xajhuang on 2023/2/6. // Created by xajhuang on 2023/2/6.
// //
#include "zlog_module.h" #include "zlog_module.h"
#include "misc.h"
#define MAX_ZLOG_MOD_LEN (8)
typedef struct { typedef struct {
ZLOG_MOD_NAME logModule; ZLOG_MOD_NAME logModule;
@ -9,29 +12,15 @@ typedef struct {
char catName[MAX_ZLOG_MOD_LEN]; char catName[MAX_ZLOG_MOD_LEN];
} ZLOG_MODULE; } ZLOG_MODULE;
#define GENERATE_ENUM_ARRAY(ENUM, desc) {ENUM, NULL, desc},
static ZLOG_MODULE g_zlogModule[] = { static ZLOG_MODULE g_zlogModule[] = {
{ZLOG_MOD_MAIN, NULL, "MAIN" }, DEF_ZLOG_MOD(GENERATE_ENUM_ARRAY) {-1, NULL, "UNKNOWN"}
{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" }
}; };
zlog_category_t *zlog_get_mod_cat(ZLOG_MOD_NAME logMod) { zlog_category_t *zlog_get_mod_cat(ZLOG_MOD_NAME logMod) {
if (logMod >= ZLOG_MOD_MAX || logMod < 0) { if (logMod >= ARRAY_SIZE(g_zlogModule) || logMod < 0) {
logMod = ZLOG_MOD_MAIN; logMod = ARRAY_SIZE(g_zlogModule) - 1;
} }
if (g_zlogModule[logMod].pCat == NULL) { if (g_zlogModule[logMod].pCat == NULL) {