X-Git-Url: https://tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fpokey%2Flogging.c;fp=src%2Fpokey%2Flogging.c;h=81a4fda204d10b1921f4a8f9e0c57814d354f174;hb=04d33be4bd102de67bb6dba5c449e12fea0db4d2;hp=0000000000000000000000000000000000000000;hpb=b0a676988a8da3120e64ef0e1a4ea4c28b1511e1;p=tinc diff --git a/src/pokey/logging.c b/src/pokey/logging.c new file mode 100644 index 00000000..81a4fda2 --- /dev/null +++ b/src/pokey/logging.c @@ -0,0 +1,103 @@ +/* + 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/28 12:46:26 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(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) +{ + const int priorities[] = { LOG_DEBUG, LOG_INFO, LOG_NOTICE, LOG_ERR, LOG_CRIT }; + + if(debug_lvl >= level) + vsyslog(priorities[priority], fmt, ap); +} + +void tinc_syslog(int priority, char *fmt, ...) +{ + /* Mapping syslog prio -> tinc prio */ + const int priorities[] = { TLOG_CRITICAL, TLOG_CRITICAL, TLOG_CRITICAL, TLOG_ERROR, + TLOG_NOTICE, TLOG_NOTICE, TLOG_INFO, TLOG_DEBUG }; + 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))(0, priorities[priority], fmt, args); + } + va_end(args); +}