2 logging.c -- log messages to e.g. syslog
3 Copyright (C) 2001-2002 Guus Sliepen <guus@sliepen.warande.net>,
4 2001-2002 Ivo Timmermans <itimmermans@bigfoot.com>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 $Id: logging.c,v 1.4 2002/04/13 11:07:12 zarq Exp $
34 avl_tree_t *log_hooks_tree = NULL;
38 int log_compare(const void *a, const void *b)
47 void log(int level, int priority, char *fmt, ...)
53 for(avlnode = log_hooks_tree->head; avlnode; avlnode = avlnode->next)
55 assert(avlnode->data);
56 ((log_function_t*)(avlnode->data))(level, priority, fmt, args);
61 void log_add_hook(log_function_t *fn)
64 log_hooks_tree = avl_alloc_tree(log_compare, NULL);
66 avl_insert(log_hooks_tree, (void*)fn);
69 void log_del_hook(log_function_t *fn)
71 avl_delete(log_hooks_tree, (void*)fn);
74 void log_default(int level, int priority, char *fmt, va_list ap)
76 if(debug_lvl >= level)
77 vfprintf(stderr, fmt, ap);
80 void log_syslog(int level, int priority, char *fmt, va_list ap)
82 const int priorities[] = { LOG_DEBUG, LOG_INFO, LOG_NOTICE, LOG_ERR, LOG_CRIT };
84 if(debug_lvl >= level)
85 vsyslog(priorities[priority], fmt, ap);
88 void tinc_syslog(int priority, char *fmt, ...)
90 /* Mapping syslog prio -> tinc prio */
91 const int priorities[] = { TLOG_CRITICAL, TLOG_CRITICAL, TLOG_CRITICAL, TLOG_ERROR,
92 TLOG_NOTICE, TLOG_NOTICE, TLOG_INFO, TLOG_DEBUG };
97 for(avlnode = log_hooks_tree->head; avlnode; avlnode = avlnode->next)
99 assert(avlnode->data);
100 ((log_function_t*)(avlnode->data))(0, priorities[priority], fmt, args);