]> arthur.barton.de Git - netatalk.git/blob - include/atalk/logger.h
Reworked logger
[netatalk.git] / include / atalk / logger.h
1 #ifndef _ATALK_LOGGER_H
2 #define _ATALK_LOGGER_H 1
3
4 #include <limits.h>
5 #include <stdio.h>
6
7 #include <atalk/boolean.h>
8
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif
12
13 #define MAXLOGSIZE 512
14
15 enum loglevels {
16     log_none     = 0,
17     log_severe   = 10,
18     log_error    = 20,
19     log_warning  = 30,
20     log_note     = 40,
21     log_info     = 50,
22     log_debug    = 60,
23     log_debug6   = 70,
24     log_debug7   = 80,
25     log_debug8   = 90,
26     log_debug9   = 100,
27     log_maxdebug = 110
28 };
29 #define LOGLEVEL_STRING_IDENTIFIERS { \
30   "LOG_NOTHING",                      \
31   "LOG_SEVERE",                       \
32   "LOG_ERROR",                        \
33   "LOG_WARN",                         \
34   "LOG_NOTE",                         \
35   "LOG_INFO",                         \
36   "LOG_DEBUG",                        \
37   "LOG_DEBUG6",                       \
38   "LOG_DEBUG7",                       \
39   "LOG_DEBUG8",                       \
40   "LOG_DEBUG9",                       \
41   "LOG_MAXDEBUG"}                        
42
43 /* this is the enum specifying all availiable logtypes */
44 enum logtypes {
45   logtype_default,
46   logtype_core,
47   logtype_logger,
48   logtype_cnid,
49   logtype_afpd,
50   logtype_atalkd,
51   logtype_papd,
52   logtype_uams,
53
54   logtype_end_of_list_marker  /* don't put any logtypes after this */
55 };
56
57 /* these are the string identifiers corresponding to each logtype */
58 #define LOGTYPE_STRING_IDENTIFIERS { \
59   "Default",                         \
60   "Core",                            \
61   "Logger",                          \
62   "CNID",                            \
63   "AFPDaemon",                       \
64   "ATalkDaemon",                     \
65   "PAPDaemon",                       \
66   "UAMSDaemon",                      \
67                                      \
68   "end_of_list_marker"}              \
69
70 /* Display Option flags. */
71 /* redefine these so they can don't interfeer with syslog */
72 /* these can be used in standard logging too */
73 #define logoption_nsrcinfo    0x04   /* don't log source info */
74 /* the following do not work anymore, they're only provided in order to not
75  * break existing source code */
76 #define logoption_pid         0x01   /* log the pid with each message */
77 #define logoption_cons        0x02   /* log on the console if error logging */
78 #define logoption_ndelay      0x08   /* don't delay open */
79 #define logoption_perror      0x20   /* log to stderr as well */
80 #define logoption_nfile       0x40   /* ignore the file that called the log */
81 #define logoption_nline       0x80   /* ignore the line that called the log*/
82
83 /* facility codes */
84 /* redefine these so they can don't interfeer with syslog */
85 #define logfacility_user        (1<<3)  /* random user-level messages */
86 #define logfacility_mail        (2<<3)  /* mail system */
87 #define logfacility_daemon      (3<<3)  /* system daemons */
88 #define logfacility_auth        (4<<3)  /* security/authorization messages */
89 #define logfacility_syslog      (5<<3)  /* messages generated by syslogd */
90 #define logfacility_lpr         (6<<3)  /* line printer subsystem */
91 #define logfacility_authpriv    (10<<3) /* security/auth messages (private) */
92 #define logfacility_ftp         (11<<3) /* ftp daemon */
93
94 /* ========================================================================= 
95     Structure definitions
96    ========================================================================= */
97
98 /* Main log config */
99 typedef struct {
100     int   inited;                 /* file log config initialized ? */
101     int   filelogging;            /* Any level set to filelogging ? */
102                                   /* Deactivates syslog logging */
103     char  processname[16];
104     int   syslog_opened;          /* syslog opened ? */
105     int   facility;               /* syslog facility to use */
106     int   syslog_display_options;
107     int   syslog_level;           /* Log Level to send to syslog */
108 } log_config_t;
109
110 /* This stores the config and options for one filelog type (e.g. logger, afpd etc.) */
111 typedef struct {
112     int  set;                     /* set individually ? yes: changing default
113                                    * doesnt change it. no: it changes it.*/
114     char *filename;               /* Name of file */
115     int  fd;                      /* logfiles fd */
116     int  level;                   /* Log Level to put in this file */
117     int  display_options;
118 } filelog_conf_t;
119
120 /* ========================================================================= 
121     Global variables
122    ========================================================================= */
123
124 #ifndef LOGGER_C
125 /* Make config accessible for LOG macro */
126 extern log_config_t log_config;
127 extern filelog_conf_t file_configs[logtype_end_of_list_marker];
128
129 /* These are used by the LOG macro to store __FILE__ and __LINE__ */
130 extern char *log_src_filename;
131 extern int  log_src_linenumber;
132 #endif
133
134 /* =========================================================================
135     Global function decarations
136    ========================================================================= */
137
138 /*  */
139 void log_init(void);
140
141 /* Setup the level and type of log that will be logged for file loggging */
142 bool log_setup(char *filename, enum loglevels loglevel, enum logtypes logtype);
143
144 /* Setup the level and type of log that will be logged to syslog. */
145 //void syslog_setup(enum loglevels loglevel);
146 void syslog_setup(int loglevel, enum logtypes logtype, 
147                   int display_options, int facility);
148
149 /* void setuplog(char *logsource, char *logtype, char *loglevel, char *filename); */
150 void setuplog(char *logtype, char *loglevel, char *filename);
151
152 /* finish up and close the logs */
153 void log_close();
154
155 /* This function sets up the ProcessName */
156 void set_processname(char *processname);
157
158 /*
159  * How to write a LOG macro:
160  * http://c-faq.com/cpp/debugmacs.html
161  * 
162  * We choose the verbose form in favor of the obfuscated ones, its easier
163  * to parse for human beings and facilitates expanding the macro for
164  * inline checks for debug levels.
165  *
166  * How to properly enclose multistatement macros:
167  * http://en.wikipedia.org/wiki/C_macro#Multiple_statements
168  */
169
170 /* LOG macro func no.1: log the message to file */
171 void make_log_entry(enum loglevels loglevel, enum logtypes logtype, char *message, ...);
172
173 /* LOG macro func no.2: log the message to syslog */
174 void make_syslog_entry(enum loglevels loglevel, enum logtypes logtype, char *message, ...);
175
176 /* 
177    Note:
178    any configured file-logging deactivates syslog logging
179  */
180
181 #define LOG(a,b, ...)  \
182   do { \
183     if ( ! log_config.inited) \
184       log_init(); \
185     if (file_configs[b].level >= a) \
186       log_src_filename = __FILE__, \
187       log_src_linenumber = __LINE__, \
188       make_log_entry(a, b, __VA_ARGS__); \
189     else if (( ! log_config.filelogging) && (log_config.syslog_level >= a)) \
190        make_syslog_entry(a, b, __VA_ARGS__); \
191   } while(0)  
192 #endif /* _ATALK_LOGGER_H */