From cc603e2765f17555ecdc2b74c27ebf96e6691bf6 Mon Sep 17 00:00:00 2001 From: Ivo Timmermans Date: Sat, 13 Apr 2002 10:25:38 +0000 Subject: [PATCH] New logging system to replace syslog() calls with a generic function. --- src/logging.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/logging.h | 58 ++++++++++++++++++++++++++++++++++ src/process.c | 6 ++-- src/tincd.c | 8 ++--- 4 files changed, 150 insertions(+), 8 deletions(-) create mode 100644 src/logging.c create mode 100644 src/logging.h diff --git a/src/logging.c b/src/logging.c new file mode 100644 index 00000000..983bdfad --- /dev/null +++ b/src/logging.c @@ -0,0 +1,86 @@ +/* + logging.c -- log messages to e.g. syslog + Copyright (C) 2001-2002 Guus Sliepen , + 2001-2002 Ivo Timmermans + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + + $Id: logging.c,v 1.1 2002/04/13 10:25:38 zarq Exp $ +*/ + +#include "config.h" + +#include +#include +#include +#include + +#include + +#include "logging.h" + +avl_tree_t *log_hooks_tree = NULL; + +int debug_lvl = 0; + +int log_compare(const void *a, const void *b) +{ + if(a < b) + return -1; + if(a > b) + return 1; + return 0; +} + +void log_message(int level, int priority, char *fmt, ...) +{ + avl_node_t *avlnode; + va_list args; + + va_start(args, fmt); + for(avlnode = log_hooks_tree->head; avlnode; avlnode = avlnode->next) + { + assert(avlnode->data); + ((log_function_t*)(avlnode->data))(level, priority, fmt, args); + } + va_end(args); +} + +void log_add_hook(log_function_t *fn) +{ + if(!log_hooks_tree) + log_hooks_tree = avl_alloc_tree(log_compare, NULL); + + avl_insert(log_hooks_tree, (void*)fn); +} + +void log_del_hook(log_function_t *fn) +{ + avl_delete(log_hooks_tree, (void*)fn); +} + +void log_default(int level, int priority, char *fmt, va_list ap) +{ + if(debug_lvl >= level) + vfprintf(stderr, fmt, ap); +} + +void log_syslog(int level, int priority, char *fmt, va_list ap) +{ + int priorities[] = { LOG_DEBUG, LOG_INFO, LOG_NOTICE, LOG_ERR, LOG_CRIT }; + + if(debug_lvl >= level) + vsyslog(priorities[priority], fmt, ap); +} diff --git a/src/logging.h b/src/logging.h new file mode 100644 index 00000000..2e2ccf6f --- /dev/null +++ b/src/logging.h @@ -0,0 +1,58 @@ +/* + logging.h -- header for logging.c + Copyright (C) 2002 Guus Sliepen , + 2002 Ivo Timmermans + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + + $Id: logging.h,v 1.1 2002/04/13 10:25:38 zarq Exp $ +*/ + +#ifndef __TINC_LOGGING_H__ +#define __TINC_LOGGING_H__ + +#include + +enum { + TLOG_DEBUG, + TLOG_INFO, + TLOG_NOTICE, + TLOG_ERROR, + TLOG_CRITICAL +}; + +enum { + DEBUG_NOTHING = 0, /* Quiet mode, only show starting/stopping of the daemon */ + DEBUG_CONNECTIONS = 1, /* Show (dis)connects of other tinc daemons via TCP */ + DEBUG_ERROR = 2, /* Show error messages received from other hosts */ + DEBUG_STATUS = 2, /* Show status messages received from other hosts */ + DEBUG_PROTOCOL = 3, /* Show the requests that are sent/received */ + DEBUG_META = 4, /* Show contents of every request that is sent/received */ + DEBUG_TRAFFIC = 5, /* Show network traffic information */ + DEBUG_PACKET = 6, /* Show contents of each packet that is being sent/received */ + DEBUG_SCARY_THINGS = 10 /* You have been warned */ +}; + +typedef void (log_function_t)(int,int,char*,va_list); + +extern int debug_lvl; +extern avl_tree_t *log_hooks_tree; + +extern void log_message(int, int, char *, ...); +extern void log_add_hook(log_function_t *); +extern void log_del_hook(log_function_t *); +extern log_function_t log_default_hook; + +#endif /* __TINC_LOGGING_H__ */ diff --git a/src/process.c b/src/process.c index 8f9f01ae..1f0a012f 100644 --- a/src/process.c +++ b/src/process.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: process.c,v 1.2 2002/04/09 15:26:00 zarq Exp $ + $Id: process.c,v 1.3 2002/04/13 10:25:38 zarq Exp $ */ #include "config.h" @@ -189,8 +189,10 @@ cp if(!write_pid(pidfilename)) return -1; } - + openlog(identname, LOG_CONS | LOG_PID, LOG_DAEMON); + log_add_hook(log_syslog); + log_del_hook(log_default); if(debug_lvl > DEBUG_NOTHING) syslog(LOG_NOTICE, _("tincd %s (%s %s) starting, debug level %d"), diff --git a/src/tincd.c b/src/tincd.c index c02ce5cf..e53f5776 100644 --- a/src/tincd.c +++ b/src/tincd.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: tincd.c,v 1.12 2002/04/09 15:26:01 zarq Exp $ + $Id: tincd.c,v 1.13 2002/04/13 10:25:38 zarq Exp $ */ #include "config.h" @@ -338,11 +338,7 @@ main(int argc, char **argv, char **envp) if(show_help) usage(0); -#ifdef HAVE_SOLARIS - openlog("tinc", LOG_CONS, LOG_DAEMON); /* Catch all syslog() calls issued before detaching */ -#else - openlog("tinc", LOG_PERROR, LOG_DAEMON); /* Catch all syslog() calls issued before detaching */ -#endif + log_add_hook(log_default); g_argv = argv; -- 2.20.1