chiark / gitweb /
[PATCH] fix up logging code so that it can be built without it being enabled
[elogind.git] / logging.h
1 /*
2  * logging.h
3  *
4  * Simple logging functions that can be compiled away into nothing.
5  *
6  * Copyright (C) 2003,2004 Greg Kroah-Hartman <greg@kroah.com>
7  * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
8  *
9  *      This program is free software; you can redistribute it and/or modify it
10  *      under the terms of the GNU General Public License as published by the
11  *      Free Software Foundation version 2 of the License.
12  * 
13  *      This program is distributed in the hope that it will be useful, but
14  *      WITHOUT ANY WARRANTY; without even the implied warranty of
15  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  *      General Public License for more details.
17  * 
18  *      You should have received a copy of the GNU General Public License along
19  *      with this program; if not, write to the Free Software Foundation, Inc.,
20  *      675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */
23
24 #ifndef LOGGING_H
25 #define LOGGING_H
26
27 #define info(format, arg...)            do { } while (0)
28 #define dbg(format, arg...)             do { } while (0)
29 #define dbg_parse(format, arg...)       do { } while (0)
30 #define init_logging(foo)               do { } while (0)
31
32 #ifdef LOG
33 #include <stdarg.h>
34 #include <syslog.h>
35
36 #undef info
37 #define info(format, arg...)                                                            \
38         do {                                                                            \
39                 log_message (LOG_INFO , format , ## arg);                               \
40         } while (0)
41
42 #ifdef DEBUG
43 #undef dbg
44 #define dbg(format, arg...)                                                             \
45         do {                                                                            \
46                 log_message (LOG_DEBUG , "%s: " format , __FUNCTION__ , ## arg);        \
47         } while (0)
48 #endif
49
50 /* Parser needs it's own debugging statement, we usually don't care about this at all */
51 #ifdef DEBUG_PARSER
52 #undef dbg_parse
53 #define dbg_parse(format, arg...)                                                       \
54         do {                                                                            \
55                 log_message (LOG_DEBUG , "%s: " format , __FUNCTION__ , ## arg);        \
56         } while (0)
57 #endif
58
59 static void log_message (int level, const char *format, ...)
60         __attribute__ ((format (printf, 2, 3)));
61 static inline void log_message (int level, const char *format, ...)
62 {
63         va_list args;
64
65         va_start(args, format);
66         vsyslog(level, format, args);
67         va_end(args);
68 }
69
70 /* each program must declare this variable somewhere */
71 extern unsigned char logname[42];
72
73 #undef init_logging
74 static inline void init_logging(char *program_name)
75 {
76         snprintf(logname, 42,"%s[%d]", program_name, getpid());
77         openlog(logname, 0, LOG_DAEMON);
78 }
79
80 #endif  /* LOG */
81
82 #endif