some platforms (e.g. FreeBSD4) need netinet/in.h to be included
[asterisk/asterisk.git] / main / logger.c
index 1c7ef56..eb7ceeb 100644 (file)
  * \author Mark Spencer <markster@digium.com>
  */
 
+/*
+ * define _ASTERISK_LOGGER_H to prevent the inclusion of logger.h;
+ * it redefines LOG_* which we need to define syslog_level_map.
+ * Later, we force the inclusion of logger.h again.
+ */
+#define _ASTERISK_LOGGER_H
 #include "asterisk.h"
 
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
+#include "asterisk/_private.h"
+#include "asterisk/paths.h"    /* use ast_config_AST_LOG_DIR */
 #include <signal.h>
-#include <stdarg.h>
-#include <stdio.h>
-#include <unistd.h>
 #include <time.h>
-#include <string.h>
-#include <stdlib.h>
-#include <errno.h>
 #include <sys/stat.h>
-#if ((defined(AST_DEVMODE)) && (defined(Linux)))
+#include <fcntl.h>
+#if ((defined(AST_DEVMODE)) && (defined(linux)))
 #include <execinfo.h>
 #define MAX_BACKTRACE_FRAMES 20
 #endif
@@ -59,9 +62,9 @@ static int syslog_level_map[] = {
 
 #define SYSLOG_NLEVELS sizeof(syslog_level_map) / sizeof(int)
 
+#undef _ASTERISK_LOGGER_H      /* now include logger.h */
 #include "asterisk/logger.h"
 #include "asterisk/lock.h"
-#include "asterisk/options.h"
 #include "asterisk/channel.h"
 #include "asterisk/config.h"
 #include "asterisk/term.h"
@@ -70,6 +73,7 @@ static int syslog_level_map[] = {
 #include "asterisk/manager.h"
 #include "asterisk/threadstorage.h"
 #include "asterisk/strings.h"
+#include "asterisk/pbx.h"
 
 #if defined(__linux__) && !defined(__NR_gettid)
 #include <asm/unistd.h>
@@ -81,14 +85,19 @@ static int syslog_level_map[] = {
 #define GETTID() getpid()
 #endif
 
-
 static char dateformat[256] = "%b %e %T";              /* Original Asterisk Format */
 
 static char queue_log_name[256] = QUEUELOG;
+static char exec_after_rotate[256] = "";
 
 static int filesize_reload_needed;
 static int global_logmask = -1;
-static int rotatetimestamp;
+
+enum rotatestrategy {
+       SEQUENTIAL = 1 << 0,     /* Original method - create a new file, in order */
+       ROTATE = 1 << 1,         /* Rotate all files, such that the oldest file has the highest suffix */
+       TIMESTAMP = 1 << 2,      /* Append the epoch timestamp onto the end of the archived file */
+} rotatestrategy = SEQUENTIAL;
 
 static struct {
        unsigned int queue_log:1;
@@ -113,7 +122,7 @@ struct logchannel {
        AST_LIST_ENTRY(logchannel) list;
 };
 
-static AST_LIST_HEAD_STATIC(logchannels, logchannel);
+static AST_RWLIST_HEAD_STATIC(logchannels, logchannel);
 
 enum logmsgtypes {
        LOGMSG_NORMAL = 0,
@@ -139,6 +148,7 @@ static int close_logger_thread;
 static FILE *eventlog;
 static FILE *qlog;
 
+/*! \brief Logging channels used in the Asterisk logging system */
 static char *levels[] = {
        "DEBUG",
        "EVENT",
@@ -149,6 +159,7 @@ static char *levels[] = {
        "DTMF"
 };
 
+/*! \brief Colors used in the console for logging */
 static int colors[] = {
        COLOR_BRGREEN,
        COLOR_BRBLUE,
@@ -165,11 +176,11 @@ AST_THREADSTORAGE(verbose_buf);
 AST_THREADSTORAGE(log_buf);
 #define LOG_BUF_INIT_SIZE       256
 
-static int make_components(char *s, int lineno)
+static int make_components(const char *s, int lineno)
 {
        char *w;
        int res = 0;
-       char *stringp = s;
+       char *stringp = ast_strdupa(s);
 
        while ((w = strsep(&stringp, ","))) {
                w = ast_skip_blanks(w);
@@ -195,7 +206,7 @@ static int make_components(char *s, int lineno)
        return res;
 }
 
-static struct logchannel *make_logchannel(char *channel, char *components, int lineno)
+static struct logchannel *make_logchannel(const char *channel, const char *components, int lineno)
 {
        struct logchannel *chan;
        char *facility;
@@ -306,26 +317,30 @@ static struct logchannel *make_logchannel(char *channel, char *components, int l
        return chan;
 }
 
-static void init_logger_chain(void)
+static void init_logger_chain(int reload, int locked)
 {
        struct logchannel *chan;
        struct ast_config *cfg;
        struct ast_variable *var;
        const char *s;
+       struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
+
+       if ((cfg = ast_config_load("logger.conf", config_flags)) == CONFIG_STATUS_FILEUNCHANGED)
+               return;
 
        /* delete our list of log channels */
-       AST_LIST_LOCK(&logchannels);
-       while ((chan = AST_LIST_REMOVE_HEAD(&logchannels, list)))
+       if (!locked)
+               AST_RWLIST_WRLOCK(&logchannels);
+       while ((chan = AST_RWLIST_REMOVE_HEAD(&logchannels, list)))
                free(chan);
-       AST_LIST_UNLOCK(&logchannels);
+       if (!locked)
+               AST_RWLIST_UNLOCK(&logchannels);
        
        global_logmask = 0;
        errno = 0;
        /* close syslog */
        closelog();
        
-       cfg = ast_config_load("logger.conf");
-       
        /* If no config file, we're fine, set default options. */
        if (!cfg) {
                if (errno)
@@ -336,9 +351,11 @@ static void init_logger_chain(void)
                        return;
                chan->type = LOGTYPE_CONSOLE;
                chan->logmask = 28; /*warning,notice,error */
-               AST_LIST_LOCK(&logchannels);
-               AST_LIST_INSERT_HEAD(&logchannels, chan, list);
-               AST_LIST_UNLOCK(&logchannels);
+               if (!locked)
+                       AST_RWLIST_WRLOCK(&logchannels);
+               AST_RWLIST_INSERT_HEAD(&logchannels, chan, list);
+               if (!locked)
+                       AST_RWLIST_UNLOCK(&logchannels);
                global_logmask |= chan->logmask;
                return;
        }
@@ -347,7 +364,7 @@ static void init_logger_chain(void)
                if (ast_true(s)) {
                        if (gethostname(hostname, sizeof(hostname) - 1)) {
                                ast_copy_string(hostname, "unknown", sizeof(hostname));
-                               ast_log(LOG_WARNING, "What box has no hostname???\n");
+                               fprintf(stderr, "What box has no hostname???\n");
                        }
                } else
                        hostname[0] = '\0';
@@ -363,18 +380,35 @@ static void init_logger_chain(void)
                logfiles.event_log = ast_true(s);
        if ((s = ast_variable_retrieve(cfg, "general", "queue_log_name")))
                ast_copy_string(queue_log_name, s, sizeof(queue_log_name));
-       if ((s = ast_variable_retrieve(cfg, "general", "rotatetimestamp")))
-               rotatetimestamp = ast_true(s);
+       if ((s = ast_variable_retrieve(cfg, "general", "exec_after_rotate")))
+               ast_copy_string(exec_after_rotate, s, sizeof(exec_after_rotate));
+       if ((s = ast_variable_retrieve(cfg, "general", "rotatestrategy"))) {
+               if (strcasecmp(s, "timestamp") == 0)
+                       rotatestrategy = TIMESTAMP;
+               else if (strcasecmp(s, "rotate") == 0)
+                       rotatestrategy = ROTATE;
+               else if (strcasecmp(s, "sequential") == 0)
+                       rotatestrategy = SEQUENTIAL;
+               else
+                       fprintf(stderr, "Unknown rotatestrategy: %s\n", s);
+       } else {
+               if ((s = ast_variable_retrieve(cfg, "general", "rotatetimestamp"))) {
+                       rotatestrategy = ast_true(s) ? TIMESTAMP : SEQUENTIAL;
+                       fprintf(stderr, "rotatetimestamp option has been deprecated.  Please use rotatestrategy instead.\n");
+               }
+       }
 
-       AST_LIST_LOCK(&logchannels);
+       if (!locked)
+               AST_RWLIST_WRLOCK(&logchannels);
        var = ast_variable_browse(cfg, "logfiles");
        for (; var; var = var->next) {
                if (!(chan = make_logchannel(var->name, var->value, var->lineno)))
                        continue;
-               AST_LIST_INSERT_HEAD(&logchannels, chan, list);
+               AST_RWLIST_INSERT_HEAD(&logchannels, chan, list);
                global_logmask |= chan->logmask;
        }
-       AST_LIST_UNLOCK(&logchannels);
+       if (!locked)
+               AST_RWLIST_UNLOCK(&logchannels);
 
        ast_config_destroy(cfg);
 }
@@ -382,44 +416,152 @@ static void init_logger_chain(void)
 void ast_queue_log(const char *queuename, const char *callid, const char *agent, const char *event, const char *fmt, ...)
 {
        va_list ap;
-       AST_LIST_LOCK(&logchannels);
+       char qlog_msg[8192];
+       int qlog_len;
        if (qlog) {
                va_start(ap, fmt);
-               fprintf(qlog, "%ld|%s|%s|%s|%s|", (long)time(NULL), callid, queuename, agent, event);
-               vfprintf(qlog, fmt, ap);
-               fprintf(qlog, "\n");
+               qlog_len = snprintf(qlog_msg, sizeof(qlog_msg), "%ld|%s|%s|%s|%s|", (long)time(NULL), callid, queuename, agent, event);
+               vsnprintf(qlog_msg + qlog_len, sizeof(qlog_msg) - qlog_len, fmt, ap);
                va_end(ap);
+       }
+       AST_RWLIST_RDLOCK(&logchannels);
+       if (qlog) {
+               fprintf(qlog, "%s\n", qlog_msg);
                fflush(qlog);
        }
-       AST_LIST_UNLOCK(&logchannels);
+       AST_RWLIST_UNLOCK(&logchannels);
 }
 
-int reload_logger(int rotate)
+static int rotate_file(const char *filename)
 {
-       char old[PATH_MAX] = "";
+       char old[PATH_MAX];
        char new[PATH_MAX];
+       int x, y, which, found, res = 0, fd;
+       char *suffixes[4] = { "", ".gz", ".bz2", ".Z" };
+
+       switch (rotatestrategy) {
+       case SEQUENTIAL:
+               for (x = 0; ; x++) {
+                       snprintf(new, sizeof(new), "%s.%d", filename, x);
+                       fd = open(new, O_RDONLY);
+                       if (fd > -1)
+                               close(fd);
+                       else
+                               break;
+               }
+               if (rename(filename, new)) {
+                       fprintf(stderr, "Unable to rename file '%s' to '%s'\n", filename, new);
+                       res = -1;
+               }
+               break;
+       case TIMESTAMP:
+               snprintf(new, sizeof(new), "%s.%ld", filename, (long)time(NULL));
+               if (rename(filename, new)) {
+                       fprintf(stderr, "Unable to rename file '%s' to '%s'\n", filename, new);
+                       res = -1;
+               }
+               break;
+       case ROTATE:
+               /* Find the next empty slot, including a possible suffix */
+               for (x = 0; ; x++) {
+                       found = 0;
+                       for (which = 0; which < sizeof(suffixes) / sizeof(suffixes[0]); which++) {
+                               snprintf(new, sizeof(new), "%s.%d%s", filename, x, suffixes[which]);
+                               fd = open(new, O_RDONLY);
+                               if (fd > -1)
+                                       close(fd);
+                               else {
+                                       found = 1;
+                                       break;
+                               }
+                       }
+                       if (!found)
+                               break;
+               }
+
+               /* Found an empty slot */
+               for (y = x; y > -1; y--) {
+                       for (which = 0; which < sizeof(suffixes) / sizeof(suffixes[0]); which++) {
+                               snprintf(old, sizeof(old), "%s.%d%s", filename, y - 1, suffixes[which]);
+                               fd = open(old, O_RDONLY);
+                               if (fd > -1) {
+                                       /* Found the right suffix */
+                                       close(fd);
+                                       snprintf(new, sizeof(new), "%s.%d%s", filename, y, suffixes[which]);
+                                       if (rename(old, new)) {
+                                               fprintf(stderr, "Unable to rename file '%s' to '%s'\n", old, new);
+                                               res = -1;
+                                       }
+                                       break;
+                               }
+                       }
+               }
+
+               /* Finally, rename the current file */
+               snprintf(new, sizeof(new), "%s.0", filename);
+               if (rename(filename, new)) {
+                       fprintf(stderr, "Unable to rename file '%s' to '%s'\n", filename, new);
+                       res = -1;
+               }
+       }
+
+       if (!ast_strlen_zero(exec_after_rotate)) {
+               struct ast_channel *c = ast_channel_alloc(0, 0, "", "", "", "", "", 0, "Logger/rotate");
+               char buf[512];
+               pbx_builtin_setvar_helper(c, "filename", filename);
+               pbx_substitute_variables_helper(c, exec_after_rotate, buf, sizeof(buf));
+               system(buf);
+               ast_channel_free(c);
+       }
+       return res;
+}
+
+static int reload_logger(int rotate)
+{
+       char old[PATH_MAX] = "";
        int event_rotate = rotate, queue_rotate = rotate;
        struct logchannel *f;
-       FILE *myf;
-       int x, res = 0;
+       int res = 0;
+       struct stat st;
 
-       AST_LIST_LOCK(&logchannels);
+       AST_RWLIST_WRLOCK(&logchannels);
 
-       if (eventlog) 
-               fclose(eventlog);
-       else 
+       if (eventlog) {
+               if (rotate < 0) {
+                       /* Check filesize - this one typically doesn't need an auto-rotate */
+                       snprintf(old, sizeof(old), "%s/%s", ast_config_AST_LOG_DIR, EVENTLOG);
+                       if (stat(old, &st) != 0 || st.st_size > 0x40000000) { /* Arbitrarily, 1 GB */
+                               fclose(eventlog);
+                               eventlog = NULL;
+                       } else
+                               event_rotate = 0;
+               } else {
+                       fclose(eventlog);
+                       eventlog = NULL;
+               }
+       } else
                event_rotate = 0;
-       eventlog = NULL;
 
-       if (qlog) 
-               fclose(qlog);
-       else 
+       if (qlog) {
+               if (rotate < 0) {
+                       /* Check filesize - this one typically doesn't need an auto-rotate */
+                       snprintf(old, sizeof(old), "%s/%s", ast_config_AST_LOG_DIR, queue_log_name);
+                       if (stat(old, &st) != 0 || st.st_size > 0x40000000) { /* Arbitrarily, 1 GB */
+                               fclose(qlog);
+                               qlog = NULL;
+                       } else
+                               event_rotate = 0;
+               } else {
+                       fclose(qlog);
+                       qlog = NULL;
+               }
+       } else 
                queue_rotate = 0;
        qlog = NULL;
 
        ast_mkdir(ast_config_AST_LOG_DIR, 0777);
 
-       AST_LIST_TRAVERSE(&logchannels, f, list) {
+       AST_RWLIST_TRAVERSE(&logchannels, f, list) {
                if (f->disabled) {
                        f->disabled = 0;        /* Re-enable logging at reload */
                        manager_event(EVENT_FLAG_SYSTEM, "LogChannel", "Channel: %s\r\nEnabled: Yes\r\n", f->filename);
@@ -427,51 +569,19 @@ int reload_logger(int rotate)
                if (f->fileptr && (f->fileptr != stdout) && (f->fileptr != stderr)) {
                        fclose(f->fileptr);     /* Close file */
                        f->fileptr = NULL;
-                       if (rotate) {
-                               ast_copy_string(old, f->filename, sizeof(old));
-                               
-                               if (!rotatetimestamp) { 
-                                       for (x = 0; ; x++) {
-                                               snprintf(new, sizeof(new), "%s.%d", f->filename, x);
-                                               myf = fopen(new, "r");
-                                               if (myf)
-                                                       fclose(myf);
-                                               else
-                                                       break;
-                                       }
-                               } else 
-                                       snprintf(new, sizeof(new), "%s.%ld", f->filename, (long)time(NULL));
-
-                               /* do it */
-                               if (rename(old,new))
-                                       fprintf(stderr, "Unable to rename file '%s' to '%s'\n", old, new);
-                       }
+                       if (rotate)
+                               rotate_file(f->filename);
                }
        }
 
        filesize_reload_needed = 0;
-       
-       init_logger_chain();
+
+       init_logger_chain(rotate ? 0 : 1 /* reload */, 1 /* locked */);
 
        if (logfiles.event_log) {
                snprintf(old, sizeof(old), "%s/%s", ast_config_AST_LOG_DIR, EVENTLOG);
-               if (event_rotate) {
-                       if (!rotatetimestamp) { 
-                               for (x=0;;x++) {
-                                       snprintf(new, sizeof(new), "%s/%s.%d", ast_config_AST_LOG_DIR, EVENTLOG,x);
-                                       myf = fopen(new, "r");
-                                       if (myf)        /* File exists */
-                                               fclose(myf);
-                                       else
-                                               break;
-                               }
-                       } else 
-                               snprintf(new, sizeof(new), "%s/%s.%ld", ast_config_AST_LOG_DIR, EVENTLOG,(long)time(NULL));
-       
-                       /* do it */
-                       if (rename(old,new))
-                               ast_log(LOG_ERROR, "Unable to rename file '%s' to '%s'\n", old, new);
-               }
+               if (event_rotate)
+                       rotate_file(old);
 
                eventlog = fopen(old, "a");
                if (eventlog) {
@@ -486,27 +596,14 @@ int reload_logger(int rotate)
 
        if (logfiles.queue_log) {
                snprintf(old, sizeof(old), "%s/%s", ast_config_AST_LOG_DIR, queue_log_name);
-               if (queue_rotate) {
-                       if (!rotatetimestamp) { 
-                               for (x = 0; ; x++) {
-                                       snprintf(new, sizeof(new), "%s/%s.%d", ast_config_AST_LOG_DIR, queue_log_name, x);
-                                       myf = fopen(new, "r");
-                                       if (myf)        /* File exists */
-                                               fclose(myf);
-                                       else
-                                               break;
-                               }
-       
-                       } else 
-                               snprintf(new, sizeof(new), "%s/%s.%ld", ast_config_AST_LOG_DIR, queue_log_name,(long)time(NULL));
-                       /* do it */
-                       if (rename(old, new))
-                               ast_log(LOG_ERROR, "Unable to rename file '%s' to '%s'\n", old, new);
-               }
+               if (queue_rotate)
+                       rotate_file(old);
 
                qlog = fopen(old, "a");
                if (qlog) {
+                       AST_RWLIST_UNLOCK(&logchannels);
                        ast_queue_log("NONE", "NONE", "NONE", "CONFIGRELOAD", "%s", "");
+                       AST_RWLIST_WRLOCK(&logchannels);
                        ast_log(LOG_EVENT, "Restarted Asterisk Queue Logger\n");
                        if (option_verbose)
                                ast_verbose("Asterisk Queue Logger restarted\n");
@@ -516,64 +613,102 @@ int reload_logger(int rotate)
                }
        }
 
-       AST_LIST_UNLOCK(&logchannels);
+       AST_RWLIST_UNLOCK(&logchannels);
 
        return res;
 }
 
-static int handle_logger_reload(int fd, int argc, char *argv[])
+/*! \brief Reload the logger module without rotating log files (also used from loader.c during
+       a full Asterisk reload) */
+int logger_reload(void)
 {
-       if (reload_logger(0)) {
-               ast_cli(fd, "Failed to reload the logger\n");
+       if(reload_logger(0))
                return RESULT_FAILURE;
-       } else
-               return RESULT_SUCCESS;
+       return RESULT_SUCCESS;
+}
+
+static char *handle_logger_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+       switch (cmd) {
+       case CLI_INIT:
+               e->command = "logger reload";
+               e->usage = 
+                       "Usage: logger reload\n"
+                       "       Reloads the logger subsystem state.  Use after restarting syslogd(8) if you are using syslog logging.\n";
+               return NULL;
+       case CLI_GENERATE:
+               return NULL;
+       }
+       if (reload_logger(0)) {
+               ast_cli(a->fd, "Failed to reload the logger\n");
+               return CLI_FAILURE;
+       }
+       return CLI_SUCCESS;
 }
 
-static int handle_logger_rotate(int fd, int argc, char *argv[])
+static char *handle_logger_rotate(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
+       switch (cmd) {
+       case CLI_INIT:
+               e->command = "logger rotate";
+               e->usage = 
+                       "Usage: logger rotate\n"
+                       "       Rotates and Reopens the log files.\n";
+               return NULL;
+       case CLI_GENERATE:
+               return NULL;    
+       }
        if (reload_logger(1)) {
-               ast_cli(fd, "Failed to reload the logger and rotate log files\n");
-               return RESULT_FAILURE;
-       } else
-               return RESULT_SUCCESS;
+               ast_cli(a->fd, "Failed to reload the logger and rotate log files\n");
+               return CLI_FAILURE;
+       } 
+       return CLI_SUCCESS;
 }
 
 /*! \brief CLI command to show logging system configuration */
-static int handle_logger_show_channels(int fd, int argc, char *argv[])
+static char *handle_logger_show_channels(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
 #define FORMATL        "%-35.35s %-8.8s %-9.9s "
        struct logchannel *chan;
-
-       ast_cli(fd,FORMATL, "Channel", "Type", "Status");
-       ast_cli(fd, "Configuration\n");
-       ast_cli(fd,FORMATL, "-------", "----", "------");
-       ast_cli(fd, "-------------\n");
-       AST_LIST_LOCK(&logchannels);
-       AST_LIST_TRAVERSE(&logchannels, chan, list) {
-               ast_cli(fd, FORMATL, chan->filename, chan->type==LOGTYPE_CONSOLE ? "Console" : (chan->type==LOGTYPE_SYSLOG ? "Syslog" : "File"),
+       switch (cmd) {
+       case CLI_INIT:
+               e->command = "logger show channels";
+               e->usage = 
+                       "Usage: logger show channels\n"
+                       "       List configured logger channels.\n";
+               return NULL;
+       case CLI_GENERATE:
+               return NULL;    
+       }
+       ast_cli(a->fd,FORMATL, "Channel", "Type", "Status");
+       ast_cli(a->fd, "Configuration\n");
+       ast_cli(a->fd,FORMATL, "-------", "----", "------");
+       ast_cli(a->fd, "-------------\n");
+       AST_RWLIST_RDLOCK(&logchannels);
+       AST_RWLIST_TRAVERSE(&logchannels, chan, list) {
+               ast_cli(a->fd, FORMATL, chan->filename, chan->type==LOGTYPE_CONSOLE ? "Console" : (chan->type==LOGTYPE_SYSLOG ? "Syslog" : "File"),
                        chan->disabled ? "Disabled" : "Enabled");
-               ast_cli(fd, " - ");
+               ast_cli(a->fd, " - ");
                if (chan->logmask & (1 << __LOG_DEBUG)) 
-                       ast_cli(fd, "Debug ");
+                       ast_cli(a->fd, "Debug ");
                if (chan->logmask & (1 << __LOG_DTMF)) 
-                       ast_cli(fd, "DTMF ");
+                       ast_cli(a->fd, "DTMF ");
                if (chan->logmask & (1 << __LOG_VERBOSE)) 
-                       ast_cli(fd, "Verbose ");
+                       ast_cli(a->fd, "Verbose ");
                if (chan->logmask & (1 << __LOG_WARNING)) 
-                       ast_cli(fd, "Warning ");
+                       ast_cli(a->fd, "Warning ");
                if (chan->logmask & (1 << __LOG_NOTICE)) 
-                       ast_cli(fd, "Notice ");
+                       ast_cli(a->fd, "Notice ");
                if (chan->logmask & (1 << __LOG_ERROR)) 
-                       ast_cli(fd, "Error ");
+                       ast_cli(a->fd, "Error ");
                if (chan->logmask & (1 << __LOG_EVENT)) 
-                       ast_cli(fd, "Event ");
-               ast_cli(fd, "\n");
+                       ast_cli(a->fd, "Event ");
+               ast_cli(a->fd, "\n");
        }
-       AST_LIST_UNLOCK(&logchannels);
-       ast_cli(fd, "\n");
+       AST_RWLIST_UNLOCK(&logchannels);
+       ast_cli(a->fd, "\n");
                
-       return RESULT_SUCCESS;
+       return CLI_SUCCESS;
 }
 
 struct verb {
@@ -581,32 +716,12 @@ struct verb {
        AST_LIST_ENTRY(verb) list;
 };
 
-static AST_LIST_HEAD_STATIC(verbosers, verb);
-
-static char logger_reload_help[] =
-"Usage: logger reload\n"
-"       Reloads the logger subsystem state.  Use after restarting syslogd(8) if you are using syslog logging.\n";
-
-static char logger_rotate_help[] =
-"Usage: logger rotate\n"
-"       Rotates and Reopens the log files.\n";
-
-static char logger_show_channels_help[] =
-"Usage: logger show channels\n"
-"       List configured logger channels.\n";
+static AST_RWLIST_HEAD_STATIC(verbosers, verb);
 
 static struct ast_cli_entry cli_logger[] = {
-       { { "logger", "show", "channels", NULL }, 
-       handle_logger_show_channels, "List configured log channels",
-       logger_show_channels_help },
-
-       { { "logger", "reload", NULL }, 
-       handle_logger_reload, "Reopens the log files",
-       logger_reload_help },
-
-       { { "logger", "rotate", NULL }, 
-       handle_logger_rotate, "Rotates and reopens the log files",
-       logger_rotate_help },
+       AST_CLI_DEFINE(handle_logger_show_channels, "List configured log channels"),
+       AST_CLI_DEFINE(handle_logger_reload, "Reopens the log files"),
+       AST_CLI_DEFINE(handle_logger_rotate, "Rotates and reopens the log files")
 };
 
 static int handle_SIGXFSZ(int sig) 
@@ -641,23 +756,23 @@ static void ast_log_vsyslog(int level, const char *file, int line, const char *f
         syslog(syslog_level_map[level], "%s", buf);
 }
 
-/* Print a normal log message to the channels */
+/*! \brief Print a normal log message to the channels */
 static void logger_print_normal(struct logmsg *logmsg)
 {
        struct logchannel *chan = NULL;
        char buf[BUFSIZ];
 
-       AST_LIST_LOCK(&logchannels);
+       AST_RWLIST_RDLOCK(&logchannels);
 
        if (logfiles.event_log && logmsg->level == __LOG_EVENT) {
                fprintf(eventlog, "%s asterisk[%ld]: %s", logmsg->date, (long)getpid(), logmsg->str);
                fflush(eventlog);
-               AST_LIST_UNLOCK(&logchannels);
+               AST_RWLIST_UNLOCK(&logchannels);
                return;
        }
 
-       if (!AST_LIST_EMPTY(&logchannels)) {
-               AST_LIST_TRAVERSE(&logchannels, chan, list) {
+       if (!AST_RWLIST_EMPTY(&logchannels)) {
+               AST_RWLIST_TRAVERSE(&logchannels, chan, list) {
                        /* If the channel is disabled, then move on to the next one */
                        if (chan->disabled)
                                continue;
@@ -714,11 +829,11 @@ static void logger_print_normal(struct logmsg *logmsg)
                fputs(logmsg->str, stdout);
        }
 
-       AST_LIST_UNLOCK(&logchannels);
+       AST_RWLIST_UNLOCK(&logchannels);
 
        /* If we need to reload because of the file size, then do so */
        if (filesize_reload_needed) {
-               reload_logger(1);
+               reload_logger(-1);
                ast_log(LOG_EVENT, "Rotated Logs Per SIGXFSZ (Exceeded file size limit)\n");
                if (option_verbose)
                        ast_verbose("Rotated Logs Per SIGXFSZ (Exceeded file size limit)\n");
@@ -727,21 +842,21 @@ static void logger_print_normal(struct logmsg *logmsg)
        return;
 }
 
-/* Print a verbose message to the verbosers */
+/*! \brief Print a verbose message to the verbosers */
 static void logger_print_verbose(struct logmsg *logmsg)
 {
        struct verb *v = NULL;
 
        /* Iterate through the list of verbosers and pass them the log message string */
-       AST_LIST_LOCK(&verbosers);
-       AST_LIST_TRAVERSE(&verbosers, v, list)
+       AST_RWLIST_RDLOCK(&verbosers);
+       AST_RWLIST_TRAVERSE(&verbosers, v, list)
                v->verboser(logmsg->str);
-       AST_LIST_UNLOCK(&verbosers);
+       AST_RWLIST_UNLOCK(&verbosers);
 
        return;
 }
 
-/* Actual logging thread */
+/*! \brief Actual logging thread */
 static void *logger_thread(void *data)
 {
        struct logmsg *next = NULL, *msg = NULL;
@@ -755,10 +870,6 @@ static void *logger_thread(void *data)
                AST_LIST_HEAD_INIT_NOLOCK(&logmsgs);
                AST_LIST_UNLOCK(&logmsgs);
 
-               /* If we should stop, then stop */
-               if (close_logger_thread)
-                       break;
-
                /* Otherwise go through and process each message in the order added */
                while ((msg = next)) {
                        /* Get the next entry now so that we can free our current structure later */
@@ -773,6 +884,10 @@ static void *logger_thread(void *data)
                        /* Free the data since we are done */
                        free(msg);
                }
+
+               /* If we should stop, then stop */
+               if (close_logger_thread)
+                       break;
        }
 
        return NULL;
@@ -799,7 +914,7 @@ int init_logger(void)
        ast_mkdir(ast_config_AST_LOG_DIR, 0777);
   
        /* create log channels */
-       init_logger_chain();
+       init_logger_chain(0 /* reload */, 0 /* locked */);
 
        /* create the eventlog */
        if (logfiles.event_log) {
@@ -833,7 +948,10 @@ void close_logger(void)
        ast_cond_signal(&logcond);
        AST_LIST_UNLOCK(&logmsgs);
 
-       AST_LIST_LOCK(&logchannels);
+       if (logthread != AST_PTHREADT_NULL)
+               pthread_join(logthread, NULL);
+
+       AST_RWLIST_WRLOCK(&logchannels);
 
        if (eventlog) {
                fclose(eventlog);
@@ -845,7 +963,7 @@ void close_logger(void)
                qlog = NULL;
        }
 
-       AST_LIST_TRAVERSE(&logchannels, f, list) {
+       AST_RWLIST_TRAVERSE(&logchannels, f, list) {
                if (f->fileptr && (f->fileptr != stdout) && (f->fileptr != stderr)) {
                        fclose(f->fileptr);
                        f->fileptr = NULL;
@@ -854,7 +972,7 @@ void close_logger(void)
 
        closelog(); /* syslog */
 
-       AST_LIST_UNLOCK(&logchannels);
+       AST_RWLIST_UNLOCK(&logchannels);
 
        return;
 }
@@ -866,15 +984,15 @@ void ast_log(int level, const char *file, int line, const char *function, const
 {
        struct logmsg *logmsg = NULL;
        struct ast_str *buf = NULL;
-       struct tm tm;
-       time_t t;
+       struct ast_tm tm;
+       struct timeval tv = ast_tvnow();
        int res = 0;
        va_list ap;
 
        if (!(buf = ast_str_thread_get(&log_buf, LOG_BUF_INIT_SIZE)))
                return;
 
-       if (AST_LIST_EMPTY(&logchannels)) {
+       if (AST_RWLIST_EMPTY(&logchannels)) {
                /*
                 * we don't have the logger chain configured yet,
                 * so just log to stdout
@@ -905,10 +1023,6 @@ void ast_log(int level, const char *file, int line, const char *function, const
        if (!(global_logmask & (1 << level)))
                return;
        
-       /* Ignore anything other than the currently debugged file if there is one */
-       if ((level == __LOG_DEBUG) && !ast_strlen_zero(debug_filename) && strcasecmp(debug_filename, file))
-               return;
-
        /* Build string */
        va_start(ap, fmt);
        res = ast_str_set_va(&buf, BUFSIZ, fmt, ap);
@@ -929,9 +1043,8 @@ void ast_log(int level, const char *file, int line, const char *function, const
        logmsg->type = LOGMSG_NORMAL;
 
        /* Create our date/time */
-       time(&t);
-       ast_localtime(&t, &tm, NULL);
-       strftime(logmsg->date, sizeof(logmsg->date), dateformat, &tm);
+       ast_localtime(&tv, &tm, NULL);
+       ast_strftime(logmsg->date, sizeof(logmsg->date), dateformat, &tm);
 
        /* Copy over data */
        logmsg->level = level;
@@ -955,7 +1068,7 @@ void ast_log(int level, const char *file, int line, const char *function, const
 
 void ast_backtrace(void)
 {
-#ifdef Linux
+#ifdef linux
 #ifdef AST_DEVMODE
        int count=0, i=0;
        void **addresses;
@@ -966,7 +1079,11 @@ void ast_backtrace(void)
                if ((strings = backtrace_symbols(addresses, count))) {
                        ast_debug(1, "Got %d backtrace record%c\n", count, count != 1 ? 's' : ' ');
                        for (i=0; i < count ; i++) {
-                               ast_debug(1, "#%d: [%08X] %s\n", i, (unsigned int)addresses[i], strings[i]);
+#if __WORDSIZE == 32
+                               ast_log(LOG_DEBUG, "#%d: [%08X] %s\n", i, (unsigned int)addresses[i], strings[i]);
+#elif __WORDSIZE == 64
+                               ast_log(LOG_DEBUG, "#%d: [%016lX] %s\n", i, (unsigned long)addresses[i], strings[i]);
+#endif
                        }
                        free(strings);
                } else {
@@ -977,7 +1094,7 @@ void ast_backtrace(void)
 #else
        ast_log(LOG_WARNING, "Must run configure with '--enable-dev-mode' for stack backtraces.\n");
 #endif
-#else /* ndef Linux */
+#else /* ndef linux */
        ast_log(LOG_WARNING, "Inline stack backtraces are only available on the Linux platform.\n");
 #endif
 }
@@ -993,14 +1110,14 @@ void ast_verbose(const char *fmt, ...)
                return;
 
         if (ast_opt_timestamp) {
-                time_t t;
-                struct tm tm;
+                struct timeval tv;
+                struct ast_tm tm;
                 char date[40];
                 char *datefmt;
 
-                time(&t);
-                ast_localtime(&t, &tm, NULL);
-                strftime(date, sizeof(date), dateformat, &tm);
+               tv = ast_tvnow();
+                ast_localtime(&tv, &tm, NULL);
+                ast_strftime(date, sizeof(date), dateformat, &tm);
                 datefmt = alloca(strlen(date) + 3 + strlen(fmt) + 1);
                 sprintf(datefmt, "[%s] %s", date, fmt);
                 fmt = datefmt;
@@ -1046,9 +1163,9 @@ int ast_register_verbose(void (*v)(const char *string))
 
        verb->verboser = v;
 
-       AST_LIST_LOCK(&verbosers);
-       AST_LIST_INSERT_HEAD(&verbosers, verb, list);
-       AST_LIST_UNLOCK(&verbosers);
+       AST_RWLIST_WRLOCK(&verbosers);
+       AST_RWLIST_INSERT_HEAD(&verbosers, verb, list);
+       AST_RWLIST_UNLOCK(&verbosers);
        
        return 0;
 }
@@ -1057,16 +1174,16 @@ int ast_unregister_verbose(void (*v)(const char *string))
 {
        struct verb *cur;
 
-       AST_LIST_LOCK(&verbosers);
-       AST_LIST_TRAVERSE_SAFE_BEGIN(&verbosers, cur, list) {
+       AST_RWLIST_WRLOCK(&verbosers);
+       AST_RWLIST_TRAVERSE_SAFE_BEGIN(&verbosers, cur, list) {
                if (cur->verboser == v) {
-                       AST_LIST_REMOVE_CURRENT(&verbosers, list);
+                       AST_RWLIST_REMOVE_CURRENT(list);
                        free(cur);
                        break;
                }
        }
-       AST_LIST_TRAVERSE_SAFE_END
-       AST_LIST_UNLOCK(&verbosers);
+       AST_RWLIST_TRAVERSE_SAFE_END;
+       AST_RWLIST_UNLOCK(&verbosers);
        
        return cur ? 0 : -1;
 }