]> arthur.barton.de Git - netatalk.git/blobdiff - libatalk/util/logger.c
afpd set the logger default to log_info and move some log msgs to log_debug
[netatalk.git] / libatalk / util / logger.c
index 97fd32a20d169e0d39da175be268ce2cc248ae06..deb308a010a3068135b15086a828ac2547561c24 100644 (file)
@@ -26,30 +26,87 @@ Netatalk 2001 (c)
 #include <sys/time.h>
 #include <time.h>
 #include <ctype.h>
+#include <errno.h>
 
 #include <atalk/boolean.h>
+#include <atalk/util.h>
 
-#define LOGGER_C
 #include <atalk/logger.h>
-#undef LOGGER_C
 
 #define OPEN_LOGS_AS_UID 0
 
 #define COUNT_ARRAY(array) (sizeof((array))/sizeof((array)[0]))
 
+#define MAXLOGSIZE 512
+
+#define LOGLEVEL_STRING_IDENTIFIERS { \
+  "LOG_NOTHING",                      \
+  "LOG_SEVERE",                       \
+  "LOG_ERROR",                        \
+  "LOG_WARN",                         \
+  "LOG_NOTE",                         \
+  "LOG_INFO",                         \
+  "LOG_DEBUG",                        \
+  "LOG_DEBUG6",                       \
+  "LOG_DEBUG7",                       \
+  "LOG_DEBUG8",                       \
+  "LOG_DEBUG9",                       \
+  "LOG_MAXDEBUG"}                        
+
+/* these are the string identifiers corresponding to each logtype */
+#define LOGTYPE_STRING_IDENTIFIERS { \
+  "Default",                         \
+  "Core",                            \
+  "Logger",                          \
+  "CNID",                            \
+  "AFPDaemon",                       \
+  "ATalkDaemon",                     \
+  "PAPDaemon",                       \
+  "UAMSDaemon",                      \
+  "Console",                         \
+  "end_of_list_marker"}              \
+
+/* ========================================================================= 
+    Structure definitions
+   ========================================================================= */
+
+/* Main log config */
+typedef struct {
+    int            inited;                /* file log config initialized ? */
+    int            filelogging;                   /* Any level set to filelogging ? */
+                                           /* Deactivates syslog logging */
+    char           processname[16];
+    int            syslog_opened;         /* syslog opened ? */
+    int            facility;               /* syslog facility to use */
+    int            syslog_display_options;
+    enum loglevels syslog_level;           /* Log Level to send to syslog */
+    int            console;                /* if logging to console from a cli util */
+} log_config_t;
+
+/* This stores the config and options for one filelog type (e.g. logger, afpd etc.) */
+typedef struct {
+    int            set;                  /* set individually ? yes: changing default
+                                  * doesnt change it. no: it changes it.*/
+    char           *filename;     /* Name of file */
+    int            fd;            /* logfiles fd */
+    enum loglevels level;         /* Log Level to put in this file */
+    int            display_options;
+} filelog_conf_t;
+
 /* =========================================================================
    Config
    ========================================================================= */
 
