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