Add features (incomplete, highly experimental), fix DundiLookup app, debug improvemen...
[asterisk/asterisk.git] / logger.c
index 5bb326c..7f32b70 100755 (executable)
--- a/logger.c
+++ b/logger.c
@@ -48,16 +48,26 @@ static int syslog_level_map[] = {
 
 #define MAX_MSG_QUEUE 200
 
+#ifdef __linux__
+#include <asm/unistd.h>
+#define GETTID() syscall(__NR_gettid)
+#else
+#define GETTID() getpid()
+#endif
+
 static char dateformat[256] = "%b %e %T";              /* Original Asterisk Format */
 AST_MUTEX_DEFINE_STATIC(msglist_lock);
 AST_MUTEX_DEFINE_STATIC(loglock);
 static int pending_logger_reload = 0;
+static int global_logmask = -1;
 
 static struct msglist {
        char *msg;
        struct msglist *next;
 } *list = NULL, *last = NULL;
 
+static char hostname[256];
+
 struct logchannel {
        int logmask;
        int facility; /* syslog */
@@ -167,10 +177,19 @@ static struct logchannel *make_logchannel(char *channel, char *components, int l
                    chan->syslog = 1;
                    openlog("asterisk", LOG_PID, chan->facility);
                } else {
-                       if (channel[0] == '/') 
-                               strncpy(chan->filename, channel, sizeof(chan->filename) - 1);
-                       else
+                       if (channel[0] == '/') {
+                               if(!ast_strlen_zero(hostname)) { 
+                                       snprintf(chan->filename, sizeof(chan->filename) - 1,"%s.%s", channel, hostname);
+                               } else {
+                                       strncpy(chan->filename, channel, sizeof(chan->filename) - 1);
+                               }
+                       }                 
+                       
+                       if(!ast_strlen_zero(hostname)) {
+                               snprintf(chan->filename, sizeof(chan->filename), "%s/%s.%s",(char *)ast_config_AST_LOG_DIR, channel, hostname);
+                       } else {
                                snprintf(chan->filename, sizeof(chan->filename), "%s/%s", (char *)ast_config_AST_LOG_DIR, channel);
+                       }
                        chan->fileptr = fopen(chan->filename, "a");
                        if (!chan->fileptr) {
                                /* Can't log here, since we're called with a lock */
@@ -199,26 +218,39 @@ static void init_logger_chain(void)
        }
        logchannels = NULL;
        ast_mutex_unlock(&loglock);
-
+       
+       global_logmask = 0;
        /* close syslog */
        closelog();
-
+       
        cfg = ast_load("logger.conf");
        
        /* If no config file, we're fine */
        if (!cfg)
            return;
-
+       
        ast_mutex_lock(&loglock);
+       if ((s = ast_variable_retrieve(cfg, "general", "appendhostname"))) {
+               if(ast_true(s)) {
+                       if(gethostname(hostname, sizeof(hostname))) {
+                               strncpy(hostname, "unknown", sizeof(hostname)-1);
+                               ast_log(LOG_WARNING, "What box has no hostname???\n");
+                       }
+               } else
+                       hostname[0] = '\0';
+       } else
+               hostname[0] = '\0';
        if ((s = ast_variable_retrieve(cfg, "general", "dateformat"))) {
                strncpy(dateformat, s, sizeof(dateformat) - 1);
-       }
+       } else
+               strncpy(dateformat, "%b %e %T", sizeof(dateformat) - 1);
        var = ast_variable_browse(cfg, "logfiles");
        while(var) {
                chan = make_logchannel(var->name, var->value, var->lineno);
                if (chan) {
                        chan->next = logchannels;
                        logchannels = chan;
+                       global_logmask |= chan->logmask;
                }
                var = var->next;
        }
@@ -433,8 +465,8 @@ void close_logger(void)
 {
        struct msglist *m, *tmp;
 
-       m = list;
        ast_mutex_lock(&msglist_lock);
+       m = list;
        while(m) {
                if (m->msg) {
                        free(m->msg);
@@ -444,6 +476,7 @@ void close_logger(void)
                m = tmp;
        }
        list = last = NULL;
+       msgcnt = 0;
        ast_mutex_unlock(&msglist_lock);
        return;
 }
@@ -458,11 +491,11 @@ static void ast_log_vsyslog(int level, const char *file, int line, const char *f
                return;
        }
        if (level == __LOG_VERBOSE) {
-               snprintf(buf, sizeof(buf), "VERBOSE[%ld]: ", (long)pthread_self());
+               snprintf(buf, sizeof(buf), "VERBOSE[%ld]: ", (long)GETTID());
                level = __LOG_DEBUG;
        } else {
                snprintf(buf, sizeof(buf), "%s[%ld]: %s:%d in %s: ",
-                       levels[level], (long)pthread_self(), file, line, function);
+                       levels[level], (long)GETTID(), file, line, function);
        }
        vsnprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), fmt, args);
        syslog(syslog_level_map[level], "%s", buf);
@@ -484,6 +517,9 @@ void ast_log(int level, const char *file, int line, const char *function, const
        if (!option_verbose && !option_debug && (level == __LOG_DEBUG)) {
                return;
        }
+       /* Ignore anything that never gets logged anywhere */
+       if (!(global_logmask & (1 << level)))
+               return;
 
        /* begin critical section */
        ast_mutex_lock(&loglock);
@@ -520,7 +556,7 @@ void ast_log(int level, const char *file, int line, const char *function, const
                                        snprintf(buf, sizeof(buf), "%s %s[%ld]: %s:%s %s: ",
                                                date,
                                                term_color(tmp1, levels[level], colors[level], 0, sizeof(tmp1)),
-                                               (long)pthread_self(),
+                                               (long)GETTID(),
                                                term_color(tmp2, file, COLOR_BRWHITE, 0, sizeof(tmp2)),
                                                term_color(tmp3, linestr, COLOR_BRWHITE, 0, sizeof(tmp3)),
                                                term_color(tmp4, function, COLOR_BRWHITE, 0, sizeof(tmp4)));
@@ -533,7 +569,7 @@ void ast_log(int level, const char *file, int line, const char *function, const
                                }
                        } else if ((chan->logmask & (1 << level)) && (chan->fileptr)) {
                                snprintf(buf, sizeof(buf), "%s %s[%ld]: ", date,
-                                       levels[level], (long)pthread_self());
+                                       levels[level], (long)GETTID());
                                fprintf(chan->fileptr, buf);
                                va_start(ap, fmt);
                                vsnprintf(buf, sizeof(buf), fmt, ap);
@@ -633,8 +669,8 @@ extern void ast_verbose(const char *fmt, ...)
 int ast_verbose_dmesg(void (*v)(const char *string, int opos, int replacelast, int complete))
 {
        struct msglist *m;
-       m = list;
        ast_mutex_lock(&msglist_lock);
+       m = list;
        while(m) {
                /* Send all the existing entries that we have queued (i.e. they're likely to have missed) */
                v(m->msg, 0, 0, 1);