From 20b233e96f7ea8cf58c26ae6a35ed48153dfc40e Mon Sep 17 00:00:00 2001 From: Philip Prindeville Date: Thu, 4 Jan 2024 11:11:55 -0700 Subject: [PATCH] Allow more complex debugging like YANG with DETAIL --- lib/clixon/clixon_debug.h | 31 ++++++++++++++++++++++++++----- lib/src/clixon_debug.c | 4 ++-- lib/src/clixon_proto.c | 10 +++++----- lib/src/clixon_xml_sort.c | 2 +- 4 files changed, 34 insertions(+), 13 deletions(-) diff --git a/lib/clixon/clixon_debug.h b/lib/clixon/clixon_debug.h index 3c8e5a69..da203b50 100644 --- a/lib/clixon/clixon_debug.h +++ b/lib/clixon/clixon_debug.h @@ -46,11 +46,20 @@ * Constants */ -/* Debug-level masks */ -#define CLIXON_DBG_DEFAULT 1 /* Default logs */ -#define CLIXON_DBG_MSG 2 /* In/out messages and datastore reads */ -#define CLIXON_DBG_DETAIL 4 /* Details: traces, parse trees, etc */ -#define CLIXON_DBG_EXTRA 8 /* Extra Detailed logs */ +/* Detail level */ +#define CLIXON_DBG_ALWAYS 0x0 /* Unconditionally logged */ +#define CLIXON_DBG_DETAIL 0x1 /* Details: traces, parse trees, etc */ +#define CLIXON_DBG_DETAIL2 0x2 /* Extra details */ +#define CLIXON_DBG_DETAIL3 0x3 /* Probably more detail than you want */ +#define CLIXON_DBG_DMASK 0x3 /* Detail mask */ + +/* Subject area */ +#define CLIXON_DBG_DEFAULT 0x04 /* Default logs */ +#define CLIXON_DBG_MSG 0x08 /* In/out messages and datastore reads */ +#define CLIXON_DBG_XML 0x10 /* XML processing */ +#define CLIXON_DBG_XPATH 0x20 /* XPath processing */ +#define CLIXON_DBG_YANG 0x40 /* YANG processing */ +#define CLIXON_DBG_SMASK ~0x03 /* Subject mask */ /* * Macros @@ -65,4 +74,16 @@ int clixon_debug_init(clixon_handle h, int dbglevel); int clixon_debug_get(void); int clixon_debug_fn(clixon_handle h, int dbglevel, cxobj *x, const char *format, ...) __attribute__ ((format (printf, 4, 5))); +static inline int clixon_debug_isset(unsigned n) +{ + unsigned level = clixon_debug_get(); + unsigned detail = (n & CLIXON_DBG_DMASK); + unsigned subject = (n & CLIXON_DBG_SMASK); + + /* not this subject */ + if ((level & subject) == 0) + return 0; + return ((level & CLIXON_DBG_DMASK) >= detail); +} + #endif /* _CLIXON_DEBUG_H_ */ diff --git a/lib/src/clixon_debug.c b/lib/src/clixon_debug.c index 41b8e521..5ce7cabe 100644 --- a/lib/src/clixon_debug.c +++ b/lib/src/clixon_debug.c @@ -1,4 +1,4 @@ - /* +/* * ***** BEGIN LICENSE BLOCK ***** @@ -146,7 +146,7 @@ clixon_debug_fn(clixon_handle h, cbuf *cb = NULL; /* Mask debug level with global dbg variable */ - if ((dbglevel & clixon_debug_get()) == 0) + if (!clixon_debug_isset(dbglevel)) return 0; if (h == NULL) /* Accept NULL, use saved clixon handle */ h = _debug_clixon_h; diff --git a/lib/src/clixon_proto.c b/lib/src/clixon_proto.c index 2c0a67f5..e0fe56e9 100644 --- a/lib/src/clixon_proto.c +++ b/lib/src/clixon_proto.c @@ -322,7 +322,7 @@ msg_hex(int dbglevel, cbuf *cb = NULL; int i; - if ((dbglevel & clixon_debug_get()) == 0) /* compare debug level with global variable */ + if (!clixon_debug_isset(dbglevel)) /* compare debug level with global variable */ goto ok; if ((cb = cbuf_new()) == NULL){ clixon_err(OE_CFG, errno, "cbuf_new"); @@ -372,7 +372,7 @@ clicon_msg_send(int s, else{ clixon_debug(CLIXON_DBG_MSG, "Send: %s", msg->op_body); } - msg_hex(CLIXON_DBG_EXTRA, (char*)msg, ntohl(msg->op_len), __FUNCTION__); + msg_hex(CLIXON_DBG_DETAIL2, (char*)msg, ntohl(msg->op_len), __FUNCTION__); if (atomicio((ssize_t (*)(int, void *, size_t))write, s, msg, ntohl(msg->op_len)) < 0){ e = errno; @@ -438,7 +438,7 @@ clicon_msg_rcv(int s, clixon_err(OE_CFG, errno, "atomicio"); goto done; } - msg_hex(CLIXON_DBG_EXTRA, (char*)&hdr, hlen, __FUNCTION__); + msg_hex(CLIXON_DBG_DETAIL2, (char*)&hdr, hlen, __FUNCTION__); if (hlen == 0){ *eof = 1; goto ok; @@ -448,7 +448,7 @@ clicon_msg_rcv(int s, goto done; } mlen = ntohl(hdr.op_len); - clixon_debug(CLIXON_DBG_EXTRA, "op-len:%u op-id:%u", + clixon_debug(CLIXON_DBG_DETAIL2, "op-len:%u op-id:%u", mlen, ntohl(hdr.op_id)); clixon_debug(CLIXON_DBG_DETAIL, "%s: rcv msg len=%d", __FUNCTION__, mlen); @@ -467,7 +467,7 @@ clicon_msg_rcv(int s, goto done; } if (len2) - msg_hex(CLIXON_DBG_EXTRA, (*msg)->op_body, len2, __FUNCTION__); + msg_hex(CLIXON_DBG_DETAIL2, (*msg)->op_body, len2, __FUNCTION__); if (len2 != mlen - sizeof(hdr)){ clixon_err(OE_PROTO, 0, "body too short"); *eof = 1; diff --git a/lib/src/clixon_xml_sort.c b/lib/src/clixon_xml_sort.c index 27c32baf..54cf600c 100644 --- a/lib/src/clixon_xml_sort.c +++ b/lib/src/clixon_xml_sort.c @@ -386,7 +386,7 @@ xml_cmp(cxobj *x1, break; } /* switch */ done: - clixon_debug(CLIXON_DBG_EXTRA, "%s %s %s eq:%d nr: %d %d yi: %d %d", __FUNCTION__, xml_name(x1), xml_name(x2), equal, nr1, nr2, yi1, yi2); + clixon_debug(CLIXON_DBG_DETAIL2, "%s %s %s eq:%d nr: %d %d yi: %d %d", __FUNCTION__, xml_name(x1), xml_name(x2), equal, nr1, nr2, yi1, yi2); return equal; }