]> arthur.barton.de Git - netatalk.git/blobdiff - libatalk/util/logger.c
logger: write to fd 1 directly instead of /dev/tty. The latter cant be redirected
[netatalk.git] / libatalk / util / logger.c
index b124142b4fe59cb5646ea3a526167395b9d25c3c..0664b09c2e694e278494ee7620e87547ba721eaf 100644 (file)
@@ -31,27 +31,82 @@ Netatalk 2001 (c)
 #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.
@@ -62,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 */
@@ -70,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
@@ -105,7 +161,7 @@ static const int num_loglevel_strings = COUNT_ARRAY(arr_loglevel_strings);
 /* -[un]setuplog <logtype> <loglevel> [<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++) {
@@ -196,6 +252,7 @@ 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;
@@ -209,9 +266,9 @@ static void generate_message_details(char *message_details_buffer,
         len -= templen;
         ptr += templen;
     }
-
+    
     strncat(ptr, "): ", len);
-    ptr[len] = 0;
+    ptr[len -1] = 0;
 }
 
 static int get_syslog_equivalent(enum loglevels loglevel)
@@ -289,26 +346,37 @@ 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) {
-        if (seteuid(OPEN_LOGS_AS_UID) == -1) {
-            /* XXX failing silently */
-            return;
+    /* 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);
+        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);
+            }
         }
     }
 
@@ -334,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",
@@ -371,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
@@ -392,6 +472,31 @@ 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 */
@@ -405,7 +510,6 @@ void make_log_entry(enum loglevels loglevel, enum logtypes logtype,
         return;
     }
 
-    inlog = 1;
     /* Initialise the Messages */
     va_start(args, message);
     len = vsnprintf(temp_buffer, MAXLOGSIZE -1, message, args);
@@ -422,57 +526,30 @@ void make_log_entry(enum loglevels loglevel, enum logtypes logtype,
         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 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);
-
-    inlog = 0;
-}
-
-/* Called by the LOG macro for syslog messages */
-void make_syslog_entry(enum loglevels loglevel, enum logtypes logtype _U_, 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;
+    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);
+
+        /* 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));
     }
 
-    /* Initialise the Messages */
-    va_start(args, message);
-    vsnprintf(log_buffer, sizeof(log_buffer), message, args);
-    va_end(args);
-    log_buffer[MAXLOGSIZE -1] = 0;
-    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;
 
@@ -495,6 +572,8 @@ void setuplog(const char *logstr)
                 ptr++;
         }
         filename = ptr;
+        if (filename && *filename == 0)
+            filename = NULL;
     }
 
     /* finally call setuplog, filename can be NULL */