Replaced the global variable debug with access function: clicon_debug_get().

This commit is contained in:
Olof hagsand 2020-06-09 16:04:49 +02:00
parent 718f494549
commit ff5462ecac
35 changed files with 227 additions and 100 deletions

View file

@ -457,7 +457,7 @@ xmldb_get_nocache(clicon_handle h,
if (xml_apply0(xt, -1, xml_sort_verify, NULL) < 0)
clicon_log(LOG_NOTICE, "%s: sort verify failed #2", __FUNCTION__);
#endif
if (debug>1)
if (clicon_debug_get()>1)
clicon_xml2file(stderr, xt, 0, 1);
*xtop = xt;
xt = NULL;
@ -565,7 +565,7 @@ xmldb_get_cache(clicon_handle h,
/* Copy the matching parts of the (relevant) XML tree.
* If cache was empty, also update to datastore cache
*/
if (debug>1)
if (clicon_debug_get()>1)
clicon_xml2file(stderr, x1t, 0, 1);
*xtop = x1t;
retval = 0;
@ -639,7 +639,7 @@ xmldb_get_zerocopy(clicon_handle h,
/* Apply default values (removed in clear function) */
if (xml_default_recurse(x0t) < 0)
goto done;
if (debug>1)
if (clicon_debug_get()>1)
clicon_xml2file(stderr, x0t, 0, 1);
*xtop = x0t;
retval = 0;

View file

@ -58,8 +58,12 @@
#include "clixon_err.h"
#include "clixon_log.h"
/* The global debug level. 0 means no debug */
int debug = 0;
/* The global debug level. 0 means no debug
* @note There are pros and cons in having the debug state as a global variable. The
* alternative to bind it to the clicon handle (h) was considered but it limits its
* usefulness, since not all functions have h
*/
static int _clixon_debug = 0;
/* Bitmask whether to log to syslog or stderr: CLICON_LOG_STDERR | CLICON_LOG_SYSLOG */
static int _logflags = 0x0;
@ -67,6 +71,7 @@ static int _logflags = 0x0;
/* Set to open file to write debug messages directly to file */
static FILE *_logfile = NULL;
/*! Initialize system logger.
*
* Make syslog(3) calls with specified ident and gates calls of level upto specified level (upto).
@ -217,7 +222,7 @@ clicon_log_str(int level,
/* syslog makes own filtering, we do it here:
* if normal (not debug) then filter loglevels >= debug
*/
if (debug == 0 && level >= LOG_DEBUG)
if (_clixon_debug == 0 && level >= LOG_DEBUG)
goto done;
if (_logflags & CLICON_LOG_STDERR){
flogtime(stderr);
@ -231,6 +236,7 @@ clicon_log_str(int level,
flogtime(_logfile);
fprintf(_logfile, "%s\n", msg);
fflush(_logfile);
}
/* Enable this if you want syslog in a stream. But there are problems with
@ -288,7 +294,6 @@ clicon_log(int level,
return retval;
}
/*! Initialize debug messages. Set debug level.
*
* Initialize debug module. The level is used together with clicon_debug(dbglevel) calls as follows:
@ -309,10 +314,16 @@ int
clicon_debug_init(int dbglevel,
FILE *f)
{
debug = dbglevel; /* Global variable */
_clixon_debug = dbglevel; /* Global variable */
return 0;
}
int
clicon_debug_get(void)
{
return _clixon_debug;
}
/*! Print a debug message with debug-level. Settings determine where msg appears.
*
* If the dbglevel passed in the function is equal to or lower than the one set by
@ -335,7 +346,7 @@ clicon_debug(int dbglevel,
char *msg = NULL;
int retval = -1;
if (dbglevel > debug) /* debug mask */
if (dbglevel > _clixon_debug) /* compare debug mask with global variable */
return 0;
/* first round: compute length of debug message */
va_start(args, format);

View file

@ -1559,7 +1559,7 @@ clixon_xml_find_api_path(cxobj *xt,
/* Parse api-path string to structured clixon-path data */
if (api_path_parse(api_path, &cplist) < 0)
goto done;
if (debug)
if (clicon_debug_get())
clixon_path_print(stderr, cplist);
/* Resolve module:name to yang-stmt, fail if not successful */
if ((ret = api_path_resolve(cplist, yt)) < 0)
@ -1652,7 +1652,7 @@ clixon_xml_find_instance_id(cxobj *xt,
va_end(ap);
if (instance_id_parse(path, &cplist) < 0)
goto done;
if (debug)
if (clicon_debug_get())
clixon_path_print(stderr, cplist);
/* Resolve module:name to pointer to yang-stmt, fail if not successful */
if ((ret = instance_id_resolve(cplist, yt)) < 0)

View file

@ -326,7 +326,7 @@ clicon_msg_send(int s,
clicon_debug(2, "%s: send msg len=%d",
__FUNCTION__, ntohl(msg->op_len));
if (debug > 2)
if (clicon_debug_get() > 2)
msg_dump(msg);
if (atomicio((ssize_t (*)(int, void *, size_t))write,
s, msg, ntohl(msg->op_len)) < 0){
@ -400,7 +400,7 @@ clicon_msg_rcv(int s,
clicon_err(OE_CFG, errno, "body too short");
goto done;
}
if (debug > 1)
if (clicon_debug_get() > 1)
msg_dump(*msg);
retval = 0;
done:

View file

@ -927,7 +927,7 @@ url_post(char *url,
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postfields);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(postfields));
if (debug)
if (clicon_debug_get())
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
if ((errcode = curl_easy_perform(curl)) != CURLE_OK){
clicon_debug(1, "%s: curl: %s(%d)", __FUNCTION__, err, errcode);

View file

@ -358,7 +358,7 @@ xml2cvec(cxobj *xt,
}
}
}
if (debug > 1){
if (clicon_debug_get() > 1){
clicon_debug(2, "%s cvv:\n", __FUNCTION__);
cvec_print(stderr, cvv);
}

View file

@ -504,7 +504,7 @@ xpath_parse(char *xpath,
clicon_err(OE_XML, 0, "XPATH parser error with no error code (should not happen)");
goto done;
}
if (debug > 1){
if (clicon_debug_get() > 1){
if ((cb = cbuf_new()) == NULL){
clicon_err(OE_XML, errno, "cbuf_new");
goto done;

View file

@ -940,7 +940,7 @@ xp_eval(xp_ctx *xc,
xp_ctx *xr2 = NULL;
int use_xr0 = 0; /* In 2nd child use transitively result of 1st child */
if (debug > 1)
if (clicon_debug_get() > 1)
ctx_print(stderr, xc, xpath_tree_int2str(xs->xs_type));
/* Pre-actions before check first child c0
*/
@ -1096,7 +1096,7 @@ xp_eval(xp_ctx *xc,
xr0 = NULL;
}
ok:
if (debug>1)
if (clicon_debug_get() > 1)
ctx_print(stderr, *xrp, xpath_tree_int2str(xs->xs_type));
retval = 0;
done:

View file

@ -195,13 +195,6 @@
extern int clixon_yang_parseget_lineno (void);
int
clicon_yang_debug(int d)
{
debug = d;
return 0;
}
/*
clixon_yang_parseerror
also called from yacc generated code *