Restructured error,debug anf log API

Renamed functions clicon->clixon, replaced global variables w access functions
Unified clicon_netconf_error with clixon_err()
This commit is contained in:
Olof hagsand 2023-11-13 10:12:52 +01:00
parent 261469be16
commit 24a4991ec8
199 changed files with 4668 additions and 4158 deletions

165
lib/src/clixon_debug.c Normal file
View file

@ -0,0 +1,165 @@
/*
*
***** BEGIN LICENSE BLOCK *****
Copyright (C) 2009-2016 Olof Hagsand and Benny Holmgren
Copyright (C) 2017-2019 Olof Hagsand
Copyright (C) 2020-2022 Olof Hagsand and Rubicon Communications, LLC(Netgate)
This file is part of CLIXON.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Alternatively, the contents of this file may be used under the terms of
the GNU General Public License Version 3 or later (the "GPL"),
in which case the provisions of the GPL are applicable instead
of those above. If you wish to allow use of your version of this file only
under the terms of the GPL, and not to allow others to
use your version of this file under the terms of Apache License version 2,
indicate your decision by deleting the provisions above and replace them with
the notice and other provisions required by the GPL. If you do not delete
the provisions above, a recipient may use your version of this file under
the terms of any one of the Apache License version 2 or the GPL.
***** END LICENSE BLOCK *****
*
* Regular logging and debugging. Syslog using levels.
*/
#ifdef HAVE_CONFIG_H
#include "clixon_config.h" /* generated by config & autoconf */
#endif
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/types.h>
/* cligen */
#include <cligen/cligen.h>
/* clixon */
#include "clixon_queue.h"
#include "clixon_hash.h"
#include "clixon_handle.h"
#include "clixon_err.h"
#include "clixon_log.h"
#include "clixon_debug.h"
/*
* Local Variables
*/
/*! 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 _debug_level = 0;
/*! Initialize debug messages. Set debug level.
*
* Initialize debug module. The level is used together with clixon_debug(dbglevel) calls as follows:
* print message if level >= dbglevel.
* Example: clixon_debug_init(1) -> debug(1) is printed, but not debug(2).
* Normally, debug messages are sent to clixon_log() which in turn can be sent to syslog and/or stderr.
* But you can also override this with a specific debug file so that debug messages are written on the file
* independently of log or errors. This is to ensure that a syslog of normal logs is unpolluted by extensive
* debugging.
*
* @param[in] h Clixon handle
* @param[in] dbglevel 0 is show no debug messages, 1 is normal, 2.. is high debug.
* Note this is _not_ level from syslog(3)
* @param[in] f Debug-file. Open file where debug messages are directed.
* @see clixon_log_file For specifying a debug-file
*/
int
clixon_debug_init(clixon_handle h,
int dbglevel)
{
_debug_level = dbglevel; /* Global variable */
return 0;
}
/*! Get debug level
*/
int
clixon_debug_get(void)
{
return _debug_level;
}
/*! 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
* clixon_debug_init(level). That is, only print debug messages <= than what you want:
* print message if level >= dbglevel.
* The message is sent to clixon_log. EIther to syslog, stderr or both, depending on
* clixon_log_init() setting
* @param[in] dbglevel Mask of CLIXON_DBG_DEFAULT and other masks
* @param[in] format Message to print as argv.
* @retval 0 OK
* @retval -1 Error
* @see clixon_debug_xml Specialization for XML tree
* @see CLIXON_DBG_DEFAULT and other flags
*/
int
clixon_debug(int dbglevel,
const char *format, ...)
{
int retval = -1;
va_list args;
size_t len;
char *msg = NULL;
size_t trunc;
/* Mask debug level with global dbg variable */
if ((dbglevel & clixon_debug_get()) == 0)
return 0;
/* first round: compute length of debug message */
va_start(args, format);
len = vsnprintf(NULL, 0, format, args);
va_end(args);
/* Truncate long debug strings */
if ((trunc = clixon_log_string_limit_get()) && trunc < len)
len = trunc;
/* allocate a message string exactly fitting the message length */
if ((msg = malloc(len+1)) == NULL){
clixon_err(OE_UNIX, errno, "malloc");
goto done;
}
/* second round: compute write message from format and args */
va_start(args, format);
if (vsnprintf(msg, len+1, format, args) < 0){
va_end(args);
clixon_err(OE_UNIX, errno, "vsnprintf");
goto done;
}
va_end(args);
clixon_log_str(LOG_DEBUG, msg);
retval = 0;
done:
if (msg)
free(msg);
return retval;
}