1 #ifndef _ATALK_LOGGER_H
2 #define _ATALK_LOGGER_H 1
5 * logger LOG Macro Usage
6 * ======================
8 * LOG(<logtype>, <loglevel>, "<string>"[, args]);
17 * setuplog(char *confstring)
18 * confstring = "<logtype> <loglevel> [<filename>]"
20 * Calling without <filename> configures basic logging to syslog. Specifying <filename>
21 * configures extended logging to <filename>.
23 * You can later disable logging by calling
25 * unsetuplog(char *confstring)
26 * confstring = "<logtype> [<any_string>]"
28 * Calling without <any_string> disables syslog logging, calling with <any_string>
29 * disables file logging.
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
37 * setuplog("default log_debug /var/log/debug.log");
38 * See also libatalk/util/test/logger_test.c
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
46 * syslog_setup(int loglevel, enum logtypes logtype, int display_options, int facility);
48 * directly. These daemons are therefore limited to syslog logging. Also their
49 * loglevel can't be changed at runtime.
53 * dont get confused by log_init(). It only gets called if your app
54 * forgets to setup logging before calling LOG.
61 #include <atalk/boolean.h>
67 #define MAXLOGSIZE 512
83 #define LOGLEVEL_STRING_IDENTIFIERS { \
97 /* this is the enum specifying all availiable logtypes */
108 logtype_end_of_list_marker /* don't put any logtypes after this */
111 /* these are the string identifiers corresponding to each logtype */
112 #define LOGTYPE_STRING_IDENTIFIERS { \
122 "end_of_list_marker"} \
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*/
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 */
148 /* =========================================================================
149 Structure definitions
150 ========================================================================= */
152 /* Main log config */
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 */
164 /* This stores the config and options for one filelog type (e.g. logger, afpd etc.) */
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 */
174 /* =========================================================================
176 ========================================================================= */
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];
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;
188 /* =========================================================================
189 Global function decarations
190 ========================================================================= */
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);
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);
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);
206 /* This gets called e.g. from afpd.conf parsing code with a string like: */
207 /* "default dummyname" */
208 void unsetuplog(const char *logstr);
210 /* finish up and close the logs */
213 /* This function sets up the ProcessName */
214 void set_processname(const char *processname);
217 * How to write a LOG macro:
218 * http://c-faq.com/cpp/debugmacs.html
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.
224 * How to properly enclose multistatement macros:
225 * http://en.wikipedia.org/wiki/C_macro#Multiple_statements
228 /* LOG macro func no.1: log the message to file */
229 void make_log_entry(enum loglevels loglevel, enum logtypes logtype, char *message, ...);
231 /* LOG macro func no.2: log the message to syslog */
232 void make_syslog_entry(enum loglevels loglevel, enum logtypes logtype, char *message, ...);
236 any configured file-logging deactivates syslog logging
239 #define LOG(a,b, ...) \
241 if ( ! log_config.inited) \
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__); \
250 #endif /* _ATALK_LOGGER_H */