]> arthur.barton.de Git - netatalk.git/blob - include/atalk/logger.h
93322f297a496c96d9db32f620b1846cd0fdbe21
[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 /* logger is used by pam modules */
68 #ifndef UAM_MODULE_EXPORT
69 #define UAM_MODULE_EXPORT 
70 #endif
71
72 enum loglevels {
73     log_none,
74     log_severe,
75     log_error,
76     log_warning,
77     log_note,
78     log_info,
79     log_debug,
80     log_debug6,
81     log_debug7,
82     log_debug8,
83     log_debug9,
84     log_maxdebug
85 };
86
87 /* this is the enum specifying all availiable logtypes */
88 enum logtypes {
89   logtype_default,
90   logtype_core,
91   logtype_logger,
92   logtype_cnid,
93   logtype_afpd,
94   logtype_atalkd,
95   logtype_papd,
96   logtype_uams,
97   logtype_console,
98   logtype_end_of_list_marker  /* don't put any logtypes after this */
99 };
100
101
102 /* Display Option flags. */
103 /* redefine these so they can don't interfeer with syslog */
104 /* these can be used in standard logging too */
105 #define logoption_nsrcinfo    0x04   /* don't log source info */
106 /* the following do not work anymore, they're only provided in order to not
107  * break existing source code */
108 #define logoption_pid         0x01   /* log the pid with each message */
109 #define logoption_cons        0x02   /* log on the console if error logging */
110 #define logoption_ndelay      0x08   /* don't delay open */
111 #define logoption_perror      0x20   /* log to stderr as well */
112 #define logoption_nfile       0x40   /* ignore the file that called the log */
113 #define logoption_nline       0x80   /* ignore the line that called the log*/
114
115 /* facility codes */
116 /* redefine these so they can don't interfeer with syslog */
117 #define logfacility_user        (1<<3)  /* random user-level messages */
118 #define logfacility_mail        (2<<3)  /* mail system */
119 #define logfacility_daemon      (3<<3)  /* system daemons */
120 #define logfacility_auth        (4<<3)  /* security/authorization messages */
121 #define logfacility_syslog      (5<<3)  /* messages generated by syslogd */
122 #define logfacility_lpr         (6<<3)  /* line printer subsystem */
123 #define logfacility_authpriv    (10<<3) /* security/auth messages (private) */
124 #define logfacility_ftp         (11<<3) /* ftp daemon */
125
126 /* =========================================================================
127     Global function decarations
128    ========================================================================= */
129
130 /*  */
131 void log_init(void);
132
133 /* Setup the level and type of log that will be logged for file loggging */
134 void log_setup(const char *filename, enum loglevels loglevel, enum logtypes logtype);
135
136 /* Setup the level and type of log that will be logged to syslog. */
137 void syslog_setup(int loglevel, enum logtypes logtype, 
138                   int display_options, int facility);
139
140 /* This gets called e.g. from afpd.conf parsing code with a string like: */
141 /* "default log_maxdebug /var/log/afpd.log" */
142 void setuplog(const char *logstr);
143
144 /* This gets called e.g. from afpd.conf parsing code with a string like: */
145 /* "default dummyname" */
146 void unsetuplog(const char *logstr);
147
148 /* finish up and close the logs */
149 void log_close(void);
150
151 /* This function sets up the ProcessName */
152 void set_processname(const char *processname);
153
154 /*
155  * How to write a LOG macro:
156  * http://c-faq.com/cpp/debugmacs.html
157  * 
158  * We choose the verbose form in favor of the obfuscated ones, its easier
159  * to parse for human beings and facilitates expanding the macro for
160  * inline checks for debug levels.
161  *
162  * How to properly enclose multistatement macros:
163  * http://en.wikipedia.org/wiki/C_macro#Multiple_statements
164  */
165
166 /* LOG macro func no.1: log the message to file */
167 UAM_MODULE_EXPORT  void make_log_entry(enum loglevels loglevel, enum logtypes logtype, const char *file, int line, char *message, ...);
168
169 /* 
170    Note:
171    any configured file-logging deactivates syslog logging
172    log_level is always a constant with O2 a sane compiler will remove the call to
173    make_log_entry
174  */
175 #ifdef NO_DEBUG
176 #define LOG_MAX log_info
177 #else 
178 #define LOG_MAX log_maxdebug
179 #endif
180
181 #define LOG(log_level, type, ...)  \
182   do { \
183     if (log_level > LOG_MAX) \
184       break; \
185     make_log_entry((log_level), (type), __FILE__, __LINE__,  __VA_ARGS__); \
186   } while(0)  
187 #endif /* _ATALK_LOGGER_H */