]> arthur.barton.de Git - netatalk.git/blob - include/atalk/logger.h
move most of a LOG macros in the logger function
[netatalk.git] / include / atalk / logger.h
1 #ifndef _ATALK_LOGGER_H
2 #define _ATALK_LOGGER_H 1
3
4 /* 
5  * logger LOG Macro Usage
6  * ======================
7  *
8  * LOG(<logtype>, <loglevel>, "<string>"[, args]);
9  * 
10  *
11  * logger API Setup
12  * ================
13  * 
14  * Standard interface:
15  * -------------------
16  *
17  *    setuplog(char *confstring)
18  *    confstring = "<logtype> <loglevel> [<filename>]"
19  *
20  * Calling without <filename> configures basic logging to syslog. Specifying <filename>
21  * configures extended logging to <filename>.
22  * 
23  * You can later disable logging by calling
24  *
25  *    unsetuplog(char *confstring)
26  *    confstring = "<logtype> [<any_string>]"
27  *
28  * Calling without <any_string> disables syslog logging, calling with <any_string>
29  * disables file logging.
30  *
31  * <logtype>:
32  * you can setup a default with "Default". Any other logtype used in LOG will then
33  * use the default if not setup itself. This is probabyl the only thing you may
34  * want to use.
35  *
36  * Example:
37  * setuplog("default log_debug /var/log/debug.log");
38  * See also libatalk/util/test/logger_test.c
39  *
40  * "Legacy" interface:
41  * -------------------
42  *
43  * Some netatalk daemons (31.3.2009.: e.g. atalkd, papd) may not be converted to
44  * use the new API and might still call
45  *
46  *    syslog_setup(int loglevel, enum logtypes logtype, int display_options, int facility);
47  *
48  * directly. These daemons are therefore limited to syslog logging. Also their
49  * loglevel can't be changed at runtime.
50  *
51  *
52  * Note:
53  * dont get confused by log_init(). It only gets called if your app
54  * forgets to setup logging before calling LOG.
55  */
56
57
58 #include <limits.h>
59 #include <stdio.h>
60
61 #include <atalk/boolean.h>
62
63 #ifdef HAVE_CONFIG_H
64 #include "config.h"
65 #endif
66
67 enum loglevels {
68     log_none,
69     log_severe,
70     log_error,
71     log_warning,
72     log_note,
73     log_info,
74     log_debug,
75     log_debug6,
76     log_debug7,
77     log_debug8,
78     log_debug9,
79     log_maxdebug
80 };
81
82 /* this is the enum specifying all availiable logtypes */
83 enum logtypes {
84   logtype_default,
85   logtype_core,
86   logtype_logger,
87   logtype_cnid,
88   logtype_afpd,
89   logtype_atalkd,
90   logtype_papd,
91   logtype_uams,
92
93   logtype_end_of_list_marker  /* don't put any logtypes after this */
94 };
95
96
97 /* Display Option flags. */
98 /* redefine these so they can don't interfeer with syslog */
99 /* these can be used in standard logging too */
100 #define logoption_nsrcinfo    0x04   /* don't log source info */
101 /* the following do not work anymore, they're only provided in order to not
102  * break existing source code */
103 #define logoption_pid         0x01   /* log the pid with each message */
104 #define logoption_cons        0x02   /* log on the console if error logging */
105 #define logoption_ndelay      0x08   /* don't delay open */
106 #define logoption_perror      0x20   /* log to stderr as well */
107 #define logoption_nfile       0x40   /* ignore the file that called the log */
108 #define logoption_nline       0x80   /* ignore the line that called the log*/
109
110 /* facility codes */
111 /* redefine these so they can don't interfeer with syslog */
112 #define logfacility_user        (1<<3)  /* random user-level messages */
113 #define logfacility_mail        (2<<3)  /* mail system */
114 #define logfacility_daemon      (3<<3)  /* system daemons */
115 #define logfacility_auth        (4<<3)  /* security/authorization messages */
116 #define logfacility_syslog      (5<<3)  /* messages generated by syslogd */
117 #define logfacility_lpr         (6<<3)  /* line printer subsystem */
118 #define logfacility_authpriv    (10<<3) /* security/auth messages (private) */
119 #define logfacility_ftp         (11<<3) /* ftp daemon */
120
121 /* =========================================================================
122     Global function decarations
123    ========================================================================= */
124
125 /*  */
126 void log_init(void);
127
128 /* Setup the level and type of log that will be logged for file loggging */
129 void log_setup(const char *filename, enum loglevels loglevel, enum logtypes logtype);
130
131 /* Setup the level and type of log that will be logged to syslog. */
132 void syslog_setup(int loglevel, enum logtypes logtype, 
133                   int display_options, int facility);
134
135 /* This gets called e.g. from afpd.conf parsing code with a string like: */
136 /* "default log_maxdebug /var/log/afpd.log" */
137 void setuplog(const char *logstr);
138
139 /* This gets called e.g. from afpd.conf parsing code with a string like: */
140 /* "default dummyname" */
141 void unsetuplog(const char *logstr);
142
143 /* finish up and close the logs */
144 void log_close(void);
145
146 /* This function sets up the ProcessName */
147 void set_processname(const char *processname);
148
149 /*
150  * How to write a LOG macro:
151  * http://c-faq.com/cpp/debugmacs.html
152  * 
153  * We choose the verbose form in favor of the obfuscated ones, its easier
154  * to parse for human beings and facilitates expanding the macro for
155  * inline checks for debug levels.
156  *
157  * How to properly enclose multistatement macros:
158  * http://en.wikipedia.org/wiki/C_macro#Multiple_statements
159  */
160
161 /* LOG macro func no.1: log the message to file */
162 void make_log_entry(enum loglevels loglevel, enum logtypes logtype, const char *file, int line, char *message, ...);
163
164 /* 
165    Note:
166    any configured file-logging deactivates syslog logging
167    log_level is always a constant with O2 a sane compiler will remove the call to
168    make_log_entry
169  */
170 #ifdef NO_DEBUG
171 #define LOG_MAX log_info
172 #else 
173 #define LOG_MAX log_maxdebug
174 #endif
175
176 #define LOG(log_level, type, ...)  \
177   do { \
178     if (log_level > LOG_MAX) \
179       break; \
180     make_log_entry((log_level), (type), __FILE__, __LINE__,  __VA_ARGS__); \
181   } while(0)  
182 #endif /* _ATALK_LOGGER_H */