vector: Additional string vector definitions.
[asterisk/asterisk.git] / main / logger.c
index 607755b..6060b3c 100644 (file)
@@ -40,8 +40,6 @@
 
 #include "asterisk.h"
 
-ASTERISK_REGISTER_FILE()
-
 /* When we include logger.h again it will trample on some stuff in syslog.h, but
  * nothing we care about in here. */
 #include <syslog.h>
@@ -69,6 +67,7 @@ ASTERISK_REGISTER_FILE()
 #include "asterisk/buildinfo.h"
 #include "asterisk/ast_version.h"
 #include "asterisk/backtrace.h"
+#include "asterisk/json.h"
 
 /*** DOCUMENTATION
  ***/
@@ -87,6 +86,11 @@ static int display_callids;
 
 AST_THREADSTORAGE(unique_callid);
 
+static int logger_queue_size;
+static int logger_queue_limit = 1000;
+static int logger_messages_discarded;
+static unsigned int high_water_alert;
+
 static enum rotatestrategy {
        NONE = 0,                /* Do not rotate log files at all, instead rely on external mechanisms */
        SEQUENTIAL = 1 << 0,     /* Original method - create a new file, in order */
@@ -104,6 +108,16 @@ static struct {
 static char hostname[MAXHOSTNAMELEN];
 AST_THREADSTORAGE_RAW(in_safe_log);
 
+struct logchannel;
+struct logmsg;
+
+struct logformatter {
+       /* The name of the log formatter */
+       const char *name;
+       /* Pointer to the function that will format the log */
+       int (* const format_log)(struct logchannel *channel, struct logmsg *msg, char *buf, size_t size);
+};
+
 enum logtypes {
        LOGTYPE_SYSLOG,
        LOGTYPE_FILE,
@@ -111,6 +125,8 @@ enum logtypes {
 };
 
 struct logchannel {
+       /*! How the logs sent to this channel will be formatted */
+       struct logformatter formatter;
        /*! What to log to this channel */
        unsigned int logmask;
        /*! If this channel is disabled or not */
@@ -145,6 +161,7 @@ enum logmsgtypes {
 struct logmsg {
        enum logmsgtypes type;
        int level;
+       int sublevel;
        int line;
        int lwp;
        ast_callid callid;
@@ -160,6 +177,7 @@ struct logmsg {
 
 static void logmsg_free(struct logmsg *msg)
 {
+       ast_string_field_free_memory(msg);
        ast_free(msg);
 }
 
@@ -234,6 +252,171 @@ AST_THREADSTORAGE(verbose_build_buf);
 AST_THREADSTORAGE(log_buf);
 #define LOG_BUF_INIT_SIZE       256
 
+static int format_log_json(struct logchannel *channel, struct logmsg *msg, char *buf, size_t size)
+{
+       struct ast_json *json;
+       char *str;
+       char call_identifier_str[13];
+       size_t json_str_len;
+
+       if (msg->callid) {
+               snprintf(call_identifier_str, sizeof(call_identifier_str), "[C-%08x]", msg->callid);
+       } else {
+               call_identifier_str[0] = '\0';
+       }
+
+       json = ast_json_pack("{s: s, s: s, "
+               "s: {s: i, s: s} "
+               "s: {s: {s: s, s: s, s: i}, "
+               "s: s, s: s} }",
+               "hostname", ast_config_AST_SYSTEM_NAME,
+               "timestamp", msg->date,
+               "identifiers",
+               "lwp", msg->lwp,
+               "callid", S_OR(call_identifier_str, ""),
+               "logmsg",
+               "location",
+               "filename", msg->file,
+               "function", msg->function,
+               "line", msg->line,
+               "level", msg->level_name,
+               "message", msg->message);
+       if (!json) {
+               return -1;
+       }
+
+       str = ast_json_dump_string(json);
+       if (!str) {
+               ast_json_unref(json);
+               return -1;
+       }
+
+       ast_copy_string(buf, str, size);
+       json_str_len = strlen(str);
+       if (json_str_len > size - 1) {
+               json_str_len = size - 1;
+       }
+       buf[json_str_len] = '\n';
+       buf[json_str_len + 1] = '\0';
+
+       term_strip(buf, buf, size);
+
+       ast_json_free(str);
+       ast_json_unref(json);
+
+       return 0;
+}
+
+static struct logformatter logformatter_json = {
+       .name = "json",
+       .format_log = format_log_json
+};
+
+static int logger_add_verbose_magic(struct logmsg *logmsg, char *buf, size_t size)
+{
+       const char *p;
+       const char *fmt;
+       struct ast_str *prefixed;
+       signed char magic = logmsg->sublevel > 9 ? -10 : -logmsg->sublevel - 1; /* 0 => -1, 1 => -2, etc.  Can't pass NUL, as it is EOS-delimiter */
+
+       /* For compatibility with modules still calling ast_verbose() directly instead of using ast_verb() */
+       if (logmsg->sublevel < 0) {
+               if (!strncmp(logmsg->message, VERBOSE_PREFIX_4, strlen(VERBOSE_PREFIX_4))) {
+                       magic = -5;
+               } else if (!strncmp(logmsg->message, VERBOSE_PREFIX_3, strlen(VERBOSE_PREFIX_3))) {
+                       magic = -4;
+               } else if (!strncmp(logmsg->message, VERBOSE_PREFIX_2, strlen(VERBOSE_PREFIX_2))) {
+                       magic = -3;
+               } else if (!strncmp(logmsg->message, VERBOSE_PREFIX_1, strlen(VERBOSE_PREFIX_1))) {
+                       magic = -2;
+               } else {
+                       magic = -1;
+               }
+       }
+
+       if (!(prefixed = ast_str_thread_get(&verbose_buf, VERBOSE_BUF_INIT_SIZE))) {
+               return -1;
+       }
+
+       ast_str_reset(prefixed);
+
+       /* for every newline found in the buffer add verbose prefix data */
+       fmt = logmsg->message;
+       do {
+               if (!(p = strchr(fmt, '\n'))) {
+                       p = strchr(fmt, '\0') - 1;
+               }
+               ++p;
+
+               ast_str_append(&prefixed, 0, "%c", (char)magic);
+               ast_str_append_substr(&prefixed, 0, fmt, p - fmt);
+               fmt = p;
+       } while (p && *p);
+
+       snprintf(buf, size, "%s", ast_str_buffer(prefixed));
+
+       return 0;
+}
+
+static int format_log_default(struct logchannel *chan, struct logmsg *msg, char *buf, size_t size)
+{
+       char call_identifier_str[13];
+
+       if (msg->callid) {
+               snprintf(call_identifier_str, sizeof(call_identifier_str), "[C-%08x]", msg->callid);
+       } else {
+               call_identifier_str[0] = '\0';
+       }
+
+       switch (chan->type) {
+       case LOGTYPE_SYSLOG:
+               snprintf(buf, size, "%s[%d]%s: %s:%d in %s: %s",
+                    levels[msg->level], msg->lwp, call_identifier_str, msg->file,
+                    msg->line, msg->function, msg->message);
+               term_strip(buf, buf, size);
+               break;
+       case LOGTYPE_FILE:
+               snprintf(buf, size, "[%s] %s[%d]%s %s: %s",
+                     msg->date, msg->level_name, msg->lwp, call_identifier_str,
+                     msg->file, msg->message);
+               term_strip(buf, buf, size);
+               break;
+       case LOGTYPE_CONSOLE:
+               {
+                       char linestr[32];
+
+                       /*
+                        * Verbose messages are interpreted by console channels in their own
+                        * special way
+                        */
+                       if (msg->level == __LOG_VERBOSE) {
+                               return logger_add_verbose_magic(msg, buf, size);
+                       }
+
+                       /* Turn the numeric line number into a string for colorization */
+                       snprintf(linestr, sizeof(linestr), "%d", msg->line);
+
+                       snprintf(buf, size, "[%s] " COLORIZE_FMT "[%d]%s: " COLORIZE_FMT ":" COLORIZE_FMT " " COLORIZE_FMT ": %s",
+                                msg->date,
+                                COLORIZE(colors[msg->level], 0, msg->level_name),
+                                msg->lwp,
+                                call_identifier_str,
+                                COLORIZE(COLOR_BRWHITE, 0, msg->file),
+                                COLORIZE(COLOR_BRWHITE, 0, linestr),
+                                COLORIZE(COLOR_BRWHITE, 0, msg->function),
+                                msg->message);
+               }
+               break;
+       }
+
+       return 0;
+}
+
+static struct logformatter logformatter_default = {
+       .name = "default",
+       .format_log = format_log_default,
+};
+
 static void make_components(struct logchannel *chan)
 {
        char *w;
@@ -245,6 +428,33 @@ static void make_components(struct logchannel *chan)
        /* Default to using option_verbose as the verbosity level of the logging channel.  */
        verb_level = -1;
 
+       w = strchr(stringp, '[');
+       if (w) {
+               char *end = strchr(w + 1, ']');
+               if (!end) {
+                       fprintf(stderr, "Logger Warning: bad formatter definition for %s in logger.conf\n", chan->filename);
+               } else {
+                       char *formatter_name = w + 1;
+
+                       *end = '\0';
+                       stringp = end + 1;
+
+                       if (!strcasecmp(formatter_name, "json")) {
+                               memcpy(&chan->formatter, &logformatter_json, sizeof(chan->formatter));
+                       } else if (!strcasecmp(formatter_name, "default")) {
+                               memcpy(&chan->formatter, &logformatter_default, sizeof(chan->formatter));
+                       } else {
+                               fprintf(stderr, "Logger Warning: Unknown formatter definition %s for %s in logger.conf; using 'default'\n",
+                                       formatter_name, chan->filename);
+                               memcpy(&chan->formatter, &logformatter_default, sizeof(chan->formatter));
+                       }
+               }
+       }
+
+       if (!chan->formatter.name) {
+               memcpy(&chan->formatter, &logformatter_default, sizeof(chan->formatter));
+       }
+
        while ((w = strsep(&stringp, ","))) {
                w = ast_strip(w);
                if (ast_strlen_zero(w)) {
@@ -272,12 +482,77 @@ static void make_components(struct logchannel *chan)
                 * with calculating the ast_verb_sys_level value.
                 */
                chan->verbosity = -1;
+               logmask |= (1 << __LOG_VERBOSE);
        } else {
                chan->verbosity = verb_level;
        }
        chan->logmask = logmask;
 }
 
+/*!
+ * \brief create the filename that will be used for a logger channel.
+ *
+ * \param channel The name of the logger channel
+ * \param[out] filename The filename for the logger channel
+ * \param size The size of the filename buffer
+ */
+static void make_filename(const char *channel, char *filename, size_t size)
+{
+       const char *log_dir_prefix = "";
+       const char *log_dir_separator = "";
+
+       *filename = '\0';
+
+       if (!strcasecmp(channel, "console")) {
+               return;
+       }
+
+       if (!strncasecmp(channel, "syslog", 6)) {
+               ast_copy_string(filename, channel, size);
+               return;
+       }
+
+       /* It's a filename */
+
+       if (channel[0] != '/') {
+               log_dir_prefix = ast_config_AST_LOG_DIR;
+               log_dir_separator = "/";
+       }
+
+       if (!ast_strlen_zero(hostname)) {
+               snprintf(filename, size, "%s%s%s.%s",
+                       log_dir_prefix, log_dir_separator, channel, hostname);
+       } else {
+               snprintf(filename, size, "%s%s%s",
+                       log_dir_prefix, log_dir_separator, channel);
+       }
+}
+
+/*!
+ * \brief Find a particular logger channel by name
+ *
+ * \pre logchannels list is locked
+ *
+ * \param channel The name of the logger channel to find
+ * \retval non-NULL The corresponding logger channel
+ * \retval NULL Unable to find a logger channel with that particular name
+ */
+static struct logchannel *find_logchannel(const char *channel)
+{
+       char filename[PATH_MAX];
+       struct logchannel *chan;
+
+       make_filename(channel, filename, sizeof(filename));
+
+       AST_RWLIST_TRAVERSE(&logchannels, chan, list) {
+               if (!strcmp(chan->filename, filename)) {
+                       return chan;
+               }
+       }
+
+       return NULL;
+}
+
 static struct logchannel *make_logchannel(const char *channel, const char *components, int lineno, int dynamic)
 {
        struct logchannel *chan;
@@ -293,6 +568,8 @@ static struct logchannel *make_logchannel(const char *channel, const char *compo
        chan->lineno = lineno;
        chan->dynamic = dynamic;
 
+       make_filename(channel, chan->filename, sizeof(chan->filename));
+
        if (!strcasecmp(channel, "console")) {
                chan->type = LOGTYPE_CONSOLE;
        } else if (!strncasecmp(channel, "syslog", 6)) {
@@ -314,25 +591,8 @@ static struct logchannel *make_logchannel(const char *channel, const char *compo
                }
 
                chan->type = LOGTYPE_SYSLOG;
-               ast_copy_string(chan->filename, channel, sizeof(chan->filename));
                openlog("asterisk", LOG_PID, chan->facility);
        } else {
-               const char *log_dir_prefix = "";
-               const char *log_dir_separator = "";
-
-               if (channel[0] != '/') {
-                       log_dir_prefix = ast_config_AST_LOG_DIR;
-                       log_dir_separator = "/";
-               }
-
-               if (!ast_strlen_zero(hostname)) {
-                       snprintf(chan->filename, sizeof(chan->filename), "%s%s%s.%s",
-                               log_dir_prefix, log_dir_separator, channel, hostname);
-               } else {
-                       snprintf(chan->filename, sizeof(chan->filename), "%s%s%s",
-                               log_dir_prefix, log_dir_separator, channel);
-               }
-
                if (!(chan->fileptr = fopen(chan->filename, "a"))) {
                        /* Can't do real logging here since we're called with a lock
                         * so log to any attached consoles */
@@ -361,13 +621,14 @@ static struct logchannel *make_logchannel(const char *channel, const char *compo
 }
 
 /* \brief Read config, setup channels.
- * \param locked The logchannels list is locked and this is a reload
  * \param altconf Alternate configuration file to read.
  *
+ * \pre logchannels list is write locked
+ *
  * \retval 0 Success
  * \retval -1 No config found or Failed
  */
-static int init_logger_chain(int locked, const char *altconf)
+static int init_logger_chain(const char *altconf)
 {
        struct logchannel *chan;
        struct ast_config *cfg;
@@ -379,10 +640,6 @@ static int init_logger_chain(int locked, const char *altconf)
                cfg = NULL;
        }
 
-       if (!locked) {
-               AST_RWLIST_WRLOCK(&logchannels);
-       }
-
        /* Set defaults */
        hostname[0] = '\0';
        display_callids = 1;
@@ -398,9 +655,6 @@ static int init_logger_chain(int locked, const char *altconf)
                ast_free(chan);
        }
        global_logmask = 0;
-       if (!locked) {
-               AST_RWLIST_UNLOCK(&logchannels);
-       }
 
        errno = 0;
        /* close syslog */
@@ -408,21 +662,17 @@ static int init_logger_chain(int locked, const char *altconf)
 
        /* If no config file, we're fine, set default options. */
        if (!cfg) {
-               if (!(chan = ast_calloc(1, sizeof(*chan)))) {
+               if (!(chan = ast_calloc(1, sizeof(*chan) + 1))) {
                        fprintf(stderr, "Failed to initialize default logging\n");
                        return -1;
                }
                chan->type = LOGTYPE_CONSOLE;
-               chan->logmask = __LOG_WARNING | __LOG_NOTICE | __LOG_ERROR;
+               chan->logmask = (1 << __LOG_WARNING) | (1 << __LOG_NOTICE) | (1 << __LOG_ERROR)
+                       | (1 << __LOG_VERBOSE);
+               memcpy(&chan->formatter, &logformatter_default, sizeof(chan->formatter));
 
-               if (!locked) {
-                       AST_RWLIST_WRLOCK(&logchannels);
-               }
                AST_RWLIST_INSERT_HEAD(&logchannels, chan, list);
                global_logmask |= chan->logmask;
-               if (!locked) {
-                       AST_RWLIST_UNLOCK(&logchannels);
-               }
 
                return -1;
        }
@@ -474,10 +724,17 @@ static int init_logger_chain(int locked, const char *altconf)
                        fprintf(stderr, "rotatetimestamp option has been deprecated.  Please use rotatestrategy instead.\n");
                }
        }
-
-       if (!locked) {
-               AST_RWLIST_WRLOCK(&logchannels);
+       if ((s = ast_variable_retrieve(cfg, "general", "logger_queue_limit"))) {
+               if (sscanf(s, "%30d", &logger_queue_limit) != 1) {
+                       fprintf(stderr, "logger_queue_limit has an invalid value.  Leaving at default of %d.\n",
+                               logger_queue_limit);
+               }
+               if (logger_queue_limit < 10) {
+                       fprintf(stderr, "logger_queue_limit must be >= 10. Setting to 10.\n");
+                       logger_queue_limit = 10;
+               }
        }
+
        var = ast_variable_browse(cfg, "logfiles");
        for (; var; var = var->next) {
                if (!(chan = make_logchannel(var->name, var->value, var->lineno, 0))) {
@@ -497,10 +754,6 @@ static int init_logger_chain(int locked, const char *altconf)
                qlog = NULL;
        }
 
-       if (!locked) {
-               AST_RWLIST_UNLOCK(&logchannels);
-       }
-
        ast_config_destroy(cfg);
 
        return 0;
@@ -855,7 +1108,7 @@ static int reload_logger(int rotate, const char *altconf)
 
        filesize_reload_needed = 0;
 
-       init_logger_chain(1 /* locked */, altconf);
+       init_logger_chain(altconf);
 
        ast_unload_realtime("queue_log");
        if (logfiles.queue_log) {
@@ -928,14 +1181,10 @@ int ast_logger_rotate()
 int ast_logger_rotate_channel(const char *log_channel)
 {
        struct logchannel *f;
-       int success = 0;
-
-       struct ast_str *filename = ast_str_create(64);
-       if (!filename) {
-               return -1;
-       }
+       int success = AST_LOGGER_FAILURE;
+       char filename[PATH_MAX];
 
-       ast_str_append(&filename, 0, "%s/%s", ast_config_AST_LOG_DIR, log_channel);
+       make_filename(log_channel, filename, sizeof(filename));
 
        AST_RWLIST_WRLOCK(&logchannels);
 
@@ -950,17 +1199,16 @@ int ast_logger_rotate_channel(const char *log_channel)
                if (f->fileptr && (f->fileptr != stdout) && (f->fileptr != stderr)) {
                        fclose(f->fileptr);     /* Close file */
                        f->fileptr = NULL;
-                       if (strcmp(ast_str_buffer(filename), f->filename) == 0) {
+                       if (strcmp(filename, f->filename) == 0) {
                                rotate_file(f->filename);
-                               success = 1;
+                               success = AST_LOGGER_SUCCESS;
                        }
                }
        }
 
-       init_logger_chain(1 /* locked */, NULL);
+       init_logger_chain(NULL);
 
        AST_RWLIST_UNLOCK(&logchannels);
-       ast_free(filename);
 
        return success;
 }
@@ -1007,10 +1255,52 @@ static char *handle_logger_set_level(struct ast_cli_entry *e, int cmd, struct as
        return CLI_SUCCESS;
 }
 
+int ast_logger_get_channels(int (*logentry)(const char *channel, const char *type,
+       const char *status, const char *configuration, void *data), void *data)
+{
+       struct logchannel *chan;
+       struct ast_str *configs = ast_str_create(64);
+       int res = AST_LOGGER_SUCCESS;
+
+       if (!configs) {
+               return AST_LOGGER_ALLOC_ERROR;
+       }
+
+       AST_RWLIST_RDLOCK(&logchannels);
+       AST_RWLIST_TRAVERSE(&logchannels, chan, list) {
+               unsigned int level;
+
+               ast_str_reset(configs);
+
+               for (level = 0; level < ARRAY_LEN(levels); level++) {
+                       if ((chan->logmask & (1 << level)) && levels[level]) {
+                               ast_str_append(&configs, 0, "%s ", levels[level]);
+                       }
+               }
+
+               res = logentry(chan->filename, chan->type == LOGTYPE_CONSOLE ? "Console" :
+                       (chan->type == LOGTYPE_SYSLOG ? "Syslog" : "File"), chan->disabled ?
+                       "Disabled" : "Enabled", ast_str_buffer(configs), data);
+
+               if (res) {
+                       AST_RWLIST_UNLOCK(&logchannels);
+                       ast_free(configs);
+                       configs = NULL;
+                       return AST_LOGGER_FAILURE;
+               }
+       }
+       AST_RWLIST_UNLOCK(&logchannels);
+
+       ast_free(configs);
+       configs = NULL;
+
+       return AST_LOGGER_SUCCESS;
+}
+
 /*! \brief CLI command to show logging system configuration */
 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 "
+#define FORMATL        "%-35.35s %-8.8s %-10.10s %-9.9s "
        struct logchannel *chan;
        switch (cmd) {
        case CLI_INIT:
@@ -1022,15 +1312,17 @@ static char *handle_logger_show_channels(struct ast_cli_entry *e, int cmd, struc
        case CLI_GENERATE:
                return NULL;
        }
-       ast_cli(a->fd, FORMATL, "Channel", "Type", "Status");
+       ast_cli(a->fd, "Logger queue limit: %d\n\n", logger_queue_limit);
+       ast_cli(a->fd, FORMATL, "Channel", "Type", "Formatter", "Status");
        ast_cli(a->fd, "Configuration\n");
-       ast_cli(a->fd, FORMATL, "-------", "----", "------");
+       ast_cli(a->fd, FORMATL, "-------", "----", "---------", "------");
        ast_cli(a->fd, "-------------\n");
        AST_RWLIST_RDLOCK(&logchannels);
        AST_RWLIST_TRAVERSE(&logchannels, chan, list) {
                unsigned int level;
 
                ast_cli(a->fd, FORMATL, chan->filename, chan->type == LOGTYPE_CONSOLE ? "Console" : (chan->type == LOGTYPE_SYSLOG ? "Syslog" : "File"),
+                       chan->formatter.name,
                        chan->disabled ? "Disabled" : "Enabled");
                ast_cli(a->fd, " - ");
                for (level = 0; level < ARRAY_LEN(levels); level++) {
@@ -1046,10 +1338,38 @@ static char *handle_logger_show_channels(struct ast_cli_entry *e, int cmd, struc
        return CLI_SUCCESS;
 }
 
-static char *handle_logger_add_channel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+int ast_logger_create_channel(const char *log_channel, const char *components)
 {
        struct logchannel *chan;
 
+       if (ast_strlen_zero(components)) {
+               return AST_LOGGER_DECLINE;
+       }
+
+       AST_RWLIST_WRLOCK(&logchannels);
+
+       chan = find_logchannel(log_channel);
+       if (chan) {
+               AST_RWLIST_UNLOCK(&logchannels);
+               return AST_LOGGER_FAILURE;
+       }
+
+       chan = make_logchannel(log_channel, components, 0, 1);
+       if (!chan) {
+               AST_RWLIST_UNLOCK(&logchannels);
+               return AST_LOGGER_ALLOC_ERROR;
+       }
+
+       AST_RWLIST_INSERT_HEAD(&logchannels, chan, list);
+       global_logmask |= chan->logmask;
+
+       AST_RWLIST_UNLOCK(&logchannels);
+
+       return AST_LOGGER_SUCCESS;
+}
+
+static char *handle_logger_add_channel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
        switch (cmd) {
        case CLI_INIT:
                e->command = "logger add channel";
@@ -1058,7 +1378,9 @@ static char *handle_logger_add_channel(struct ast_cli_entry *e, int cmd, struct
                        "       Adds a temporary logger channel. This logger channel\n"
                        "       will exist until removed or until Asterisk is restarted.\n"
                        "       <levels> is a comma-separated list of desired logger\n"
-                       "       levels such as: verbose,warning,error\n";
+                       "       levels such as: verbose,warning,error\n"
+                       "       An optional formatter may be specified with the levels;\n"
+                       "       valid values are '[json]' and '[default]'.\n";
                return NULL;
        case CLI_GENERATE:
                return NULL;
@@ -1068,31 +1390,43 @@ static char *handle_logger_add_channel(struct ast_cli_entry *e, int cmd, struct
                return CLI_SHOWUSAGE;
        }
 
-       AST_RWLIST_WRLOCK(&logchannels);
-       AST_RWLIST_TRAVERSE(&logchannels, chan, list) {
-               if (!strcmp(chan->filename, a->argv[3])) {
-                       break;
-               }
-       }
-
-       if (chan) {
-               AST_RWLIST_UNLOCK(&logchannels);
+       switch (ast_logger_create_channel(a->argv[3], a->argv[4])) {
+       case AST_LOGGER_SUCCESS:
+               return CLI_SUCCESS;
+       case AST_LOGGER_FAILURE:
                ast_cli(a->fd, "Logger channel '%s' already exists\n", a->argv[3]);
                return CLI_SUCCESS;
+       case AST_LOGGER_DECLINE:
+       case AST_LOGGER_ALLOC_ERROR:
+       default:
+               ast_cli(a->fd, "ERROR: Unable to create log channel '%s'\n", a->argv[3]);
+               return CLI_FAILURE;
        }
+}
 
-       chan = make_logchannel(a->argv[3], a->argv[4], 0, 1);
-       if (chan) {
-               AST_RWLIST_INSERT_HEAD(&logchannels, chan, list);
-               global_logmask |= chan->logmask;
+int ast_logger_remove_channel(const char *log_channel)
+{
+       struct logchannel *chan;
+
+       AST_RWLIST_WRLOCK(&logchannels);
+
+       chan = find_logchannel(log_channel);
+       if (chan && chan->dynamic) {
+               AST_RWLIST_REMOVE(&logchannels, chan, list);
+       } else {
                AST_RWLIST_UNLOCK(&logchannels);
-               return CLI_SUCCESS;
+               return AST_LOGGER_FAILURE;
        }
-
        AST_RWLIST_UNLOCK(&logchannels);
-       ast_cli(a->fd, "ERROR: Unable to create log channel '%s'\n", a->argv[3]);
 
-       return CLI_FAILURE;
+       if (chan->fileptr) {
+               fclose(chan->fileptr);
+               chan->fileptr = NULL;
+       }
+       ast_free(chan);
+       chan = NULL;
+
+       return AST_LOGGER_SUCCESS;
 }
 
 static char *handle_logger_remove_channel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
@@ -1131,39 +1465,19 @@ static char *handle_logger_remove_channel(struct ast_cli_entry *e, int cmd, stru
                return CLI_SHOWUSAGE;
        }
 
-       AST_RWLIST_WRLOCK(&logchannels);
-       AST_RWLIST_TRAVERSE_SAFE_BEGIN(&logchannels, chan, list) {
-               if (chan->dynamic && !strcmp(chan->filename, a->argv[3])) {
-                       AST_RWLIST_REMOVE_CURRENT(list);
-                       break;
-               }
-       }
-       AST_RWLIST_TRAVERSE_SAFE_END;
-       AST_RWLIST_UNLOCK(&logchannels);
-
-       if (!chan) {
+       switch (ast_logger_remove_channel(a->argv[3])) {
+       case AST_LOGGER_SUCCESS:
+               ast_cli(a->fd, "Removed dynamic logger channel '%s'\n", a->argv[3]);
+               return CLI_SUCCESS;
+       case AST_LOGGER_FAILURE:
                ast_cli(a->fd, "Unable to find dynamic logger channel '%s'\n", a->argv[3]);
                return CLI_SUCCESS;
+       default:
+               ast_cli(a->fd, "Internal failure attempting to delete dynamic logger channel '%s'\n", a->argv[3]);
+               return CLI_FAILURE;
        }
-
-       ast_cli(a->fd, "Removed dynamic logger channel '%s'\n", chan->filename);
-       if (chan->fileptr) {
-               fclose(chan->fileptr);
-               chan->fileptr = NULL;
-       }
-       ast_free(chan);
-       chan = NULL;
-
-       return CLI_SUCCESS;
 }
 
-struct verb {
-       void (*verboser)(const char *string);
-       AST_LIST_ENTRY(verb) list;
-};
-
-static AST_RWLIST_HEAD_STATIC(verbosers, verb);
-
 static struct ast_cli_entry cli_logger[] = {
        AST_CLI_DEFINE(handle_logger_show_channels, "List configured log channels"),
        AST_CLI_DEFINE(handle_logger_reload, "Reopens the log files"),
@@ -1184,97 +1498,16 @@ static struct sigaction handle_SIGXFSZ = {
        .sa_flags = SA_RESTART,
 };
 
-static void ast_log_vsyslog(struct logmsg *msg)
-{
-       char buf[BUFSIZ];
-       int syslog_level = ast_syslog_priority_from_loglevel(msg->level);
-       char call_identifier_str[13];
-
-       if (msg->callid) {
-               snprintf(call_identifier_str, sizeof(call_identifier_str), "[C-%08x]", msg->callid);
-       } else {
-               call_identifier_str[0] = '\0';
-       }
-
-       if (syslog_level < 0) {
-               /* we are locked here, so cannot ast_log() */
-               fprintf(stderr, "ast_log_vsyslog called with bogus level: %d\n", msg->level);
-               return;
-       }
-
-       snprintf(buf, sizeof(buf), "%s[%d]%s: %s:%d in %s: %s",
-                levels[msg->level], msg->lwp, call_identifier_str, msg->file, msg->line, msg->function, msg->message);
-
-       term_strip(buf, buf, strlen(buf) + 1);
-       syslog(syslog_level, "%s", buf);
-}
-
-static char *logger_strip_verbose_magic(const char *message, int level)
-{
-       const char *begin, *end;
-       char *stripped_message, *dst;
-       char magic = -(level + 1);
-
-       if (!(stripped_message = ast_malloc(strlen(message) + 1))) {
-               return NULL;
-       }
-
-       begin = message;
-       dst = stripped_message;
-       do {
-               end = strchr(begin, magic);
-               if (end) {
-                       size_t len = end - begin;
-                       memcpy(dst, begin, len);
-                       begin = end + 1;
-                       dst += len;
-               } else {
-                       strcpy(dst, begin); /* safe */
-                       break;
-               }
-       } while (1);
-
-       return stripped_message;
-}
-
 /*! \brief Print a normal log message to the channels */
 static void logger_print_normal(struct logmsg *logmsg)
 {
        struct logchannel *chan = NULL;
        char buf[BUFSIZ];
-       struct verb *v = NULL;
-       char *tmpmsg;
        int level = 0;
 
-       if (logmsg->level == __LOG_VERBOSE) {
-
-               /* Iterate through the list of verbosers and pass them the log message string */
-               AST_RWLIST_RDLOCK(&verbosers);
-               AST_RWLIST_TRAVERSE(&verbosers, v, list)
-                       v->verboser(logmsg->message);
-               AST_RWLIST_UNLOCK(&verbosers);
-
-               level = VERBOSE_MAGIC2LEVEL(logmsg->message);
-
-               tmpmsg = logger_strip_verbose_magic(logmsg->message, level);
-               if (tmpmsg) {
-                       ast_string_field_set(logmsg, message, tmpmsg);
-                       ast_free(tmpmsg);
-               }
-       }
-
        AST_RWLIST_RDLOCK(&logchannels);
-
        if (!AST_RWLIST_EMPTY(&logchannels)) {
                AST_RWLIST_TRAVERSE(&logchannels, chan, list) {
-                       char call_identifier_str[13];
-
-                       if (logmsg->callid) {
-                               snprintf(call_identifier_str, sizeof(call_identifier_str), "[C-%08x]", logmsg->callid);
-                       } else {
-                               call_identifier_str[0] = '\0';
-                       }
-
 
                        /* If the channel is disabled, then move on to the next one */
                        if (chan->disabled) {
@@ -1285,68 +1518,75 @@ static void logger_print_normal(struct logmsg *logmsg)
                                continue;
                        }
 
-                       /* Check syslog channels */
-                       if (chan->type == LOGTYPE_SYSLOG && (chan->logmask & (1 << logmsg->level))) {
-                               ast_log_vsyslog(logmsg);
-                       /* Console channels */
-                       } else if (chan->type == LOGTYPE_CONSOLE && (chan->logmask & (1 << logmsg->level))) {
-                               char linestr[128];
-
-                               /* If the level is verbose, then skip it */
-                               if (logmsg->level == __LOG_VERBOSE)
-                                       continue;
-
-                               /* Turn the numerical line number into a string */
-                               snprintf(linestr, sizeof(linestr), "%d", logmsg->line);
-                               /* Build string to print out */
-                               snprintf(buf, sizeof(buf), "[%s] " COLORIZE_FMT "[%d]%s: " COLORIZE_FMT ":" COLORIZE_FMT " " COLORIZE_FMT ": %s",
-                                        logmsg->date,
-                                        COLORIZE(colors[logmsg->level], 0, logmsg->level_name),
-                                        logmsg->lwp,
-                                        call_identifier_str,
-                                        COLORIZE(COLOR_BRWHITE, 0, logmsg->file),
-                                        COLORIZE(COLOR_BRWHITE, 0, linestr),
-                                        COLORIZE(COLOR_BRWHITE, 0, logmsg->function),
-                                        logmsg->message);
-                               /* Print out */
-                               ast_console_puts_mutable(buf, logmsg->level);
-                       /* File channels */
-                       } else if (chan->type == LOGTYPE_FILE && (chan->logmask & (1 << logmsg->level))) {
-                               int res = 0;
-
-                               /* If no file pointer exists, skip it */
-                               if (!chan->fileptr) {
-                                       continue;
+                       if (!(chan->logmask & (1 << logmsg->level))) {
+                               continue;
+                       }
+
+                       switch (chan->type) {
+                       case LOGTYPE_SYSLOG:
+                               {
+                                       int syslog_level = ast_syslog_priority_from_loglevel(logmsg->level);
+
+                                       if (syslog_level < 0) {
+                                               /* we are locked here, so cannot ast_log() */
+                                               fprintf(stderr, "ast_log_vsyslog called with bogus level: %d\n", logmsg->level);
+                                               continue;
+                                       }
+
+                                       /* Don't use LOG_MAKEPRI because it's broken in glibc<2.17 */
+                                       syslog_level = chan->facility | syslog_level; /* LOG_MAKEPRI(chan->facility, syslog_level); */
+                                       if (!chan->formatter.format_log(chan, logmsg, buf, BUFSIZ)) {
+                                               syslog(syslog_level, "%s", buf);
+                                       }
+                               }
+                               break;
+                       case LOGTYPE_CONSOLE:
+                               if (!chan->formatter.format_log(chan, logmsg, buf, BUFSIZ)) {
+                                       ast_console_puts_mutable_full(buf, logmsg->level, logmsg->sublevel);
                                }
+                               break;
+                       case LOGTYPE_FILE:
+                               {
+                                       int res = 0;
+
+                                       if (!chan->fileptr) {
+                                               continue;
+                                       }
+
+                                       if (chan->formatter.format_log(chan, logmsg, buf, BUFSIZ)) {
+                                               continue;
+                                       }
 
-                               /* Print out to the file */
-                               res = fprintf(chan->fileptr, "[%s] %s[%d]%s %s: %s",
-                                             logmsg->date, logmsg->level_name, logmsg->lwp, call_identifier_str,
-                                             logmsg->file, term_strip(buf, logmsg->message, BUFSIZ));
-                               if (res <= 0 && !ast_strlen_zero(logmsg->message)) {
-                                       fprintf(stderr, "**** Asterisk Logging Error: ***********\n");
-                                       if (errno == ENOMEM || errno == ENOSPC)
-                                               fprintf(stderr, "Asterisk logging error: Out of disk space, can't log to log file %s\n", chan->filename);
-                                       else
-                                               fprintf(stderr, "Logger Warning: Unable to write to log file '%s': %s (disabled)\n", chan->filename, strerror(errno));
-                                       /*** DOCUMENTATION
-                                               <managerEventInstance>
-                                                       <synopsis>Raised when a logging channel is disabled.</synopsis>
-                                                       <syntax>
-                                                               <parameter name="Channel">
-                                                                       <para>The name of the logging channel.</para>
-                                                               </parameter>
-                                                       </syntax>
-                                               </managerEventInstance>
-                                       ***/
-                                       manager_event(EVENT_FLAG_SYSTEM, "LogChannel", "Channel: %s\r\nEnabled: No\r\nReason: %d - %s\r\n", chan->filename, errno, strerror(errno));
-                                       chan->disabled = 1;
-                               } else if (res > 0) {
-                                       fflush(chan->fileptr);
+                                       /* Print out to the file */
+                                       res = fprintf(chan->fileptr, "%s", buf);
+                                       if (res > 0) {
+                                               fflush(chan->fileptr);
+                                       } else if (res <= 0 && !ast_strlen_zero(logmsg->message)) {
+                                               fprintf(stderr, "**** Asterisk Logging Error: ***********\n");
+                                               if (errno == ENOMEM || errno == ENOSPC) {
+                                                       fprintf(stderr, "Asterisk logging error: Out of disk space, can't log to log file %s\n", chan->filename);
+                                               } else {
+                                                       fprintf(stderr, "Logger Warning: Unable to write to log file '%s': %s (disabled)\n", chan->filename, strerror(errno));
+                                               }
+
+                                               /*** DOCUMENTATION
+                                                       <managerEventInstance>
+                                                               <synopsis>Raised when a logging channel is disabled.</synopsis>
+                                                               <syntax>
+                                                                       <parameter name="Channel">
+                                                                               <para>The name of the logging channel.</para>
+                                                                       </parameter>
+                                                               </syntax>
+                                                       </managerEventInstance>
+                                               ***/
+                                               manager_event(EVENT_FLAG_SYSTEM, "LogChannel", "Channel: %s\r\nEnabled: No\r\nReason: %d - %s\r\n", chan->filename, errno, strerror(errno));
+                                               chan->disabled = 1;
+                                       }
                                }
+                               break;
                        }
                }
-       } else if (logmsg->level != __LOG_VERBOSE) {
+       } else if (logmsg->level != __LOG_VERBOSE || option_verbose >= logmsg->sublevel) {
                fputs(logmsg->message, stdout);
        }
 
@@ -1361,6 +1601,79 @@ static void logger_print_normal(struct logmsg *logmsg)
        return;
 }
 
+static struct logmsg * __attribute__((format(printf, 7, 0))) format_log_message_ap(int level,
+       int sublevel, const char *file, int line, const char *function, ast_callid callid,
+       const char *fmt, va_list ap)
+{
+       struct logmsg *logmsg = NULL;
+       struct ast_str *buf = NULL;
+       struct ast_tm tm;
+       struct timeval now = ast_tvnow();
+       int res = 0;
+       char datestring[256];
+
+       if (!(buf = ast_str_thread_get(&log_buf, LOG_BUF_INIT_SIZE))) {
+               return NULL;
+       }
+
+       /* Build string */
+       res = ast_str_set_va(&buf, BUFSIZ, fmt, ap);
+
+       /* If the build failed, then abort and free this structure */
+       if (res == AST_DYNSTR_BUILD_FAILED) {
+               return NULL;
+       }
+
+       /* Create a new logging message */
+       if (!(logmsg = ast_calloc_with_stringfields(1, struct logmsg, res + 128))) {
+               return NULL;
+       }
+
+       /* Copy string over */
+       ast_string_field_set(logmsg, message, ast_str_buffer(buf));
+
+       /* Set type */
+       if (level == __LOG_VERBOSE) {
+               logmsg->type = LOGMSG_VERBOSE;
+       } else {
+               logmsg->type = LOGMSG_NORMAL;
+       }
+
+       if (display_callids && callid) {
+               logmsg->callid = callid;
+       }
+
+       /* Create our date/time */
+       ast_localtime(&now, &tm, NULL);
+       ast_strftime(datestring, sizeof(datestring), dateformat, &tm);
+       ast_string_field_set(logmsg, date, datestring);
+
+       /* Copy over data */
+       logmsg->level = level;
+       logmsg->sublevel = sublevel;
+       logmsg->line = line;
+       ast_string_field_set(logmsg, level_name, levels[level]);
+       ast_string_field_set(logmsg, file, file);
+       ast_string_field_set(logmsg, function, function);
+       logmsg->lwp = ast_get_tid();
+
+       return logmsg;
+}
+
+static struct logmsg * __attribute__((format(printf, 7, 0))) format_log_message(int level,
+       int sublevel, const char *file, int line, const char *function, ast_callid callid,
+       const char *fmt, ...)
+{
+       struct logmsg *logmsg;
+       va_list ap;
+
+       va_start(ap, fmt);
+       logmsg = format_log_message_ap(level, sublevel, file, line, function, callid, fmt, ap);
+       va_end(ap);
+
+       return logmsg;
+}
+
 /*! \brief Actual logging thread */
 static void *logger_thread(void *data)
 {
@@ -1377,8 +1690,21 @@ static void *logger_thread(void *data)
                                ast_cond_wait(&logcond, &logmsgs.lock);
                        }
                }
+
+               if (high_water_alert) {
+                       msg = format_log_message(__LOG_WARNING, 0, "logger", 0, "***", 0,
+                               "Logging resumed.  %d message%s discarded.\n",
+                               logger_messages_discarded, logger_messages_discarded == 1 ? "" : "s");
+                       if (msg) {
+                               AST_LIST_INSERT_TAIL(&logmsgs, msg, list);
+                       }
+                       high_water_alert = 0;
+                       logger_messages_discarded = 0;
+               }
+
                next = AST_LIST_FIRST(&logmsgs);
                AST_LIST_HEAD_INIT_NOLOCK(&logmsgs);
+               logger_queue_size = 0;
                AST_LIST_UNLOCK(&logmsgs);
 
                /* Otherwise go through and process each message in the order added */
@@ -1429,6 +1755,11 @@ static void logger_queue_init(void)
        }
 }
 
+int ast_is_logger_initialized(void)
+{
+       return logger_initialized;
+}
+
 /*!
  * \brief Start the ast_queue_log() logger.
  *
@@ -1481,7 +1812,9 @@ int init_logger(void)
        ast_mkdir(ast_config_AST_LOG_DIR, 0777);
 
        /* create log channels */
-       res = init_logger_chain(0 /* locked */, NULL);
+       AST_RWLIST_WRLOCK(&logchannels);
+       res = init_logger_chain(NULL);
+       AST_RWLIST_UNLOCK(&logchannels);
        ast_verb_update();
        logger_initialized = 1;
        if (res) {
@@ -1494,7 +1827,6 @@ int init_logger(void)
 void close_logger(void)
 {
        struct logchannel *f = NULL;
-       struct verb *cur = NULL;
 
        ast_cli_unregister_multiple(cli_logger, ARRAY_LEN(cli_logger));
 
@@ -1506,14 +1838,9 @@ void close_logger(void)
        ast_cond_signal(&logcond);
        AST_LIST_UNLOCK(&logmsgs);
 
-       if (logthread != AST_PTHREADT_NULL)
+       if (logthread != AST_PTHREADT_NULL) {
                pthread_join(logthread, NULL);
-
-       AST_RWLIST_WRLOCK(&verbosers);
-       while ((cur = AST_LIST_REMOVE_HEAD(&verbosers, list))) {
-               ast_free(cur);
        }
-       AST_RWLIST_UNLOCK(&verbosers);
 
        AST_RWLIST_WRLOCK(&logchannels);
 
@@ -1542,13 +1869,7 @@ void ast_callid_strnprint(char *buffer, size_t buffer_size, ast_callid callid)
 
 ast_callid ast_create_callid(void)
 {
-       ast_callid call;
-
-       call = ast_atomic_fetchadd_int(&next_unique_callid, +1);
-#ifdef TEST_FRAMEWORK
-       ast_debug(3, "CALL_ID [C-%08x] created by thread.\n", call);
-#endif
-       return call;
+       return ast_atomic_fetchadd_int(&next_unique_callid, +1);
 }
 
 ast_callid ast_read_threadstorage_callid(void)
@@ -1558,7 +1879,6 @@ ast_callid ast_read_threadstorage_callid(void)
        callid = ast_threadstorage_get(&unique_callid, sizeof(*callid));
 
        return callid ? *callid : 0;
-
 }
 
 int ast_callid_threadassoc_change(ast_callid callid)
@@ -1566,23 +1886,10 @@ int ast_callid_threadassoc_change(ast_callid callid)
        ast_callid *id = ast_threadstorage_get(&unique_callid, sizeof(*id));
 
        if (!id) {
-               ast_log(LOG_ERROR, "Failed to allocate thread storage.\n");
                return -1;
        }
 
-       if (*id && (*id != callid)) {
-#ifdef TEST_FRAMEWORK
-               ast_debug(3, "CALL_ID [C-%08x] being removed from thread.\n", *id);
-#endif
-               *id = 0;
-       }
-
-       if (!(*id) && callid) {
-               *id = callid;
-#ifdef TEST_FRAMEWORK
-               ast_debug(3, "CALL_ID [C-%08x] bound to thread.\n", callid);
-#endif
-       }
+       *id = callid;
 
        return 0;
 }
@@ -1592,21 +1899,17 @@ int ast_callid_threadassoc_add(ast_callid callid)
        ast_callid *pointing;
 
        pointing = ast_threadstorage_get(&unique_callid, sizeof(*pointing));
-       if (!(pointing)) {
-               ast_log(LOG_ERROR, "Failed to allocate thread storage.\n");
+       if (!pointing) {
                return -1;
        }
 
-       if (!(*pointing)) {
-               *pointing = callid;
-#ifdef TEST_FRAMEWORK
-               ast_debug(3, "CALL_ID [C-%08x] bound to thread.\n", callid);
-#endif
-       } else {
-               ast_log(LOG_WARNING, "Attempted to ast_callid_threadassoc_add on thread already associated with a callid.\n");
+       if (*pointing) {
+               ast_log(LOG_ERROR, "ast_callid_threadassoc_add(C-%08x) on thread "
+                       "already associated with callid [C-%08x].\n", callid, *pointing);
                return 1;
        }
 
+       *pointing = callid;
        return 0;
 }
 
@@ -1615,21 +1918,16 @@ int ast_callid_threadassoc_remove(void)
        ast_callid *pointing;
 
        pointing = ast_threadstorage_get(&unique_callid, sizeof(*pointing));
-       if (!(pointing)) {
-               ast_log(LOG_ERROR, "Failed to allocate thread storage.\n");
+       if (!pointing) {
                return -1;
        }
 
-       if (!(*pointing)) {
-               ast_log(LOG_ERROR, "Tried to clean callid thread storage with no callid in thread storage.\n");
-               return -1;
-       } else {
-#ifdef TEST_FRAMEWORK
-               ast_debug(3, "CALL_ID [C-%08x] being removed from thread.\n", *pointing);
-#endif
+       if (*pointing) {
                *pointing = 0;
                return 0;
        }
+
+       return -1;
 }
 
 int ast_callid_threadstorage_auto(ast_callid *callid)
@@ -1665,74 +1963,36 @@ void ast_callid_threadstorage_auto_clean(ast_callid callid, int callid_created)
 /*!
  * \brief send log messages to syslog and/or the console
  */
-static void __attribute__((format(printf, 6, 0))) ast_log_full(int level, const char *file, int line, const char *function, ast_callid callid, const char *fmt, va_list ap)
+static void __attribute__((format(printf, 7, 0))) ast_log_full(int level, int sublevel,
+       const char *file, int line, const char *function, ast_callid callid,
+       const char *fmt, va_list ap)
 {
        struct logmsg *logmsg = NULL;
-       struct ast_str *buf = NULL;
-       struct ast_tm tm;
-       struct timeval now = ast_tvnow();
-       int res = 0;
-       char datestring[256];
 
-       if (!(buf = ast_str_thread_get(&log_buf, LOG_BUF_INIT_SIZE)))
+       if (level == __LOG_VERBOSE && ast_opt_remote && ast_opt_exec) {
                return;
+       }
 
-       if (level != __LOG_VERBOSE && AST_RWLIST_EMPTY(&logchannels)) {
-               /*
-                * we don't have the logger chain configured yet,
-                * so just log to stdout
-                */
-               int result;
-               result = ast_str_set_va(&buf, BUFSIZ, fmt, ap); /* XXX BUFSIZ ? */
-               if (result != AST_DYNSTR_BUILD_FAILED) {
-                       term_filter_escapes(ast_str_buffer(buf));
-                       fputs(ast_str_buffer(buf), stdout);
+       AST_LIST_LOCK(&logmsgs);
+       if (logger_queue_size >= logger_queue_limit && !close_logger_thread) {
+               logger_messages_discarded++;
+               if (!high_water_alert && !close_logger_thread) {
+                       logmsg = format_log_message(__LOG_WARNING, 0, "logger", 0, "***", 0,
+                               "Log queue threshold (%d) exceeded.  Discarding new messages.\n", logger_queue_limit);
+                       AST_LIST_INSERT_TAIL(&logmsgs, logmsg, list);
+                       high_water_alert = 1;
+                       ast_cond_signal(&logcond);
                }
+               AST_LIST_UNLOCK(&logmsgs);
                return;
        }
+       AST_LIST_UNLOCK(&logmsgs);
 
-       /* Ignore anything that never gets logged anywhere */
-       if (level != __LOG_VERBOSE && !(global_logmask & (1 << level)))
-               return;
-
-       /* Build string */
-       res = ast_str_set_va(&buf, BUFSIZ, fmt, ap);
-
-       /* If the build failed, then abort and free this structure */
-       if (res == AST_DYNSTR_BUILD_FAILED)
+       logmsg = format_log_message_ap(level, sublevel, file, line, function, callid, fmt, ap);
+       if (!logmsg) {
                return;
-
-       /* Create a new logging message */
-       if (!(logmsg = ast_calloc_with_stringfields(1, struct logmsg, res + 128)))
-               return;
-
-       /* Copy string over */
-       ast_string_field_set(logmsg, message, ast_str_buffer(buf));
-
-       /* Set type */
-       if (level == __LOG_VERBOSE) {
-               logmsg->type = LOGMSG_VERBOSE;
-       } else {
-               logmsg->type = LOGMSG_NORMAL;
-       }
-
-       if (display_callids && callid) {
-               logmsg->callid = callid;
        }
 
-       /* Create our date/time */
-       ast_localtime(&now, &tm, NULL);
-       ast_strftime(datestring, sizeof(datestring), dateformat, &tm);
-       ast_string_field_set(logmsg, date, datestring);
-
-       /* Copy over data */
-       logmsg->level = level;
-       logmsg->line = line;
-       ast_string_field_set(logmsg, level_name, levels[level]);
-       ast_string_field_set(logmsg, file, file);
-       ast_string_field_set(logmsg, function, function);
-       logmsg->lwp = ast_get_tid();
-
        /* If the logger thread is active, append it to the tail end of the list - otherwise skip that step */
        if (logthread != AST_PTHREADT_NULL) {
                AST_LIST_LOCK(&logmsgs);
@@ -1741,6 +2001,7 @@ static void __attribute__((format(printf, 6, 0))) ast_log_full(int level, const
                        logmsg_free(logmsg);
                } else {
                        AST_LIST_INSERT_TAIL(&logmsgs, logmsg, list);
+                       logger_queue_size++;
                        ast_cond_signal(&logcond);
                }
                AST_LIST_UNLOCK(&logmsgs);
@@ -1761,7 +2022,7 @@ void ast_log(int level, const char *file, int line, const char *function, const
        if (level == __LOG_VERBOSE) {
                __ast_verbose_ap(file, line, function, 0, callid, fmt, ap);
        } else {
-               ast_log_full(level, file, line, function, callid, fmt, ap);
+               ast_log_full(level, -1, file, line, function, callid, fmt, ap);
        }
        va_end(ap);
 }
@@ -1785,7 +2046,7 @@ void ast_log_safe(int level, const char *file, int line, const char *function, c
        callid = ast_read_threadstorage_callid();
 
        va_start(ap, fmt);
-       ast_log_full(level, file, line, function, callid, fmt, ap);
+       ast_log_full(level, -1, file, line, function, callid, fmt, ap);
        va_end(ap);
 
        /* Clear flag so the next allocation failure can be logged. */
@@ -1796,7 +2057,7 @@ void ast_log_callid(int level, const char *file, int line, const char *function,
 {
        va_list ap;
        va_start(ap, fmt);
-       ast_log_full(level, file, line, function, callid, fmt, ap);
+       ast_log_full(level, -1, file, line, function, callid, fmt, ap);
        va_end(ap);
 }
 
@@ -1831,53 +2092,7 @@ void ast_log_backtrace(void)
 
 void __ast_verbose_ap(const char *file, int line, const char *func, int level, ast_callid callid, const char *fmt, va_list ap)
 {
-       const char *p;
-       struct ast_str *prefixed, *buf;
-       int res = 0;
-       signed char magic = level > 9 ? -10 : -level - 1; /* 0 => -1, 1 => -2, etc.  Can't pass NUL, as it is EOS-delimiter */
-
-       /* For compatibility with modules still calling ast_verbose() directly instead of using ast_verb() */
-       if (level < 0) {
-               if (!strncmp(fmt, VERBOSE_PREFIX_4, strlen(VERBOSE_PREFIX_4))) {
-                       magic = -5;
-               } else if (!strncmp(fmt, VERBOSE_PREFIX_3, strlen(VERBOSE_PREFIX_3))) {
-                       magic = -4;
-               } else if (!strncmp(fmt, VERBOSE_PREFIX_2, strlen(VERBOSE_PREFIX_2))) {
-                       magic = -3;
-               } else if (!strncmp(fmt, VERBOSE_PREFIX_1, strlen(VERBOSE_PREFIX_1))) {
-                       magic = -2;
-               } else {
-                       magic = -1;
-               }
-       }
-
-       if (!(prefixed = ast_str_thread_get(&verbose_buf, VERBOSE_BUF_INIT_SIZE)) ||
-           !(buf = ast_str_thread_get(&verbose_build_buf, VERBOSE_BUF_INIT_SIZE))) {
-               return;
-       }
-
-       res = ast_str_set_va(&buf, 0, fmt, ap);
-       /* If the build failed then we can drop this allocated message */
-       if (res == AST_DYNSTR_BUILD_FAILED) {
-               return;
-       }
-
-       ast_str_reset(prefixed);
-
-       /* for every newline found in the buffer add verbose prefix data */
-       fmt = ast_str_buffer(buf);
-       do {
-               if (!(p = strchr(fmt, '\n'))) {
-                       p = strchr(fmt, '\0') - 1;
-               }
-               ++p;
-
-               ast_str_append(&prefixed, 0, "%c", (char)magic);
-               ast_str_append_substr(&prefixed, 0, fmt, p - fmt);
-               fmt = p;
-       } while (p && *p);
-
-       ast_log_callid(__LOG_VERBOSE, file, line, func, callid, "%s", ast_str_buffer(prefixed));
+       ast_log_full(__LOG_VERBOSE, level, file, line, func, callid, fmt, ap);
 }
 
 void __ast_verbose(const char *file, int line, const char *func, int level, const char *fmt, ...)
@@ -1900,21 +2115,6 @@ void __ast_verbose_callid(const char *file, int line, const char *func, int leve
        va_end(ap);
 }
 
-/* No new code should use this directly, but we have the ABI for backwards compat */
-#undef ast_verbose
-void __attribute__((format(printf, 1,2))) ast_verbose(const char *fmt, ...);
-void ast_verbose(const char *fmt, ...)
-{
-       ast_callid callid;
-       va_list ap;
-
-       callid = ast_read_threadstorage_callid();
-
-       va_start(ap, fmt);
-       __ast_verbose_ap("", 0, "", 0, callid, fmt, ap);
-       va_end(ap);
-}
-
 /*! Console verbosity level node. */
 struct verb_console {
        /*! List node link */
@@ -2057,40 +2257,6 @@ void ast_verb_console_set(int verb_level)
        ast_verb_update();
 }
 
-int ast_register_verbose(void (*v)(const char *string))
-{
-       struct verb *verb;
-
-       if (!(verb = ast_malloc(sizeof(*verb))))
-               return -1;
-
-       verb->verboser = v;
-
-       AST_RWLIST_WRLOCK(&verbosers);
-       AST_RWLIST_INSERT_HEAD(&verbosers, verb, list);
-       AST_RWLIST_UNLOCK(&verbosers);
-
-       return 0;
-}
-
-int ast_unregister_verbose(void (*v)(const char *string))
-{
-       struct verb *cur;
-
-       AST_RWLIST_WRLOCK(&verbosers);
-       AST_RWLIST_TRAVERSE_SAFE_BEGIN(&verbosers, cur, list) {
-               if (cur->verboser == v) {
-                       AST_RWLIST_REMOVE_CURRENT(list);
-                       ast_free(cur);
-                       break;
-               }
-       }
-       AST_RWLIST_TRAVERSE_SAFE_END;
-       AST_RWLIST_UNLOCK(&verbosers);
-
-       return cur ? 0 : -1;
-}
-
 static void update_logchannels(void)
 {
        struct logchannel *cur;
@@ -2194,3 +2360,12 @@ const char *ast_logger_get_dateformat(void)
        return dateformat;
 }
 
+void ast_logger_set_queue_limit(int queue_limit)
+{
+       logger_queue_limit = queue_limit;
+}
+
+int ast_logger_get_queue_limit(void)
+{
+       return logger_queue_limit;
+}