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>
36 #include <atalk/boolean.h>
39 #include <atalk/logger.h>
42 #define OPEN_LOGS_AS_UID 0
44 #define COUNT_ARRAY(array) (sizeof((array))/sizeof((array)[0]))
46 /* =========================================================================
48 ========================================================================= */
50 /* Main log config container, must be globally visible */
51 log_config_t log_config = {
52 0, /* Initialized ? 0 = no */
53 0, /* No filelogging setup yet */
54 {0}, /* processname */
55 0, /* syslog opened ? */
56 logfacility_daemon, /* syslog facility to use */
57 logoption_ndelay|logoption_pid, /* logging options for syslog */
58 0 /* log level for syslog */
61 /* Default log config: log nothing to files.
62 0: not set individually
67 #define DEFAULT_LOG_CONFIG {0, NULL, -1, 0, 0}
69 filelog_conf_t file_configs[logtype_end_of_list_marker] = {
70 DEFAULT_LOG_CONFIG, /* logtype_default */
71 DEFAULT_LOG_CONFIG, /* logtype_core */
72 DEFAULT_LOG_CONFIG, /* logtype_logger */
73 DEFAULT_LOG_CONFIG, /* logtype_cnid */
74 DEFAULT_LOG_CONFIG, /* logtype_afpd */
75 DEFAULT_LOG_CONFIG, /* logtype_atalkd */
76 DEFAULT_LOG_CONFIG, /* logtype_papd */
77 DEFAULT_LOG_CONFIG /* logtype_uams */
80 /* These are used by the LOG macro to store __FILE__ and __LINE__ */
81 char *log_src_filename;
82 int log_src_linenumber;
84 /* Array to store text to list given a log type */
85 static const char *arr_logtype_strings[] = LOGTYPE_STRING_IDENTIFIERS;
86 static const int num_logtype_strings = COUNT_ARRAY(arr_logtype_strings);
88 /* Array for charachters representing log severity in the log file */
89 static const char arr_loglevel_chars[] = {'-','S', 'E', 'W', 'N', 'I', 'D'};
90 static const int num_loglevel_chars = COUNT_ARRAY(arr_loglevel_chars);
92 static const char *arr_loglevel_strings[] = LOGLEVEL_STRING_IDENTIFIERS;
93 static const int num_loglevel_strings = COUNT_ARRAY(arr_loglevel_strings);
95 /* =========================================================================
96 Internal function definitions
97 ========================================================================= */
100 * If filename == NULL its for syslog logging, otherwise its for file-logging.
101 * "unsetuplog" calls with loglevel == NULL.
102 * loglevel == NULL means:
103 * if logtype == default
106 * set to default logging
109 /* -[un]setuplog <logtype> <loglevel> [<filename>]*/
110 static void setuplog_internal(char *logtype, char *loglevel, char *filename)
112 int typenum, levelnum;
115 for( typenum=0; typenum < num_logtype_strings; typenum++) {
116 if (strcasecmp(logtype, arr_logtype_strings[typenum]) == 0)
119 if (typenum >= num_logtype_strings) {
124 if (loglevel == NULL) {
127 for(levelnum=1; levelnum < num_loglevel_strings; levelnum++) {
128 if (strcasecmp(loglevel, arr_loglevel_strings[levelnum]) == 0)
131 if (levelnum >= num_loglevel_strings) {
136 /* is this a syslog setup or a filelog setup ? */
137 if (filename == NULL) {
139 syslog_setup(levelnum, 0,
140 log_config.syslog_display_options,
141 log_config.facility);
143 /* this must be a filelog */
144 log_setup(filename, levelnum, typenum);
150 static void generate_message_details(char *message_details_buffer,
151 int message_details_buffer_length,
153 enum loglevels loglevel, enum logtypes logtype)
155 char *ptr = message_details_buffer;
157 int len = message_details_buffer_length;
165 strftime(ptr, len, "%b %d %H:%M:%S ", localtime(&thetime));
166 templen = strlen(ptr);
171 strncpy(ptr, log_config.processname, len);
172 templen = strlen(ptr);
177 pid_t pid = getpid();
178 templen = snprintf(ptr, len, "[%d]", pid);
183 if ( ! (display_options & logoption_nsrcinfo)) {
184 char *basename = strrchr(log_src_filename, '/');
186 templen = snprintf(ptr, len, " {%s:%d}", basename + 1, log_src_linenumber);
188 templen = snprintf(ptr, len, " {%s:%d}", log_src_filename, log_src_linenumber);
196 if (loglevel >= (num_loglevel_chars - 1))
197 templen = snprintf(ptr, len, " (D%d:", loglevel - 1);
199 templen = snprintf(ptr, len, " (%c:", arr_loglevel_chars[loglevel]);
204 if (logtype<num_logtype_strings) {
205 templen = snprintf(ptr, len, "%s", arr_logtype_strings[logtype]);
210 strncat(ptr, "): ", len);
213 int get_syslog_equivalent(enum loglevels loglevel)
217 /* The question is we know how bad it is for us,
218 but how should that translate in the syslogs? */
223 case 3: /* warning */
227 case 5: /* information */
234 /* =========================================================================
235 Global function definitions
236 ========================================================================= */
241 log_setup(LOGFILEPATH, log_note, logtype_default);
243 syslog_setup(log_note, 0,
244 log_config.syslog_display_options,
245 log_config.facility);
249 void log_setup(char *filename, enum loglevels loglevel, enum logtypes logtype)
255 if (file_configs[logtype].set) {
256 if (file_configs[logtype].filename) {
257 free(file_configs[logtype].filename);
258 file_configs[logtype].filename = NULL;
260 close(file_configs[logtype].fd);
261 file_configs[logtype].fd = -1;
262 file_configs[logtype].level = 0;
263 file_configs[logtype].set = 0;
265 /* if disabling default also set all "default using" levels to 0 */
266 if (logtype == logtype_default) {
267 while (logtype != logtype_end_of_list_marker) {
268 if ( ! (file_configs[logtype].set))
269 file_configs[logtype].level = 0;
279 if (NULL == filename)
282 /* Resetting existing config ? */
283 if (file_configs[logtype].set && file_configs[logtype].filename) {
284 free(file_configs[logtype].filename);
285 file_configs[logtype].filename = NULL;
286 close(file_configs[logtype].fd);
287 file_configs[logtype].fd = -1;
288 file_configs[logtype].level = 0;
289 file_configs[logtype].set = 0;
293 file_configs[logtype].filename = strdup(filename);
294 file_configs[logtype].level = loglevel;
297 /* Open log file as OPEN_LOGS_AS_UID*/
298 process_uid = geteuid();
300 seteuid(OPEN_LOGS_AS_UID);
301 file_configs[logtype].fd = open( file_configs[logtype].filename,
302 O_CREAT | O_WRONLY | O_APPEND,
303 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
305 seteuid(process_uid);
307 /* Check for error opening/creating logfile */
308 if (-1 == file_configs[logtype].fd) {
309 free(file_configs[logtype].filename);
310 file_configs[logtype].filename = NULL;
311 file_configs[logtype].level = -1;
312 file_configs[logtype].set = 0;
316 fcntl(file_configs[logtype].fd, F_SETFD, FD_CLOEXEC);
317 file_configs[logtype].set = 1;
318 log_config.filelogging = 1;
319 log_config.inited = 1;
321 /* Here's how we make it possible to LOG to a logtype like "logtype_afpd" */
322 /* which then uses the default logtype setup if it isn't setup itself: */
323 /* we just copy the loglevel from default to all logtypes that are not setup. */
324 /* In "make_log_entry" we then check for the logtypes if they arent setup */
325 /* and use default then. We must provide accessible values for all logtypes */
326 /* in order to make it easy and fast to check the loglevels in the LOG macro! */
328 if (logtype == logtype_default) {
329 while (logtype != logtype_end_of_list_marker) {
330 if ( ! (file_configs[logtype].set))
331 file_configs[logtype].level = loglevel;
334 logtype = logtype_default;
337 LOG(log_note, logtype_logger, "Setup file logging: type: %s, level: %s, file: %s",
338 arr_logtype_strings[logtype], arr_loglevel_strings[loglevel], file_configs[logtype].filename);
341 /* logtype is ignored, it's just one for all */
342 void syslog_setup(int loglevel, enum logtypes logtype _U_,
343 int display_options, int facility)
345 log_config.syslog_level = loglevel;
346 log_config.syslog_display_options = display_options;
347 log_config.facility = facility;
349 log_config.inited = 1;
351 LOG(log_note, logtype_logger, "Set syslog logging to level: %s",
352 arr_loglevel_strings[loglevel]);
359 /* This function sets up the processname */
360 void set_processname(const char *processname)
362 strncpy(log_config.processname, processname, 15);
363 log_config.processname[15] = 0;
366 /* -------------------------------------------------------------------------
367 make_log_entry has 1 main flaws:
368 The message in its entirity, must fit into the tempbuffer.
369 So it must be shorter than MAXLOGSIZE
370 ------------------------------------------------------------------------- */
371 void make_log_entry(enum loglevels loglevel, enum logtypes logtype,
374 /* fn is not reentrant but is used in signal handler
375 * with LOGGER it's a little late source name and line number
376 * are already changed. */
377 static int inlog = 0;
379 char temp_buffer[MAXLOGSIZE];
380 char log_details_buffer[MAXLOGSIZE];
388 /* Initialise the Messages */
389 va_start(args, message);
390 len = vsnprintf(temp_buffer, MAXLOGSIZE - 1, message, args);
394 if (len >= MAXLOGSIZE)
395 /* vsnprintf hit the buffer size*/
396 temp_buffer[MAXLOGSIZE-2] = '\n';
398 temp_buffer[len] = '\n';
399 temp_buffer[len+1] = 0;
402 generate_message_details(log_details_buffer, sizeof(log_details_buffer),
403 file_configs[logtype].set ?
404 file_configs[logtype].display_options :
405 file_configs[logtype_default].display_options,
408 /* Check if requested logtype is setup */
409 if (file_configs[logtype].set)
411 fd = file_configs[logtype].fd;
413 /* No: use default */
414 fd = file_configs[logtype_default].fd;
416 /* If default wasnt setup its fd is -1 */
418 iov[0].iov_base = log_details_buffer;
419 iov[0].iov_len = strlen(log_details_buffer);
420 iov[1].iov_base = temp_buffer;
421 iov[1].iov_len = strlen(temp_buffer);
428 /* Called by the LOG macro for syslog messages */
429 void make_syslog_entry(enum loglevels loglevel, enum logtypes logtype, char *message, ...)
432 char log_buffer[MAXLOGSIZE];
433 /* fn is not reentrant but is used in signal handler
434 * with LOGGER it's a little late source name and line number
435 * are already changed.
437 static int inlog = 0;
443 if ( ! (log_config.syslog_opened) ) {
444 openlog(log_config.processname, log_config.syslog_display_options,
445 log_config.facility);
446 log_config.syslog_opened = 1;
449 /* Initialise the Messages */
450 va_start(args, message);
451 vsnprintf(log_buffer, sizeof(log_buffer), message, args);
454 syslog(get_syslog_equivalent(loglevel), "%s", log_buffer);
459 void setuplog(const char *logstr)
461 char *ptr, *logtype, *loglevel, *filename;
462 ptr = strdup(logstr);
468 ptr = strpbrk(ptr, " \t");
471 while (*ptr && isspace(*ptr))
476 ptr = strpbrk(ptr, " \t");
479 while (*ptr && isspace(*ptr))
485 /* finally call setuplog, filename can be NULL */
486 setuplog_internal(logtype, loglevel, filename);
491 void unsetuplog(const char *logstr)
493 char *str, *logtype, *filename;
495 str = strdup(logstr);
500 /* get filename, can be NULL */
502 filename = strtok(NULL, " \t");
504 /* finally call setuplog, filename can be NULL */
505 setuplog_internal(str, NULL, filename);