26f8ddddc9cbbd638176f70f81d66766a4ab94a9
[tinc] / src / logging.c
1 /*
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>
5
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.
10
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.
15
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.
19
20     $Id: logging.c,v 1.5 2002/04/13 18:01:58 zarq Exp $
21 */
22
23 #include "config.h"
24
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <syslog.h>
29
30 #include <avl_tree.h>
31
32 #include "logging.h"
33
34 avl_tree_t *log_hooks_tree = NULL;
35
36 int debug_lvl = 0;
37
38 int log_compare(const void *a, const void *b)
39 {
40   if(a < b)
41     return -1;
42   if(a > b)
43     return 1;
44   return 0;
45 }
46
47 void log(int level, int priority, char *fmt, ...)
48 {
49   avl_node_t *avlnode;
50   va_list args;
51
52   va_start(args, fmt);
53   for(avlnode = log_hooks_tree->head; avlnode; avlnode = avlnode->next)
54     {
55       assert(avlnode->data);
56       ((log_function_t*)(avlnode->data))(level, priority, fmt, args);
57     }
58   va_end(args);
59 }
60
61 void log_add_hook(log_function_t *fn)
62 {
63   if(!log_hooks_tree)
64     log_hooks_tree = avl_alloc_tree(log_compare, NULL);
65
66   avl_insert(log_hooks_tree, (void*)fn);
67 }
68
69 void log_del_hook(log_function_t *fn)
70 {
71   avl_delete(log_hooks_tree, (void*)fn);
72 }
73
74 void log_default(int level, int priority, char *fmt, va_list ap)
75 {
76   if(debug_lvl >= level)
77     {
78       vfprintf(stderr, fmt, ap);
79       fprintf(stderr, "\n");
80     }
81 }
82
83 void log_syslog(int level, int priority, char *fmt, va_list ap)
84 {
85   const int priorities[] = { LOG_DEBUG, LOG_INFO, LOG_NOTICE, LOG_ERR, LOG_CRIT };
86
87   if(debug_lvl >= level)
88     vsyslog(priorities[priority], fmt, ap);
89 }
90
91 void tinc_syslog(int priority, char *fmt, ...)
92 {
93   /* Mapping syslog prio -> tinc prio */
94   const int priorities[] = { TLOG_CRITICAL, TLOG_CRITICAL, TLOG_CRITICAL, TLOG_ERROR,
95                                TLOG_NOTICE, TLOG_NOTICE, TLOG_INFO, TLOG_DEBUG };
96   avl_node_t *avlnode;
97   va_list args;
98
99   va_start(args, fmt);
100   for(avlnode = log_hooks_tree->head; avlnode; avlnode = avlnode->next)
101     {
102       assert(avlnode->data);
103       ((log_function_t*)(avlnode->data))(0, priorities[priority], fmt, args);
104     }
105   va_end(args);
106 }