5 /* =========================================================================
7 logger.c is part of the utils section in the libatalk library,
8 which is part of the netatalk project.
10 logger.c was written by Simon Bazley (sibaz@sibaz.com)
12 I believe libatalk is released under the L/GPL licence.
13 Just incase, it is, thats the licence I'm applying to this file.
16 ==========================================================================
18 Logger.c is intended as an alternative to syslog for logging
20 ========================================================================= */
28 #include <sys/types.h>
37 #include <atalk/boolean.h>
40 #include <atalk/logger.h>
43 #define OPEN_LOGS_AS_UID 0
45 #define COUNT_ARRAY(array) (sizeof((array))/sizeof((array)[0]))
47 /* =========================================================================
49 ========================================================================= */
51 /* Main log config container, must be globally visible */
52 log_config_t log_config = {
53 0, /* Initialized ? 0 = no */
54 0, /* No filelogging setup yet */
55 {0}, /* processname */
56 0, /* syslog opened ? */
57 logfacility_daemon, /* syslog facility to use */
58 logoption_ndelay|logoption_pid, /* logging options for syslog */
59 0 /* log level for syslog */
62 /* Default log config: log nothing to files.
63 0: not set individually
68 #define DEFAULT_LOG_CONFIG {0, NULL, -1, 0, 0}
70 filelog_conf_t file_configs[logtype_end_of_list_marker] = {
71 DEFAULT_LOG_CONFIG, /* logtype_default */
72 DEFAULT_LOG_CONFIG, /* logtype_core */
73 DEFAULT_LOG_CONFIG, /* logtype_logger */
74 DEFAULT_LOG_CONFIG, /* logtype_cnid */
75 DEFAULT_LOG_CONFIG, /* logtype_afpd */
76 DEFAULT_LOG_CONFIG, /* logtype_atalkd */
77 DEFAULT_LOG_CONFIG, /* logtype_papd */
78 DEFAULT_LOG_CONFIG /* logtype_uams */
81 /* These are used by the LOG macro to store __FILE__ and __LINE__ */
82 char *log_src_filename;
83 int log_src_linenumber;
85 /* Array to store text to list given a log type */
86 static const char *arr_logtype_strings[] = LOGTYPE_STRING_IDENTIFIERS;
87 static const int num_logtype_strings = COUNT_ARRAY(arr_logtype_strings);
89 /* Array for charachters representing log severity in the log file */
90 static const char arr_loglevel_chars[] = {'-','S', 'E', 'W', 'N', 'I', 'D'};
91 static const int num_loglevel_chars = COUNT_ARRAY(arr_loglevel_chars);
93 static const char *arr_loglevel_strings[] = LOGLEVEL_STRING_IDENTIFIERS;
94 static const int num_loglevel_strings = COUNT_ARRAY(arr_loglevel_strings);
96 /* =========================================================================
97 Internal function definitions
98 ========================================================================= */
101 * If filename == NULL its for syslog logging, otherwise its for file-logging.
102 * "unsetuplog" calls with loglevel == NULL.
103 * loglevel == NULL means:
104 * if logtype == default
107 * set to default logging
110 /* -[un]setuplog <logtype> <loglevel> [<filename>]*/
111 static void setuplog_internal(const char *logtype, const char *loglevel, const char *filename)
113 int typenum, levelnum;
116 for( typenum=0; typenum < num_logtype_strings; typenum++) {
117 if (strcasecmp(logtype, arr_logtype_strings[typenum]) == 0)
120 if (typenum >= num_logtype_strings) {
125 if (loglevel == NULL) {
128 for(levelnum=1; levelnum < num_loglevel_strings; levelnum++) {
129 if (strcasecmp(loglevel, arr_loglevel_strings[levelnum]) == 0)
132 if (levelnum >= num_loglevel_strings) {
137 /* is this a syslog setup or a filelog setup ? */
138 if (filename == NULL) {
140 syslog_setup(levelnum, 0,
141 log_config.syslog_display_options,
142 log_config.facility);
144 /* this must be a filelog */
145 log_setup(filename, levelnum, typenum);
151 static void generate_message_details(char *message_details_buffer,
152 int message_details_buffer_length,
154 enum loglevels loglevel, enum logtypes logtype)
156 char *ptr = message_details_buffer;
158 int len = message_details_buffer_length;
164 gettimeofday(&tv, NULL);
165 strftime(ptr, len, "%b %d %H:%M:%S.", localtime(&tv.tv_sec));
166 templen = strlen(ptr);
170 templen = snprintf(ptr, len, "%06u ", (int)tv.tv_usec);
175 strncpy(ptr, log_config.processname, len);
176 templen = strlen(ptr);
181 pid_t pid = getpid();
182 templen = snprintf(ptr, len, "[%d]", pid);
187 if ( ! (display_options & logoption_nsrcinfo)) {
188 char *basename = strrchr(log_src_filename, '/');
190 templen = snprintf(ptr, len, " {%s:%d}", basename + 1, log_src_linenumber);
192 templen = snprintf(ptr, len, " {%s:%d}", log_src_filename, log_src_linenumber);
200 if (loglevel >= (num_loglevel_chars - 1))
201 templen = snprintf(ptr, len, " (D%d:", loglevel - 1);
203 templen = snprintf(ptr, len, " (%c:", arr_loglevel_chars[loglevel]);
208 if (logtype<num_logtype_strings) {
209 templen = snprintf(ptr, len, "%s", arr_logtype_strings[logtype]);
214 strncat(ptr, "): ", len);
217 int get_syslog_equivalent(enum loglevels loglevel)
221 /* The question is we know how bad it is for us,
222 but how should that translate in the syslogs? */
227 case 3: /* warning */
231 case 5: /* information */
238 /* =========================================================================
239 Global function definitions
240 ========================================================================= */
245 log_setup(LOGFILEPATH, log_note, logtype_default);
247 syslog_setup(log_note, 0,
248 log_config.syslog_display_options,
249 log_config.facility);
253 void log_setup(const char *filename, enum loglevels loglevel, enum logtypes logtype)
259 if (file_configs[logtype].set) {
260 if (file_configs[logtype].filename) {
261 free(file_configs[logtype].filename);
262 file_configs[logtype].filename = NULL;
264 close(file_configs[logtype].fd);
265 file_configs[logtype].fd = -1;
266 file_configs[logtype].level = 0;
267 file_configs[logtype].set = 0;
269 /* if disabling default also set all "default using" levels to 0 */
270 if (logtype == logtype_default) {
271 while (logtype != logtype_end_of_list_marker) {
272 if ( ! (file_configs[logtype].set))
273 file_configs[logtype].level = 0;
283 if (NULL == filename)
286 /* Resetting existing config ? */
287 if (file_configs[logtype].set && file_configs[logtype].filename) {
288 free(file_configs[logtype].filename);
289 file_configs[logtype].filename = NULL;
290 close(file_configs[logtype].fd);
291 file_configs[logtype].fd = -1;
292 file_configs[logtype].level = 0;
293 file_configs[logtype].set = 0;
297 file_configs[logtype].filename = strdup(filename);
298 file_configs[logtype].level = loglevel;
301 /* Open log file as OPEN_LOGS_AS_UID*/
302 process_uid = geteuid();
304 seteuid(OPEN_LOGS_AS_UID);
305 file_configs[logtype].fd = open( file_configs[logtype].filename,
306 O_CREAT | O_WRONLY | O_APPEND,
307 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
309 seteuid(process_uid);
311 /* Check for error opening/creating logfile */
312 if (-1 == file_configs[logtype].fd) {
313 free(file_configs[logtype].filename);
314 file_configs[logtype].filename = NULL;
315 file_configs[logtype].level = -1;
316 file_configs[logtype].set = 0;
320 fcntl(file_configs[logtype].fd, F_SETFD, FD_CLOEXEC);
321 file_configs[logtype].set = 1;
322 log_config.filelogging = 1;
323 log_config.inited = 1;
325 /* Here's how we make it possible to LOG to a logtype like "logtype_afpd" */
326 /* which then uses the default logtype setup if it isn't setup itself: */
327 /* we just copy the loglevel from default to all logtypes that are not setup. */
328 /* In "make_log_entry" we then check for the logtypes if they arent setup */
329 /* and use default then. We must provide accessible values for all logtypes */
330 /* in order to make it easy and fast to check the loglevels in the LOG macro! */
332 if (logtype == logtype_default) {
333 while (logtype != logtype_end_of_list_marker) {
334 if ( ! (file_configs[logtype].set))
335 file_configs[logtype].level = loglevel;
338 logtype = logtype_default;
341 LOG(log_debug, logtype_logger, "Setup file logging: type: %s, level: %s, file: %s",
342 arr_logtype_strings[logtype], arr_loglevel_strings[loglevel], file_configs[logtype].filename);
345 /* logtype is ignored, it's just one for all */
346 void syslog_setup(int loglevel, enum logtypes logtype _U_,
347 int display_options, int facility)
349 log_config.syslog_level = loglevel;
350 log_config.syslog_display_options = display_options;
351 log_config.facility = facility;
353 log_config.inited = 1;
355 LOG(log_note, logtype_logger, "Set syslog logging to level: %s",
356 arr_loglevel_strings[loglevel]);
363 /* This function sets up the processname */
364 void set_processname(const char *processname)
366 strncpy(log_config.processname, processname, 15);
367 log_config.processname[15] = 0;
370 /* -------------------------------------------------------------------------
371 make_log_entry has 1 main flaws:
372 The message in its entirity, must fit into the tempbuffer.
373 So it must be shorter than MAXLOGSIZE
374 ------------------------------------------------------------------------- */
375 void make_log_entry(enum loglevels loglevel, enum logtypes logtype,
378 /* fn is not reentrant but is used in signal handler
379 * with LOGGER it's a little late source name and line number
380 * are already changed. */
381 static int inlog = 0;
383 char temp_buffer[MAXLOGSIZE];
384 char log_details_buffer[MAXLOGSIZE];
392 /* Initialise the Messages */
393 va_start(args, message);
394 len = vsnprintf(temp_buffer, MAXLOGSIZE - 1, message, args);
398 if (len >= MAXLOGSIZE)
399 /* vsnprintf hit the buffer size*/
400 temp_buffer[MAXLOGSIZE-2] = '\n';
402 temp_buffer[len] = '\n';
403 temp_buffer[len+1] = 0;
406 generate_message_details(log_details_buffer, sizeof(log_details_buffer),
407 file_configs[logtype].set ?
408 file_configs[logtype].display_options :
409 file_configs[logtype_default].display_options,
412 /* Check if requested logtype is setup */
413 if (file_configs[logtype].set)
415 fd = file_configs[logtype].fd;
417 /* No: use default */
418 fd = file_configs[logtype_default].fd;
420 /* If default wasnt setup its fd is -1 */
422 iov[0].iov_base = log_details_buffer;
423 iov[0].iov_len = strlen(log_details_buffer);
424 iov[1].iov_base = temp_buffer;
425 iov[1].iov_len = strlen(temp_buffer);
432 /* Called by the LOG macro for syslog messages */
433 void make_syslog_entry(enum loglevels loglevel, enum logtypes logtype, char *message, ...)
436 char log_buffer[MAXLOGSIZE];
437 /* fn is not reentrant but is used in signal handler
438 * with LOGGER it's a little late source name and line number
439 * are already changed.
441 static int inlog = 0;
447 if ( ! (log_config.syslog_opened) ) {
448 openlog(log_config.processname, log_config.syslog_display_options,
449 log_config.facility);
450 log_config.syslog_opened = 1;
453 /* Initialise the Messages */
454 va_start(args, message);
455 vsnprintf(log_buffer, sizeof(log_buffer), message, args);
458 syslog(get_syslog_equivalent(loglevel), "%s", log_buffer);
463 void setuplog(const char *logstr)
465 char *ptr, *ptrbak, *logtype, *loglevel, *filename;
466 ptr = strdup(logstr);
473 ptr = strpbrk(ptr, " \t");
476 while (*ptr && isspace(*ptr))
481 ptr = strpbrk(ptr, " \t");
484 while (*ptr && isspace(*ptr))
490 /* finally call setuplog, filename can be NULL */
491 setuplog_internal(logtype, loglevel, filename);
496 void unsetuplog(const char *logstr)
498 char *str, *logtype, *filename;
500 str = strdup(logstr);
505 /* get filename, can be NULL */
507 filename = strtok(NULL, " \t");
509 /* finally call setuplog, filename can be NULL */
510 setuplog_internal(str, NULL, filename);