-/* Main log config container, must be globally visible */
-log_config_t log_config = {
+/* Main log config container */
+static log_config_t log_config = {
     0,                  /* Initialized ? 0 = no */
     0,                  /* No filelogging setup yet */
     {0},                /* processname */
     0,                  /* syslog opened ? */
     logfacility_daemon,         /* syslog facility to use */
     logoption_ndelay|logoption_pid, /* logging options for syslog */
-    0                               /* log level for syslog */
+    0,                              /* log level for syslog */
+    0                               /* not logging to console */
 };
 
 /* Default log config: log nothing to files.
@@ -60,7 +117,7 @@ log_config_t log_config = {
    0:    Display options */
 #define DEFAULT_LOG_CONFIG {0, NULL, -1, 0, 0}
 
-filelog_conf_t file_configs[logtype_end_of_list_marker] = {
+static filelog_conf_t file_configs[logtype_end_of_list_marker] = {
     DEFAULT_LOG_CONFIG, /* logtype_default */
     DEFAULT_LOG_CONFIG, /* logtype_core */
     DEFAULT_LOG_CONFIG, /* logtype_logger */
@@ -68,23 +125,24 @@ filelog_conf_t file_configs[logtype_end_of_list_marker] = {
     DEFAULT_LOG_CONFIG, /* logtype_afpd */
     DEFAULT_LOG_CONFIG, /* logtype_atalkd */
     DEFAULT_LOG_CONFIG, /* logtype_papd */
-    DEFAULT_LOG_CONFIG  /* logtype_uams */
+    DEFAULT_LOG_CONFIG, /* logtype_uams */
+    DEFAULT_LOG_CONFIG  /* logtype_console */
 };
 
 /* These are used by the LOG macro to store __FILE__ and __LINE__ */
-char *log_src_filename;
-int  log_src_linenumber;
+static const char *log_src_filename;
+static int  log_src_linenumber;
 
 /* Array to store text to list given a log type */
 static const char *arr_logtype_strings[] =  LOGTYPE_STRING_IDENTIFIERS;
-static const int num_logtype_strings = COUNT_ARRAY(arr_logtype_strings);
+static const unsigned int num_logtype_strings = COUNT_ARRAY(arr_logtype_strings);
 
 /* Array for charachters representing log severity in the log file */
 static const char arr_loglevel_chars[] = {'-','S', 'E', 'W', 'N', 'I', 'D'};
-static const int num_loglevel_chars = COUNT_ARRAY(arr_loglevel_chars);
+static const unsigned int num_loglevel_chars = COUNT_ARRAY(arr_loglevel_chars);
 
 static const char *arr_loglevel_strings[] = LOGLEVEL_STRING_IDENTIFIERS;
-static const int num_loglevel_strings = COUNT_ARRAY(arr_loglevel_strings);
+static const unsigned int num_loglevel_strings = COUNT_ARRAY(arr_loglevel_strings);
 
 /* =========================================================================
    Internal function definitions
@@ -101,9 +159,9 @@ static const int num_loglevel_strings = COUNT_ARRAY(arr_loglevel_strings);
  */
 
 /* -[un]setuplog <logtype> <loglevel> [<filename>]*/
-static void setuplog_internal(const char *logtype, const char *loglevel, const char *filename)
+static void setuplog_internal(const char *loglevel, const char *logtype, const char *filename)
 {
-    int typenum, levelnum;
+    unsigned int typenum, levelnum;
 
     /* Parse logtype */
     for( typenum=0; typenum < num_logtype_strings; typenum++) {
@@ -150,6 +208,7 @@ static void generate_message_details(char *message_details_buffer,
     int    templen;
     int    len = message_details_buffer_length;
     struct timeval tv;
+    pid_t  pid;
 
     *ptr = 0;
 
@@ -161,18 +220,17 @@ static void generate_message_details(char *message_details_buffer,
     ptr += templen;
 
     templen = snprintf(ptr, len, "%06u ", (int)tv.tv_usec);
+    if (templen == -1 || templen >= len)
+        return;
+        
     len -= templen;
     ptr += templen;
 
-    /* Process name */
-    strncpy(ptr, log_config.processname, len);
-    templen = strlen(ptr);
-    len -= templen;
-    ptr += templen;
-
-    /* PID */
-    pid_t pid = getpid();
-    templen = snprintf(ptr, len, "[%d]", pid);
+    /* Process name &&  PID */
+    pid = getpid();
+    templen = snprintf(ptr, len, "%s[%d]", log_config.processname, pid);
+    if (templen == -1 || templen >= len)
+        return;
     len -= templen;
     ptr += templen;
 
@@ -183,7 +241,7 @@ static void generate_message_details(char *message_details_buffer,
             templen = snprintf(ptr, len, " {%s:%d}", basename + 1, log_src_linenumber);
         else
             templen = snprintf(ptr, len, " {%s:%d}", log_src_filename, log_src_linenumber);
-        if (templen >= len)
+        if (templen == -1 || templen >= len)
             return;
         len -= templen;
         ptr += templen;
@@ -194,20 +252,26 @@ static void generate_message_details(char *message_details_buffer,
         templen = snprintf(ptr, len,  " (D%d:", loglevel - 1);
     else
         templen = snprintf(ptr, len, " (%c:", arr_loglevel_chars[loglevel]);
+
+    if (templen == -1 || templen >= len)
+        return;
     len -= templen;
     ptr += templen;
 
     /* Errortype */
     if (logtype<num_logtype_strings) {
         templen = snprintf(ptr, len, "%s", arr_logtype_strings[logtype]);
+        if (templen == -1 || templen >= len)
+            return;
         len -= templen;
         ptr += templen;
     }
-
+    
     strncat(ptr, "): ", len);
+    ptr[len -1] = 0;
 }
 
-int get_syslog_equivalent(enum loglevels loglevel)
+static int get_syslog_equivalent(enum loglevels loglevel)
 {
     switch (loglevel)
     {
@@ -234,7 +298,7 @@ int get_syslog_equivalent(enum loglevels loglevel)
 
 void log_init(void)
 {
-    syslog_setup(log_note, 0,
+    syslog_setup(log_info, 0,
                  log_config.syslog_display_options,
                  log_config.facility);
 }
@@ -282,20 +346,39 @@ void log_setup(const char *filename, enum loglevels loglevel, enum logtypes logt
         file_configs[logtype].set = 0;
     }
 
+    /* Check if logging to a console */
+    if (logtype == logtype_console) {
+        log_config.console = 1;
+        logtype = logtype_default;
+    }
+
     /* Set new values */
     file_configs[logtype].filename = strdup(filename);
     file_configs[logtype].level = loglevel;
 
 
     /* Open log file as OPEN_LOGS_AS_UID*/
-    process_uid = geteuid();
-    if (process_uid)
-        seteuid(OPEN_LOGS_AS_UID);
-    file_configs[logtype].fd = open( file_configs[logtype].filename,
-                                     O_CREAT | O_WRONLY | O_APPEND,
-                                     S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
-    if (process_uid)
-        seteuid(process_uid);
+    /* Is it /dev/tty ? */
+    if (strcmp(file_configs[logtype].filename, "/dev/tty") == 0) {
+        file_configs[logtype].fd = 1; /* stdout */
+    } else {
+        process_uid = geteuid();
+        if (process_uid) {
+            if (seteuid(OPEN_LOGS_AS_UID) == -1) {
+                /* XXX failing silently */
+                return;
+            }
+        }
+        file_configs[logtype].fd = open( file_configs[logtype].filename,
+                                         O_CREAT | O_WRONLY | O_APPEND,
+                                         S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+        if (process_uid) {
+            if (seteuid(process_uid) == -1) {
+                LOG(log_error, logtype_logger, "can't seteuid back %s", strerror(errno));
+                exit(EXITERR_SYS);
+            }
+        }
+    }
 
     /* Check for error opening/creating logfile */
     if (-1 == file_configs[logtype].fd) {
@@ -319,12 +402,12 @@ void log_setup(const char *filename, enum loglevels loglevel, enum logtypes logt
     /* in order to make it easy and fast to check the loglevels in the LOG macro! */
 
     if (logtype == logtype_default) {
-        while (logtype != logtype_end_of_list_marker) {
-            if ( ! (file_configs[logtype].set))
-                file_configs[logtype].level = loglevel;
-            logtype++;
+        int typeiter = 0;
+        while (typeiter != logtype_end_of_list_marker) {
+            if ( ! (file_configs[typeiter].set))
+                file_configs[typeiter].level = loglevel;
+            typeiter++;
         }
-        logtype = logtype_default;
     }
 
     LOG(log_debug, logtype_logger, "Setup file logging: type: %s, level: %s, file: %s",
@@ -345,7 +428,7 @@ void syslog_setup(int loglevel, enum logtypes logtype _U_,
         arr_loglevel_strings[loglevel]);
 }
 
-void log_close()
+void log_close(void)
 {
 }
 
@@ -356,13 +439,25 @@ void set_processname(const char *processname)
     log_config.processname[15] = 0;
 }
 
+/* Called by the LOG macro for syslog messages */
+static void make_syslog_entry(enum loglevels loglevel, enum logtypes logtype _U_, char *message)
+{
+    if ( !log_config.syslog_opened ) {
+        openlog(log_config.processname, log_config.syslog_display_options,
+                log_config.facility);
+        log_config.syslog_opened = 1;
+    }
+
+    syslog(get_syslog_equivalent(loglevel), "%s", message);
+}
+
 /* -------------------------------------------------------------------------
    make_log_entry has 1 main flaws:
    The message in its entirity, must fit into the tempbuffer.
    So it must be shorter than MAXLOGSIZE
    ------------------------------------------------------------------------- */
 void make_log_entry(enum loglevels loglevel, enum logtypes logtype,
-                    char *message, ...)
+                    const char *file, int line, char *message, ...)
 {
     /* fn is not reentrant but is used in signal handler
      * with LOGGER it's a little late source name and line number
@@ -376,82 +471,85 @@ void make_log_entry(enum loglevels loglevel, enum logtypes logtype,
 
     if (inlog)
         return;
+
     inlog = 1;
 
+    if (!log_config.inited) {
+      log_init();
+    }
+    
+    if (file_configs[logtype].level >= loglevel) {
+      log_src_filename = file;
+      log_src_linenumber = line;
+    }
+    else if (!log_config.filelogging && log_config.syslog_level >= loglevel) {
+       /* Initialise the Messages */
+       va_start(args, message);
+       vsnprintf(temp_buffer, MAXLOGSIZE -1, message, args);
+       va_end(args);
+       temp_buffer[MAXLOGSIZE -1] = 0;
+       make_syslog_entry(loglevel, logtype, temp_buffer);
+       inlog = 0;
+       return;
+    }
+    else {
+       inlog = 0;
+       return;
+    }
+
+    /* Check if requested logtype is setup */
+    if (file_configs[logtype].set)
+        /* Yes */
+        fd = file_configs[logtype].fd;
+    else
+        /* No: use default */
+        fd = file_configs[logtype_default].fd;
+
+    if (fd < 0) {
+        /* no where to send the output, give up */
+        return;
+    }
+
     /* Initialise the Messages */
     va_start(args, message);
-    len = vsnprintf(temp_buffer, MAXLOGSIZE - 1, message, args);
+    len = vsnprintf(temp_buffer, MAXLOGSIZE -1, message, args);
     va_end(args);
 
     /* Append \n */
-    if (len >= MAXLOGSIZE)
+    if (len ==-1 || len >= MAXLOGSIZE -1) {
         /* vsnprintf hit the buffer size*/
         temp_buffer[MAXLOGSIZE-2] = '\n';
+        temp_buffer[MAXLOGSIZE-1] = 0;
+    }
     else {
         temp_buffer[len] = '\n';
         temp_buffer[len+1] = 0;
     }
 
-    generate_message_details(log_details_buffer, sizeof(log_details_buffer),
-                             file_configs[logtype].set ?
-                             file_configs[logtype].display_options :
-                             file_configs[logtype_default].display_options,
-                             loglevel, logtype);
+    if ( ! log_config.console) {
+        generate_message_details(log_details_buffer, sizeof(log_details_buffer),
+                                 file_configs[logtype].set ?
+                                 file_configs[logtype].display_options :
+                                 file_configs[logtype_default].display_options,
+                                 loglevel, logtype);
 
-    /* Check if requested logtype is setup */
-    if (file_configs[logtype].set)
-        /* Yes */
-        fd = file_configs[logtype].fd;
-    else
-        /* No: use default */
-        fd = file_configs[logtype_default].fd;
-
-    /* If default wasnt setup its fd is -1 */
-    if (fd >= 0) {
+        /* If default wasnt setup its fd is -1 */
         iov[0].iov_base = log_details_buffer;
         iov[0].iov_len = strlen(log_details_buffer);
         iov[1].iov_base = temp_buffer;
         iov[1].iov_len = strlen(temp_buffer);
         writev( fd,  iov, 2);
+    } else {
+        write(fd, temp_buffer, strlen(temp_buffer));
     }
 
     inlog = 0;
 }
 
-/* Called by the LOG macro for syslog messages */
-void make_syslog_entry(enum loglevels loglevel, enum logtypes logtype, char *message, ...)
-{
-    va_list args;
-    char log_buffer[MAXLOGSIZE];
-    /* fn is not reentrant but is used in signal handler
-     * with LOGGER it's a little late source name and line number
-     * are already changed.
-     */
-    static int inlog = 0;
-
-    if (inlog)
-        return;
-    inlog = 1;
-
-    if ( ! (log_config.syslog_opened) ) {
-        openlog(log_config.processname, log_config.syslog_display_options,
-                log_config.facility);
-        log_config.syslog_opened = 1;
-    }
-
-    /* Initialise the Messages */
-    va_start(args, message);
-    vsnprintf(log_buffer, sizeof(log_buffer), message, args);
-    va_end(args);
-
-    syslog(get_syslog_equivalent(loglevel), "%s", log_buffer);
-
-    inlog = 0;
-}
 
 void setuplog(const char *logstr)
 {
-    char *ptr, *ptrbak, *logtype, *loglevel, *filename;
+    char *ptr, *ptrbak, *logtype, *loglevel = NULL, *filename = NULL;
     ptr = strdup(logstr);
     ptrbak = ptr;
 
@@ -474,10 +572,12 @@ void setuplog(const char *logstr)
                 ptr++;
         }
         filename = ptr;
+        if (filename && *filename == 0)
+            filename = NULL;
     }
 
     /* finally call setuplog, filename can be NULL */
-    setuplog_internal(logtype, loglevel, filename);
+    setuplog_internal(loglevel, logtype, filename);
 
     free(ptrbak);
 }
@@ -496,7 +596,7 @@ void unsetuplog(const char *logstr)
     filename = strtok(NULL, " \t");
 
     /* finally call setuplog, filename can be NULL */
-    setuplog_internal(str, NULL, filename);
+    setuplog_internal(NULL, str, filename);
 
     free(str);
 }