]> arthur.barton.de Git - netatalk.git/blob - include/atalk/logger.h
Fix stupid free. Still banging my head against the table! Praise valgrind.
[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 #define MAXLOGSIZE 512
68
69 enum loglevels {
70     log_none,
71     log_severe,
72     log_error,
73     log_warning,
74     log_note,
75     log_info,
76     log_debug,
77     log_debug6,
78     log_debug7,
79     log_debug8,
80     log_debug9,
81     log_maxdebug
82 };
83 #define LOGLEVEL_STRING_IDENTIFIERS { \
84   "LOG_NOTHING",                      \
85   "LOG_SEVERE",                       \
86   "LOG_ERROR",                        \
87   "LOG_WARN",                         \
88   "LOG_NOTE",                         \
89   "LOG_INFO",                         \
90   "LOG_DEBUG",                        \
91   "LOG_DEBUG6",                       \
92   "LOG_DEBUG7",                       \
93   "LOG_DEBUG8",                       \
94   "LOG_DEBUG9",                       \
95   "LOG_MAXDEBUG"}                        
96
97 /* this is the enum specifying all availiable logtypes */
98 enum logtypes {
99   logtype_default,
100   logtype_core,
101   logtype_logger,
102   logtype_cnid,
103   logtype_afpd,
104   logtype_atalkd,
105   logtype_papd,
106   logtype_uams,
107
108   logtype_end_of_list_marker  /* don't put any logtypes after this */
109 };
110
111 /* these are the string identifiers corresponding to each logtype */
112 #define LOGTYPE_STRING_IDENTIFIERS { \
113   "Default",                         \
114   "Core",                            \
115   "Logger",                          \
116   "CNID",                            \
117   "AFPDaemon",                       \
118   "ATalkDaemon",                     \
119   "PAPDaemon",                       \
120   "UAMSDaemon",                      \
121                                      \
122   "end_of_list_marker"}              \
123
124 /* Display Option flags. */
125 /* redefine these so they can don't interfeer with syslog */
126 /* these can be used in standard logging too */
127 #define logoption_nsrcinfo    0x04   /* don't log source info */
128 /* the following do not work anymore, they're only provided in order to not
129  * break existing source code */
130 #define logoption_pid         0x01   /* log the pid with each message */
131 #define logoption_cons        0x02   /* log on the console if error logging */
132 #define logoption_ndelay      0x08   /* don't delay open */
133 #define logoption_perror      0x20   /* log to stderr as well */
134 #define logoption_nfile       0x40   /* ignore the file that called the log */
135 #define logoption_nline       0x80   /* ignore the line that called the log*/
136
137 /* facility codes */
138 /* redefine these so they can don't interfeer with syslog */
139 #define logfacility_user        (1<<3)  /* random user-level messages */
140 #define logfacility_mail        (2<<3)  /* mail system */
141 #define logfacility_daemon      (3<<3)  /* system daemons */
142 #define logfacility_auth        (4<<3)  /* security/authorization messages */
143 #define logfacility_syslog      (5<<3)  /* messages generated by syslogd */
144 #define logfacility_lpr         (6<<3)  /* line printer subsystem */
145 #define logfacility_authpriv    (10<<3) /* security/auth messages (private) */
146 #define logfacility_ftp         (11<<3) /* ftp daemon */
147
148 /* ========================================================================= 
149     Structure definitions
150    ========================================================================= */
151
152 /* Main log config */
153 typedef struct {
154     int   inited;                 /* file log config initialized ? */
155     int   filelogging;            /* Any level set to filelogging ? */
156                                   /* Deactivates syslog logging */
157     char  processname[16];
158     int   syslog_opened;          /* syslog opened ? */
159     int   facility;               /* syslog facility to use */
160     int   syslog_display_options;
161     int   syslog_level;           /* Log Level to send to syslog */
162 } log_config_t;
163
164 /* This stores the config and options for one filelog type (e.g. logger, afpd etc.) */
165 typedef struct {
166     int  set;                     /* set individually ? yes: changing default
167                                    * doesnt change it. no: it changes it.*/
168     char *filename;               /* Name of file */
169     int  fd;                      /* logfiles fd */
170     int  level;                   /* Log Level to put in this file */
171     int  display_options;
172 } filelog_conf_t;
173
174 /* ========================================================================= 
175     Global variables
176    ========================================================================= */
177
178 #ifndef LOGGER_C
179 /* Make config accessible for LOG macro */
180 extern log_config_t log_config;
181 extern filelog_conf_t file_configs[logtype_end_of_list_marker];
182
183 /* These are used by the LOG macro to store __FILE__ and __LINE__ */
184 extern char *log_src_filename;
185 extern int  log_src_linenumber;
186 #endif
187
188 /* =========================================================================
189     Global function decarations
190    ========================================================================= */
191
192 /*  */
193 void log_init(void);
194
195 /* Setup the level and type of log that will be logged for file loggging */
196 void log_setup(const char *filename, enum loglevels loglevel, enum logtypes logtype);
197
198 /* Setup the level and type of log that will be logged to syslog. */
199 void syslog_setup(int loglevel, enum logtypes logtype, 
200                   int display_options, int facility);
201
202 /* This gets called e.g. from afpd.conf parsing code with a string like: */
203 /* "default log_maxdebug /var/log/afpd.log" */
204 void setuplog(const char *logstr);
205
206 /* This gets called e.g. from afpd.conf parsing code with a string like: */
207 /* "default dummyname" */
208 void unsetuplog(const char *logstr);
209
210 /* finish up and close the logs */
211 void log_close();
212
213 /* This function sets up the ProcessName */
214 void set_processname(const char *processname);
215
216 /*
217  * How to write a LOG macro:
218  * http://c-faq.com/cpp/debugmacs.html
219  * 
220  * We choose the verbose form in favor of the obfuscated ones, its easier
221  * to parse for human beings and facilitates expanding the macro for
222  * inline checks for debug levels.
223  *
224  * How to properly enclose multistatement macros:
225  * http://en.wikipedia.org/wiki/C_macro#Multiple_statements
226  */
227
228 /* LOG macro func no.1: log the message to file */
229 void make_log_entry(enum loglevels loglevel, enum logtypes logtype, char *message, ...);
230
231 /* LOG macro func no.2: log the message to syslog */
232 void make_syslog_entry(enum loglevels loglevel, enum logtypes logtype, char *message, ...);
233
234 /* 
235    Note:
236    any configured file-logging deactivates syslog logging
237  */
238
239 #define LOG(a,b, ...)  \
240   do { \
241     if ( ! log_config.inited) \
242       log_init(); \
243     if (file_configs[b].level >= a) \
244       log_src_filename = __FILE__, \
245       log_src_linenumber = __LINE__, \
246       make_log_entry(a, b, __VA_ARGS__); \
247     else if (( ! log_config.filelogging) && (log_config.syslog_level >= a)) \
248        make_syslog_entry(a, b, __VA_ARGS__); \
249   } while(0)  
250 #endif /* _ATALK_LOGGER_H */