Support an alternate configuration file for the 'logger reload' command.
[asterisk/asterisk.git] / main / logger.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Asterisk Logger
22  *
23  * Logging routines
24  *
25  * \author Mark Spencer <markster@digium.com>
26  */
27
28 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 /* When we include logger.h again it will trample on some stuff in syslog.h, but
33  * nothing we care about in here. */
34 #include <syslog.h>
35
36 #include "asterisk/_private.h"
37 #include "asterisk/paths.h"     /* use ast_config_AST_LOG_DIR */
38 #include "asterisk/logger.h"
39 #include "asterisk/lock.h"
40 #include "asterisk/channel.h"
41 #include "asterisk/config.h"
42 #include "asterisk/term.h"
43 #include "asterisk/cli.h"
44 #include "asterisk/utils.h"
45 #include "asterisk/manager.h"
46 #include "asterisk/threadstorage.h"
47 #include "asterisk/strings.h"
48 #include "asterisk/pbx.h"
49 #include "asterisk/app.h"
50 #include "asterisk/syslog.h"
51
52 #include <signal.h>
53 #include <time.h>
54 #include <sys/stat.h>
55 #include <fcntl.h>
56 #ifdef HAVE_BKTR
57 #include <execinfo.h>
58 #define MAX_BACKTRACE_FRAMES 20
59 #  if defined(HAVE_DLADDR) && defined(HAVE_BFD) && defined(BETTER_BACKTRACES)
60 #    include <dlfcn.h>
61 #    include <bfd.h>
62 #  endif
63 #endif
64
65 static char dateformat[256] = "%b %e %T";               /* Original Asterisk Format */
66
67 static char queue_log_name[256] = QUEUELOG;
68 static char exec_after_rotate[256] = "";
69
70 static int filesize_reload_needed;
71 static unsigned int global_logmask = 0xFFFF;
72 static int queuelog_init;
73
74 static enum rotatestrategy {
75         SEQUENTIAL = 1 << 0,     /* Original method - create a new file, in order */
76         ROTATE = 1 << 1,         /* Rotate all files, such that the oldest file has the highest suffix */
77         TIMESTAMP = 1 << 2,      /* Append the epoch timestamp onto the end of the archived file */
78 } rotatestrategy = SEQUENTIAL;
79
80 static struct {
81         unsigned int queue_log:1;
82         unsigned int queue_log_to_file:1;
83         unsigned int queue_adaptive_realtime:1;
84 } logfiles = { 1 };
85
86 static char hostname[MAXHOSTNAMELEN];
87
88 enum logtypes {
89         LOGTYPE_SYSLOG,
90         LOGTYPE_FILE,
91         LOGTYPE_CONSOLE,
92 };
93
94 struct logchannel {
95         /*! What to log to this channel */
96         unsigned int logmask;
97         /*! If this channel is disabled or not */
98         int disabled;
99         /*! syslog facility */
100         int facility;
101         /*! Type of log channel */
102         enum logtypes type;
103         /*! logfile logging file pointer */
104         FILE *fileptr;
105         /*! Filename */
106         char filename[PATH_MAX];
107         /*! field for linking to list */
108         AST_LIST_ENTRY(logchannel) list;
109         /*! Line number from configuration file */
110         int lineno;
111         /*! Components (levels) from last config load */
112         char components[0];
113 };
114
115 static AST_RWLIST_HEAD_STATIC(logchannels, logchannel);
116
117 enum logmsgtypes {
118         LOGMSG_NORMAL = 0,
119         LOGMSG_VERBOSE,
120 };
121
122 struct logmsg {
123         enum logmsgtypes type;
124         int level;
125         int line;
126         int lwp;
127         AST_DECLARE_STRING_FIELDS(
128                 AST_STRING_FIELD(date);
129                 AST_STRING_FIELD(file);
130                 AST_STRING_FIELD(function);
131                 AST_STRING_FIELD(message);
132                 AST_STRING_FIELD(level_name);
133         );
134         AST_LIST_ENTRY(logmsg) list;
135 };
136
137 static AST_LIST_HEAD_STATIC(logmsgs, logmsg);
138 static pthread_t logthread = AST_PTHREADT_NULL;
139 static ast_cond_t logcond;
140 static int close_logger_thread = 0;
141
142 static FILE *qlog;
143
144 /*! \brief Logging channels used in the Asterisk logging system
145  *
146  * The first 16 levels are reserved for system usage, and the remaining
147  * levels are reserved for usage by dynamic levels registered via
148  * ast_logger_register_level.
149  */
150
151 /* Modifications to this array are protected by the rwlock in the
152  * logchannels list.
153  */
154
155 static char *levels[32] = {
156         "DEBUG",
157         "---EVENT---",          /* no longer used */
158         "NOTICE",
159         "WARNING",
160         "ERROR",
161         "VERBOSE",
162         "DTMF",
163 };
164
165 /*! \brief Colors used in the console for logging */
166 static const int colors[32] = {
167         COLOR_BRGREEN,
168         COLOR_BRBLUE,           /* no longer used */
169         COLOR_YELLOW,
170         COLOR_BRRED,
171         COLOR_RED,
172         COLOR_GREEN,
173         COLOR_BRGREEN,
174         0,
175         0,
176         0,
177         0,
178         0,
179         0,
180         0,
181         0,
182         0,
183         COLOR_BRBLUE,
184         COLOR_BRBLUE,
185         COLOR_BRBLUE,
186         COLOR_BRBLUE,
187         COLOR_BRBLUE,
188         COLOR_BRBLUE,
189         COLOR_BRBLUE,
190         COLOR_BRBLUE,
191         COLOR_BRBLUE,
192         COLOR_BRBLUE,
193         COLOR_BRBLUE,
194         COLOR_BRBLUE,
195         COLOR_BRBLUE,
196         COLOR_BRBLUE,
197         COLOR_BRBLUE,
198         COLOR_BRBLUE,
199 };
200
201 AST_THREADSTORAGE(verbose_buf);
202 #define VERBOSE_BUF_INIT_SIZE   256
203
204 AST_THREADSTORAGE(log_buf);
205 #define LOG_BUF_INIT_SIZE       256
206
207 static void logger_queue_init(void);
208
209 static unsigned int make_components(const char *s, int lineno)
210 {
211         char *w;
212         unsigned int res = 0;
213         char *stringp = ast_strdupa(s);
214         unsigned int x;
215
216         while ((w = strsep(&stringp, ","))) {
217                 w = ast_skip_blanks(w);
218
219                 if (!strcmp(w, "*")) {
220                         res = 0xFFFFFFFF;
221                         break;
222                 } else for (x = 0; x < ARRAY_LEN(levels); x++) {
223                         if (levels[x] && !strcasecmp(w, levels[x])) {
224                                 res |= (1 << x);
225                                 break;
226                         }
227                 }
228         }
229
230         return res;
231 }
232
233 static struct logchannel *make_logchannel(const char *channel, const char *components, int lineno)
234 {
235         struct logchannel *chan;
236         char *facility;
237
238         if (ast_strlen_zero(channel) || !(chan = ast_calloc(1, sizeof(*chan) + strlen(components) + 1)))
239                 return NULL;
240
241         strcpy(chan->components, components);
242         chan->lineno = lineno;
243
244         if (!strcasecmp(channel, "console")) {
245                 chan->type = LOGTYPE_CONSOLE;
246         } else if (!strncasecmp(channel, "syslog", 6)) {
247                 /*
248                 * syntax is:
249                 *  syslog.facility => level,level,level
250                 */
251                 facility = strchr(channel, '.');
252                 if (!facility++ || !facility) {
253                         facility = "local0";
254                 }
255
256                 chan->facility = ast_syslog_facility(facility);
257
258                 if (chan->facility < 0) {
259                         fprintf(stderr, "Logger Warning: bad syslog facility in logger.conf\n");
260                         ast_free(chan);
261                         return NULL;
262                 }
263
264                 chan->type = LOGTYPE_SYSLOG;
265                 ast_copy_string(chan->filename, channel, sizeof(chan->filename));
266                 openlog("asterisk", LOG_PID, chan->facility);
267         } else {
268                 if (!ast_strlen_zero(hostname)) {
269                         snprintf(chan->filename, sizeof(chan->filename), "%s/%s.%s",
270                                  channel[0] != '/' ? ast_config_AST_LOG_DIR : "", channel, hostname);
271                 } else {
272                         snprintf(chan->filename, sizeof(chan->filename), "%s/%s",
273                                  channel[0] != '/' ? ast_config_AST_LOG_DIR : "", channel);
274                 }
275                 if (!(chan->fileptr = fopen(chan->filename, "a"))) {
276                         /* Can't log here, since we're called with a lock */
277                         fprintf(stderr, "Logger Warning: Unable to open log file '%s': %s\n", chan->filename, strerror(errno));
278                 } 
279                 chan->type = LOGTYPE_FILE;
280         }
281         chan->logmask = make_components(chan->components, lineno);
282
283         return chan;
284 }
285
286 static void init_logger_chain(int locked, const char *altconf)
287 {
288         struct logchannel *chan;
289         struct ast_config *cfg;
290         struct ast_variable *var;
291         const char *s;
292         struct ast_flags config_flags = { 0 };
293
294         if (!(cfg = ast_config_load2(S_OR(altconf, "logger.conf"), "logger", config_flags)) || cfg == CONFIG_STATUS_FILEINVALID) {
295                 return;
296         }
297
298         /* delete our list of log channels */
299         if (!locked) {
300                 AST_RWLIST_WRLOCK(&logchannels);
301         }
302         while ((chan = AST_RWLIST_REMOVE_HEAD(&logchannels, list))) {
303                 ast_free(chan);
304         }
305         global_logmask = 0;
306         if (!locked) {
307                 AST_RWLIST_UNLOCK(&logchannels);
308         }
309
310         errno = 0;
311         /* close syslog */
312         closelog();
313
314         /* If no config file, we're fine, set default options. */
315         if (!cfg) {
316                 if (errno) {
317                         fprintf(stderr, "Unable to open logger.conf: %s; default settings will be used.\n", strerror(errno));
318                 } else {
319                         fprintf(stderr, "Errors detected in logger.conf: see above; default settings will be used.\n");
320                 }
321                 if (!(chan = ast_calloc(1, sizeof(*chan)))) {
322                         return;
323                 }
324                 chan->type = LOGTYPE_CONSOLE;
325                 chan->logmask = __LOG_WARNING | __LOG_NOTICE | __LOG_ERROR;
326                 if (!locked) {
327                         AST_RWLIST_WRLOCK(&logchannels);
328                 }
329                 AST_RWLIST_INSERT_HEAD(&logchannels, chan, list);
330                 global_logmask |= chan->logmask;
331                 if (!locked) {
332                         AST_RWLIST_UNLOCK(&logchannels);
333                 }
334                 return;
335         }
336
337         if ((s = ast_variable_retrieve(cfg, "general", "appendhostname"))) {
338                 if (ast_true(s)) {
339                         if (gethostname(hostname, sizeof(hostname) - 1)) {
340                                 ast_copy_string(hostname, "unknown", sizeof(hostname));
341                                 fprintf(stderr, "What box has no hostname???\n");
342                         }
343                 } else
344                         hostname[0] = '\0';
345         } else
346                 hostname[0] = '\0';
347         if ((s = ast_variable_retrieve(cfg, "general", "dateformat")))
348                 ast_copy_string(dateformat, s, sizeof(dateformat));
349         else
350                 ast_copy_string(dateformat, "%b %e %T", sizeof(dateformat));
351         if ((s = ast_variable_retrieve(cfg, "general", "queue_log"))) {
352                 logfiles.queue_log = ast_true(s);
353         }
354         if ((s = ast_variable_retrieve(cfg, "general", "queue_log_to_file"))) {
355                 logfiles.queue_log_to_file = ast_true(s);
356         }
357         if ((s = ast_variable_retrieve(cfg, "general", "queue_log_name"))) {
358                 ast_copy_string(queue_log_name, s, sizeof(queue_log_name));
359         }
360         if ((s = ast_variable_retrieve(cfg, "general", "exec_after_rotate"))) {
361                 ast_copy_string(exec_after_rotate, s, sizeof(exec_after_rotate));
362         }
363         if ((s = ast_variable_retrieve(cfg, "general", "rotatestrategy"))) {
364                 if (strcasecmp(s, "timestamp") == 0) {
365                         rotatestrategy = TIMESTAMP;
366                 } else if (strcasecmp(s, "rotate") == 0) {
367                         rotatestrategy = ROTATE;
368                 } else if (strcasecmp(s, "sequential") == 0) {
369                         rotatestrategy = SEQUENTIAL;
370                 } else {
371                         fprintf(stderr, "Unknown rotatestrategy: %s\n", s);
372                 }
373         } else {
374                 if ((s = ast_variable_retrieve(cfg, "general", "rotatetimestamp"))) {
375                         rotatestrategy = ast_true(s) ? TIMESTAMP : SEQUENTIAL;
376                         fprintf(stderr, "rotatetimestamp option has been deprecated.  Please use rotatestrategy instead.\n");
377                 }
378         }
379
380         if (!locked) {
381                 AST_RWLIST_WRLOCK(&logchannels);
382         }
383         var = ast_variable_browse(cfg, "logfiles");
384         for (; var; var = var->next) {
385                 if (!(chan = make_logchannel(var->name, var->value, var->lineno))) {
386                         continue;
387                 }
388                 AST_RWLIST_INSERT_HEAD(&logchannels, chan, list);
389                 global_logmask |= chan->logmask;
390         }
391         if (qlog) {
392                 char tmp[4096];
393                 fclose(qlog);
394                 snprintf(tmp, sizeof(tmp), "%s/%s", ast_config_AST_LOG_DIR, queue_log_name);
395                 qlog = fopen(tmp, "a");
396         }
397
398         if (!locked) {
399                 AST_RWLIST_UNLOCK(&logchannels);
400         }
401
402         ast_config_destroy(cfg);
403 }
404
405 void ast_child_verbose(int level, const char *fmt, ...)
406 {
407         char *msg = NULL, *emsg = NULL, *sptr, *eptr;
408         va_list ap, aq;
409         int size;
410
411         /* Don't bother, if the level isn't that high */
412         if (option_verbose < level) {
413                 return;
414         }
415
416         va_start(ap, fmt);
417         va_copy(aq, ap);
418         if ((size = vsnprintf(msg, 0, fmt, ap)) < 0) {
419                 va_end(ap);
420                 va_end(aq);
421                 return;
422         }
423         va_end(ap);
424
425         if (!(msg = ast_malloc(size + 1))) {
426                 va_end(aq);
427                 return;
428         }
429
430         vsnprintf(msg, size + 1, fmt, aq);
431         va_end(aq);
432
433         if (!(emsg = ast_malloc(size * 2 + 1))) {
434                 ast_free(msg);
435                 return;
436         }
437
438         for (sptr = msg, eptr = emsg; ; sptr++) {
439                 if (*sptr == '"') {
440                         *eptr++ = '\\';
441                 }
442                 *eptr++ = *sptr;
443                 if (*sptr == '\0') {
444                         break;
445                 }
446         }
447         ast_free(msg);
448
449         fprintf(stdout, "verbose \"%s\" %d\n", emsg, level);
450         fflush(stdout);
451         ast_free(emsg);
452 }
453
454 void ast_queue_log(const char *queuename, const char *callid, const char *agent, const char *event, const char *fmt, ...)
455 {
456         va_list ap;
457         struct timeval tv;
458         struct ast_tm tm;
459         char qlog_msg[8192];
460         int qlog_len;
461         char time_str[30];
462
463         if (!queuelog_init) {
464                 queuelog_init = 1;
465                 logger_queue_init();
466         }
467
468         if (ast_check_realtime("queue_log")) {
469                 tv = ast_tvnow();
470                 ast_localtime(&tv, &tm, NULL);
471                 ast_strftime(time_str, sizeof(time_str), "%F %T.%6q", &tm);
472                 va_start(ap, fmt);
473                 vsnprintf(qlog_msg, sizeof(qlog_msg), fmt, ap);
474                 va_end(ap);
475                 if (logfiles.queue_adaptive_realtime) {
476                         AST_DECLARE_APP_ARGS(args,
477                                 AST_APP_ARG(data)[5];
478                         );
479                         AST_NONSTANDARD_APP_ARGS(args, qlog_msg, '|');
480                         /* Ensure fields are large enough to receive data */
481                         ast_realtime_require_field("queue_log", "data1", RQ_CHAR, strlen(S_OR(args.data[0], "")),
482                                 "data2", RQ_CHAR, strlen(S_OR(args.data[1], "")),
483                                 "data3", RQ_CHAR, strlen(S_OR(args.data[2], "")),
484                                 "data4", RQ_CHAR, strlen(S_OR(args.data[3], "")),
485                                 "data5", RQ_CHAR, strlen(S_OR(args.data[4], "")),
486                                 SENTINEL);
487
488                         /* Store the log */
489                         ast_store_realtime("queue_log", "time", time_str,
490                                 "callid", callid,
491                                 "queuename", queuename,
492                                 "agent", agent,
493                                 "event", event,
494                                 "data1", S_OR(args.data[0], ""),
495                                 "data2", S_OR(args.data[1], ""),
496                                 "data3", S_OR(args.data[2], ""),
497                                 "data4", S_OR(args.data[3], ""),
498                                 "data5", S_OR(args.data[4], ""),
499                                 SENTINEL);
500                 } else {
501                         ast_store_realtime("queue_log", "time", time_str,
502                                 "callid", callid,
503                                 "queuename", queuename,
504                                 "agent", agent,
505                                 "event", event,
506                                 "data", qlog_msg,
507                                 SENTINEL);
508                 }
509
510                 if (!logfiles.queue_log_to_file) {
511                         return;
512                 }
513         }
514
515         if (qlog) {
516                 va_start(ap, fmt);
517                 qlog_len = snprintf(qlog_msg, sizeof(qlog_msg), "%ld|%s|%s|%s|%s|", (long)time(NULL), callid, queuename, agent, event);
518                 vsnprintf(qlog_msg + qlog_len, sizeof(qlog_msg) - qlog_len, fmt, ap);
519                 va_end(ap);
520                 AST_RWLIST_RDLOCK(&logchannels);
521                 if (qlog) {
522                         fprintf(qlog, "%s\n", qlog_msg);
523                         fflush(qlog);
524                 }
525                 AST_RWLIST_UNLOCK(&logchannels);
526         }
527 }
528
529 static int rotate_file(const char *filename)
530 {
531         char old[PATH_MAX];
532         char new[PATH_MAX];
533         int x, y, which, found, res = 0, fd;
534         char *suffixes[4] = { "", ".gz", ".bz2", ".Z" };
535
536         switch (rotatestrategy) {
537         case SEQUENTIAL:
538                 for (x = 0; ; x++) {
539                         snprintf(new, sizeof(new), "%s.%d", filename, x);
540                         fd = open(new, O_RDONLY);
541                         if (fd > -1)
542                                 close(fd);
543                         else
544                                 break;
545                 }
546                 if (rename(filename, new)) {
547                         fprintf(stderr, "Unable to rename file '%s' to '%s'\n", filename, new);
548                         res = -1;
549                 } else {
550                         filename = new;
551                 }
552                 break;
553         case TIMESTAMP:
554                 snprintf(new, sizeof(new), "%s.%ld", filename, (long)time(NULL));
555                 if (rename(filename, new)) {
556                         fprintf(stderr, "Unable to rename file '%s' to '%s'\n", filename, new);
557                         res = -1;
558                 } else {
559                         filename = new;
560                 }
561                 break;
562         case ROTATE:
563                 /* Find the next empty slot, including a possible suffix */
564                 for (x = 0; ; x++) {
565                         found = 0;
566                         for (which = 0; which < ARRAY_LEN(suffixes); which++) {
567                                 snprintf(new, sizeof(new), "%s.%d%s", filename, x, suffixes[which]);
568                                 fd = open(new, O_RDONLY);
569                                 if (fd > -1) {
570                                         close(fd);
571                                         found = 1;
572                                         break;
573                                 }
574                         }
575                         if (!found) {
576                                 break;
577                         }
578                 }
579
580                 /* Found an empty slot */
581                 for (y = x; y > 0; y--) {
582                         for (which = 0; which < ARRAY_LEN(suffixes); which++) {
583                                 snprintf(old, sizeof(old), "%s.%d%s", filename, y - 1, suffixes[which]);
584                                 fd = open(old, O_RDONLY);
585                                 if (fd > -1) {
586                                         /* Found the right suffix */
587                                         close(fd);
588                                         snprintf(new, sizeof(new), "%s.%d%s", filename, y, suffixes[which]);
589                                         if (rename(old, new)) {
590                                                 fprintf(stderr, "Unable to rename file '%s' to '%s'\n", old, new);
591                                                 res = -1;
592                                         }
593                                         break;
594                                 }
595                         }
596                 }
597
598                 /* Finally, rename the current file */
599                 snprintf(new, sizeof(new), "%s.0", filename);
600                 if (rename(filename, new)) {
601                         fprintf(stderr, "Unable to rename file '%s' to '%s'\n", filename, new);
602                         res = -1;
603                 }
604         }
605
606         if (!ast_strlen_zero(exec_after_rotate)) {
607                 struct ast_channel *c = ast_dummy_channel_alloc();
608                 char buf[512];
609                 pbx_builtin_setvar_helper(c, "filename", filename);
610                 pbx_substitute_variables_helper(c, exec_after_rotate, buf, sizeof(buf));
611                 if (ast_safe_system(buf) == -1) {
612                         ast_log(LOG_WARNING, "error executing '%s'\n", buf);
613                 }
614                 c = ast_channel_release(c);
615         }
616         return res;
617 }
618
619 static int reload_logger(int rotate, const char *altconf)
620 {
621         char old[PATH_MAX] = "";
622         int queue_rotate = rotate;
623         struct logchannel *f;
624         int res = 0;
625
626         AST_RWLIST_WRLOCK(&logchannels);
627
628         if (qlog) {
629                 if (rotate < 0) {
630                         /* Check filesize - this one typically doesn't need an auto-rotate */
631                         if (ftello(qlog) > 0x40000000) { /* Arbitrarily, 1 GB */
632                                 fclose(qlog);
633                                 qlog = NULL;
634                         } else {
635                                 queue_rotate = 0;
636                         }
637                 } else {
638                         fclose(qlog);
639                         qlog = NULL;
640                 }
641         } else {
642                 queue_rotate = 0;
643         }
644
645         ast_mkdir(ast_config_AST_LOG_DIR, 0777);
646
647         AST_RWLIST_TRAVERSE(&logchannels, f, list) {
648                 if (f->disabled) {
649                         f->disabled = 0;        /* Re-enable logging at reload */
650                         manager_event(EVENT_FLAG_SYSTEM, "LogChannel", "Channel: %s\r\nEnabled: Yes\r\n", f->filename);
651                 }
652                 if (f->fileptr && (f->fileptr != stdout) && (f->fileptr != stderr)) {
653                         int rotate_this = 0;
654                         if (ftello(f->fileptr) > 0x40000000) { /* Arbitrarily, 1 GB */
655                                 /* Be more proactive about rotating massive log files */
656                                 rotate_this = 1;
657                         }
658                         fclose(f->fileptr);     /* Close file */
659                         f->fileptr = NULL;
660                         if (rotate || rotate_this) {
661                                 rotate_file(f->filename);
662                         }
663                 }
664         }
665
666         filesize_reload_needed = 0;
667
668         init_logger_chain(1 /* locked */, altconf);
669
670         if (logfiles.queue_log) {
671                 do {
672                         ast_unload_realtime("queue_log");
673                         if (ast_check_realtime("queue_log")) {
674                                 if (!ast_realtime_require_field("queue_log",
675                                                 "time", RQ_DATETIME, 26, "data1", RQ_CHAR, 20,
676                                                 "data2", RQ_CHAR, 20, "data3", RQ_CHAR, 20,
677                                                 "data4", RQ_CHAR, 20, "data5", RQ_CHAR, 20, SENTINEL)) {
678                                         logfiles.queue_adaptive_realtime = 1;
679                                 } else {
680                                         logfiles.queue_adaptive_realtime = 0;
681                                 }
682
683                                 if (!logfiles.queue_log_to_file) {
684                                         /* Skip the following section */
685                                         break;
686                                 }
687                         }
688                         if (qlog) {
689                                 fclose(qlog);
690                                 qlog = NULL;
691                         }
692                         snprintf(old, sizeof(old), "%s/%s", ast_config_AST_LOG_DIR, queue_log_name);
693                         if (queue_rotate) {
694                                 rotate_file(old);
695                         }
696
697                         qlog = fopen(old, "a");
698                         if (qlog) {
699                                 AST_RWLIST_UNLOCK(&logchannels);
700                                 ast_queue_log("NONE", "NONE", "NONE", "CONFIGRELOAD", "%s", "");
701                                 AST_RWLIST_WRLOCK(&logchannels);
702                                 ast_verb(1, "Asterisk Queue Logger restarted\n");
703                         } else {
704                                 ast_log(LOG_ERROR, "Unable to create queue log: %s\n", strerror(errno));
705                                 res = -1;
706                         }
707                 } while (0);
708         }
709
710         AST_RWLIST_UNLOCK(&logchannels);
711
712         return res;
713 }
714
715 /*! \brief Reload the logger module without rotating log files (also used from loader.c during
716         a full Asterisk reload) */
717 int logger_reload(void)
718 {
719         if (reload_logger(0, NULL)) {
720                 return RESULT_FAILURE;
721         }
722         return RESULT_SUCCESS;
723 }
724
725 static char *handle_logger_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
726 {
727         switch (cmd) {
728         case CLI_INIT:
729                 e->command = "logger reload";
730                 e->usage =
731                         "Usage: logger reload [<alt-conf>]\n"
732                         "       Reloads the logger subsystem state.  Use after restarting syslogd(8) if you are using syslog logging.\n";
733                 return NULL;
734         case CLI_GENERATE:
735                 return NULL;
736         }
737         if (reload_logger(0, a->argc == 3 ? a->argv[2] : NULL)) {
738                 ast_cli(a->fd, "Failed to reload the logger\n");
739                 return CLI_FAILURE;
740         }
741         return CLI_SUCCESS;
742 }
743
744 static char *handle_logger_rotate(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
745 {
746         switch (cmd) {
747         case CLI_INIT:
748                 e->command = "logger rotate";
749                 e->usage = 
750                         "Usage: logger rotate\n"
751                         "       Rotates and Reopens the log files.\n";
752                 return NULL;
753         case CLI_GENERATE:
754                 return NULL;    
755         }
756         if (reload_logger(1, NULL)) {
757                 ast_cli(a->fd, "Failed to reload the logger and rotate log files\n");
758                 return CLI_FAILURE;
759         } 
760         return CLI_SUCCESS;
761 }
762
763 static char *handle_logger_set_level(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
764 {
765         int x;
766         int state;
767         int level = -1;
768
769         switch (cmd) {
770         case CLI_INIT:
771                 e->command = "logger set level {DEBUG|NOTICE|WARNING|ERROR|VERBOSE|DTMF} {on|off}";
772                 e->usage = 
773                         "Usage: logger set level {DEBUG|NOTICE|WARNING|ERROR|VERBOSE|DTMF} {on|off}\n"
774                         "       Set a specific log level to enabled/disabled for this console.\n";
775                 return NULL;
776         case CLI_GENERATE:
777                 return NULL;
778         }
779
780         if (a->argc < 5)
781                 return CLI_SHOWUSAGE;
782
783         AST_RWLIST_WRLOCK(&logchannels);
784
785         for (x = 0; x < ARRAY_LEN(levels); x++) {
786                 if (levels[x] && !strcasecmp(a->argv[3], levels[x])) {
787                         level = x;
788                         break;
789                 }
790         }
791
792         AST_RWLIST_UNLOCK(&logchannels);
793
794         state = ast_true(a->argv[4]) ? 1 : 0;
795
796         if (level != -1) {
797                 ast_console_toggle_loglevel(a->fd, level, state);
798                 ast_cli(a->fd, "Logger status for '%s' has been set to '%s'.\n", levels[level], state ? "on" : "off");
799         } else
800                 return CLI_SHOWUSAGE;
801
802         return CLI_SUCCESS;
803 }
804
805 /*! \brief CLI command to show logging system configuration */
806 static char *handle_logger_show_channels(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
807 {
808 #define FORMATL "%-35.35s %-8.8s %-9.9s "
809         struct logchannel *chan;
810         switch (cmd) {
811         case CLI_INIT:
812                 e->command = "logger show channels";
813                 e->usage = 
814                         "Usage: logger show channels\n"
815                         "       List configured logger channels.\n";
816                 return NULL;
817         case CLI_GENERATE:
818                 return NULL;    
819         }
820         ast_cli(a->fd, FORMATL, "Channel", "Type", "Status");
821         ast_cli(a->fd, "Configuration\n");
822         ast_cli(a->fd, FORMATL, "-------", "----", "------");
823         ast_cli(a->fd, "-------------\n");
824         AST_RWLIST_RDLOCK(&logchannels);
825         AST_RWLIST_TRAVERSE(&logchannels, chan, list) {
826                 unsigned int level;
827
828                 ast_cli(a->fd, FORMATL, chan->filename, chan->type == LOGTYPE_CONSOLE ? "Console" : (chan->type == LOGTYPE_SYSLOG ? "Syslog" : "File"),
829                         chan->disabled ? "Disabled" : "Enabled");
830                 ast_cli(a->fd, " - ");
831                 for (level = 0; level < ARRAY_LEN(levels); level++) {
832                         if ((chan->logmask & (1 << level)) && levels[level]) {
833                                 ast_cli(a->fd, "%s ", levels[level]);
834                         }
835                 }
836                 ast_cli(a->fd, "\n");
837         }
838         AST_RWLIST_UNLOCK(&logchannels);
839         ast_cli(a->fd, "\n");
840
841         return CLI_SUCCESS;
842 }
843
844 struct verb {
845         void (*verboser)(const char *string);
846         AST_LIST_ENTRY(verb) list;
847 };
848
849 static AST_RWLIST_HEAD_STATIC(verbosers, verb);
850
851 static struct ast_cli_entry cli_logger[] = {
852         AST_CLI_DEFINE(handle_logger_show_channels, "List configured log channels"),
853         AST_CLI_DEFINE(handle_logger_reload, "Reopens the log files"),
854         AST_CLI_DEFINE(handle_logger_rotate, "Rotates and reopens the log files"),
855         AST_CLI_DEFINE(handle_logger_set_level, "Enables/Disables a specific logging level for this console")
856 };
857
858 static void _handle_SIGXFSZ(int sig)
859 {
860         /* Indicate need to reload */
861         filesize_reload_needed = 1;
862 }
863
864 static struct sigaction handle_SIGXFSZ = {
865         .sa_handler = _handle_SIGXFSZ,
866         .sa_flags = SA_RESTART,
867 };
868
869 static void ast_log_vsyslog(struct logmsg *msg)
870 {
871         char buf[BUFSIZ];
872         int syslog_level = ast_syslog_priority_from_loglevel(msg->level);
873
874         if (syslog_level < 0) {
875                 /* we are locked here, so cannot ast_log() */
876                 fprintf(stderr, "ast_log_vsyslog called with bogus level: %d\n", msg->level);
877                 return;
878         }
879
880         snprintf(buf, sizeof(buf), "%s[%d]: %s:%d in %s: %s",
881                  levels[msg->level], msg->lwp, msg->file, msg->line, msg->function, msg->message);
882
883         term_strip(buf, buf, strlen(buf) + 1);
884         syslog(syslog_level, "%s", buf);
885 }
886
887 /*! \brief Print a normal log message to the channels */
888 static void logger_print_normal(struct logmsg *logmsg)
889 {
890         struct logchannel *chan = NULL;
891         char buf[BUFSIZ];
892         struct verb *v = NULL;
893
894         if (logmsg->level == __LOG_VERBOSE) {
895                 char *tmpmsg = ast_strdupa(logmsg->message + 1);
896                 /* Iterate through the list of verbosers and pass them the log message string */
897                 AST_RWLIST_RDLOCK(&verbosers);
898                 AST_RWLIST_TRAVERSE(&verbosers, v, list)
899                         v->verboser(logmsg->message);
900                 AST_RWLIST_UNLOCK(&verbosers);
901                 ast_string_field_set(logmsg, message, tmpmsg);
902         }
903
904         AST_RWLIST_RDLOCK(&logchannels);
905
906         if (!AST_RWLIST_EMPTY(&logchannels)) {
907                 AST_RWLIST_TRAVERSE(&logchannels, chan, list) {
908                         /* If the channel is disabled, then move on to the next one */
909                         if (chan->disabled)
910                                 continue;
911                         /* Check syslog channels */
912                         if (chan->type == LOGTYPE_SYSLOG && (chan->logmask & (1 << logmsg->level))) {
913                                 ast_log_vsyslog(logmsg);
914                         /* Console channels */
915                         } else if (chan->type == LOGTYPE_CONSOLE && (chan->logmask & (1 << logmsg->level))) {
916                                 char linestr[128];
917                                 char tmp1[80], tmp2[80], tmp3[80], tmp4[80];
918
919                                 /* If the level is verbose, then skip it */
920                                 if (logmsg->level == __LOG_VERBOSE)
921                                         continue;
922
923                                 /* Turn the numerical line number into a string */
924                                 snprintf(linestr, sizeof(linestr), "%d", logmsg->line);
925                                 /* Build string to print out */
926                                 snprintf(buf, sizeof(buf), "[%s] %s[%d]: %s:%s %s: %s",
927                                          logmsg->date,
928                                          term_color(tmp1, logmsg->level_name, colors[logmsg->level], 0, sizeof(tmp1)),
929                                          logmsg->lwp,
930                                          term_color(tmp2, logmsg->file, COLOR_BRWHITE, 0, sizeof(tmp2)),
931                                          term_color(tmp3, linestr, COLOR_BRWHITE, 0, sizeof(tmp3)),
932                                          term_color(tmp4, logmsg->function, COLOR_BRWHITE, 0, sizeof(tmp4)),
933                                          logmsg->message);
934                                 /* Print out */
935                                 ast_console_puts_mutable(buf, logmsg->level);
936                         /* File channels */
937                         } else if (chan->type == LOGTYPE_FILE && (chan->logmask & (1 << logmsg->level))) {
938                                 int res = 0;
939
940                                 /* If no file pointer exists, skip it */
941                                 if (!chan->fileptr) {
942                                         continue;
943                                 }
944
945                                 /* Print out to the file */
946                                 res = fprintf(chan->fileptr, "[%s] %s[%d] %s: %s",
947                                               logmsg->date, logmsg->level_name, logmsg->lwp, logmsg->file, term_strip(buf, logmsg->message, BUFSIZ));
948                                 if (res <= 0 && !ast_strlen_zero(logmsg->message)) {
949                                         fprintf(stderr, "**** Asterisk Logging Error: ***********\n");
950                                         if (errno == ENOMEM || errno == ENOSPC)
951                                                 fprintf(stderr, "Asterisk logging error: Out of disk space, can't log to log file %s\n", chan->filename);
952                                         else
953                                                 fprintf(stderr, "Logger Warning: Unable to write to log file '%s': %s (disabled)\n", chan->filename, strerror(errno));
954                                         manager_event(EVENT_FLAG_SYSTEM, "LogChannel", "Channel: %s\r\nEnabled: No\r\nReason: %d - %s\r\n", chan->filename, errno, strerror(errno));
955                                         chan->disabled = 1;
956                                 } else if (res > 0) {
957                                         fflush(chan->fileptr);
958                                 }
959                         }
960                 }
961         } else if (logmsg->level != __LOG_VERBOSE) {
962                 fputs(logmsg->message, stdout);
963         }
964
965         AST_RWLIST_UNLOCK(&logchannels);
966
967         /* If we need to reload because of the file size, then do so */
968         if (filesize_reload_needed) {
969                 reload_logger(-1, NULL);
970                 ast_verb(1, "Rotated Logs Per SIGXFSZ (Exceeded file size limit)\n");
971         }
972
973         return;
974 }
975
976 /*! \brief Actual logging thread */
977 static void *logger_thread(void *data)
978 {
979         struct logmsg *next = NULL, *msg = NULL;
980
981         for (;;) {
982                 /* We lock the message list, and see if any message exists... if not we wait on the condition to be signalled */
983                 AST_LIST_LOCK(&logmsgs);
984                 if (AST_LIST_EMPTY(&logmsgs)) {
985                         if (close_logger_thread) {
986                                 break;
987                         } else {
988                                 ast_cond_wait(&logcond, &logmsgs.lock);
989                         }
990                 }
991                 next = AST_LIST_FIRST(&logmsgs);
992                 AST_LIST_HEAD_INIT_NOLOCK(&logmsgs);
993                 AST_LIST_UNLOCK(&logmsgs);
994
995                 /* Otherwise go through and process each message in the order added */
996                 while ((msg = next)) {
997                         /* Get the next entry now so that we can free our current structure later */
998                         next = AST_LIST_NEXT(msg, list);
999
1000                         /* Depending on the type, send it to the proper function */
1001                         logger_print_normal(msg);
1002
1003                         /* Free the data since we are done */
1004                         ast_free(msg);
1005                 }
1006
1007                 /* If we should stop, then stop */
1008                 if (close_logger_thread)
1009                         break;
1010         }
1011
1012         return NULL;
1013 }
1014
1015 static void logger_queue_init(void)
1016 {
1017         /* Preloaded modules are up. */
1018         ast_unload_realtime("queue_log");
1019         if (logfiles.queue_log && ast_check_realtime("queue_log")) {
1020                 if (!ast_realtime_require_field("queue_log",
1021                                 "time", RQ_DATETIME, 26, "data1", RQ_CHAR, 20,
1022                                 "data2", RQ_CHAR, 20, "data3", RQ_CHAR, 20,
1023                                 "data4", RQ_CHAR, 20, "data5", RQ_CHAR, 20, SENTINEL)) {
1024                         logfiles.queue_adaptive_realtime = 1;
1025                 } else {
1026                         logfiles.queue_adaptive_realtime = 0;
1027                 }
1028         }
1029
1030         ast_queue_log("NONE", "NONE", "NONE", "QUEUESTART", "%s", "");
1031 }
1032
1033 int init_logger(void)
1034 {
1035         /* auto rotate if sig SIGXFSZ comes a-knockin */
1036         sigaction(SIGXFSZ, &handle_SIGXFSZ, NULL);
1037
1038         /* start logger thread */
1039         ast_cond_init(&logcond, NULL);
1040         if (ast_pthread_create(&logthread, NULL, logger_thread, NULL) < 0) {
1041                 ast_cond_destroy(&logcond);
1042                 return -1;
1043         }
1044
1045         /* register the logger cli commands */
1046         ast_cli_register_multiple(cli_logger, ARRAY_LEN(cli_logger));
1047
1048         ast_mkdir(ast_config_AST_LOG_DIR, 0777);
1049
1050         /* create log channels */
1051         init_logger_chain(0 /* locked */, NULL);
1052
1053         return 0;
1054 }
1055
1056 void close_logger(void)
1057 {
1058         struct logchannel *f = NULL;
1059
1060         /* Stop logger thread */
1061         AST_LIST_LOCK(&logmsgs);
1062         close_logger_thread = 1;
1063         ast_cond_signal(&logcond);
1064         AST_LIST_UNLOCK(&logmsgs);
1065
1066         if (logthread != AST_PTHREADT_NULL)
1067                 pthread_join(logthread, NULL);
1068
1069         AST_RWLIST_WRLOCK(&logchannels);
1070
1071         if (qlog) {
1072                 fclose(qlog);
1073                 qlog = NULL;
1074         }
1075
1076         AST_RWLIST_TRAVERSE(&logchannels, f, list) {
1077                 if (f->fileptr && (f->fileptr != stdout) && (f->fileptr != stderr)) {
1078                         fclose(f->fileptr);
1079                         f->fileptr = NULL;
1080                 }
1081         }
1082
1083         closelog(); /* syslog */
1084
1085         AST_RWLIST_UNLOCK(&logchannels);
1086
1087         return;
1088 }
1089
1090 /*!
1091  * \brief send log messages to syslog and/or the console
1092  */
1093 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
1094 {
1095         struct logmsg *logmsg = NULL;
1096         struct ast_str *buf = NULL;
1097         struct ast_tm tm;
1098         struct timeval now = ast_tvnow();
1099         int res = 0;
1100         va_list ap;
1101         char datestring[256];
1102
1103         if (!(buf = ast_str_thread_get(&log_buf, LOG_BUF_INIT_SIZE)))
1104                 return;
1105
1106         if (AST_RWLIST_EMPTY(&logchannels)) {
1107                 /*
1108                  * we don't have the logger chain configured yet,
1109                  * so just log to stdout
1110                  */
1111                 if (level != __LOG_VERBOSE) {
1112                         int result;
1113                         va_start(ap, fmt);
1114                         result = ast_str_set_va(&buf, BUFSIZ, fmt, ap); /* XXX BUFSIZ ? */
1115                         va_end(ap);
1116                         if (result != AST_DYNSTR_BUILD_FAILED) {
1117                                 term_filter_escapes(ast_str_buffer(buf));
1118                                 fputs(ast_str_buffer(buf), stdout);
1119                         }
1120                 }
1121                 return;
1122         }
1123         
1124         /* don't display LOG_DEBUG messages unless option_verbose _or_ option_debug
1125            are non-zero; LOG_DEBUG messages can still be displayed if option_debug
1126            is zero, if option_verbose is non-zero (this allows for 'level zero'
1127            LOG_DEBUG messages to be displayed, if the logmask on any channel
1128            allows it)
1129         */
1130         if (!option_verbose && !option_debug && (level == __LOG_DEBUG))
1131                 return;
1132
1133         /* Ignore anything that never gets logged anywhere */
1134         if (level != __LOG_VERBOSE && !(global_logmask & (1 << level)))
1135                 return;
1136         
1137         /* Build string */
1138         va_start(ap, fmt);
1139         res = ast_str_set_va(&buf, BUFSIZ, fmt, ap);
1140         va_end(ap);
1141
1142         /* If the build failed, then abort and free this structure */
1143         if (res == AST_DYNSTR_BUILD_FAILED)
1144                 return;
1145
1146         /* Create a new logging message */
1147         if (!(logmsg = ast_calloc_with_stringfields(1, struct logmsg, res + 128)))
1148                 return;
1149
1150         /* Copy string over */
1151         ast_string_field_set(logmsg, message, ast_str_buffer(buf));
1152
1153         /* Set type */
1154         if (level == __LOG_VERBOSE) {
1155                 logmsg->type = LOGMSG_VERBOSE;
1156         } else {
1157                 logmsg->type = LOGMSG_NORMAL;
1158         }
1159
1160         /* Create our date/time */
1161         ast_localtime(&now, &tm, NULL);
1162         ast_strftime(datestring, sizeof(datestring), dateformat, &tm);
1163         ast_string_field_set(logmsg, date, datestring);
1164
1165         /* Copy over data */
1166         logmsg->level = level;
1167         logmsg->line = line;
1168         ast_string_field_set(logmsg, level_name, levels[level]);
1169         ast_string_field_set(logmsg, file, file);
1170         ast_string_field_set(logmsg, function, function);
1171         logmsg->lwp = ast_get_tid();
1172
1173         /* If the logger thread is active, append it to the tail end of the list - otherwise skip that step */
1174         if (logthread != AST_PTHREADT_NULL) {
1175                 AST_LIST_LOCK(&logmsgs);
1176                 AST_LIST_INSERT_TAIL(&logmsgs, logmsg, list);
1177                 ast_cond_signal(&logcond);
1178                 AST_LIST_UNLOCK(&logmsgs);
1179         } else {
1180                 logger_print_normal(logmsg);
1181                 ast_free(logmsg);
1182         }
1183
1184         return;
1185 }
1186
1187 #ifdef HAVE_BKTR
1188
1189 struct ast_bt *ast_bt_create(void) 
1190 {
1191         struct ast_bt *bt = ast_calloc(1, sizeof(*bt));
1192         if (!bt) {
1193                 ast_log(LOG_ERROR, "Unable to allocate memory for backtrace structure!\n");
1194                 return NULL;
1195         }
1196
1197         bt->alloced = 1;
1198
1199         ast_bt_get_addresses(bt);
1200
1201         return bt;
1202 }
1203
1204 int ast_bt_get_addresses(struct ast_bt *bt)
1205 {
1206         bt->num_frames = backtrace(bt->addresses, AST_MAX_BT_FRAMES);
1207
1208         return 0;
1209 }
1210
1211 void *ast_bt_destroy(struct ast_bt *bt)
1212 {
1213         if (bt->alloced) {
1214                 ast_free(bt);
1215         }
1216
1217         return NULL;
1218 }
1219
1220 char **ast_bt_get_symbols(void **addresses, size_t num_frames)
1221 {
1222         char **strings = NULL;
1223 #if defined(BETTER_BACKTRACES)
1224         int stackfr;
1225         bfd *bfdobj;           /* bfd.h */
1226         Dl_info dli;           /* dlfcn.h */
1227         long allocsize;
1228         asymbol **syms = NULL; /* bfd.h */
1229         bfd_vma offset;        /* bfd.h */
1230         const char *lastslash;
1231         asection *section;
1232         const char *file, *func;
1233         unsigned int line;
1234         char address_str[128];
1235         char msg[1024];
1236         size_t strings_size;
1237         size_t *eachlen;
1238 #endif
1239
1240 #if defined(BETTER_BACKTRACES)
1241         strings_size = num_frames * sizeof(*strings);
1242         eachlen = ast_calloc(num_frames, sizeof(*eachlen));
1243
1244         if (!(strings = ast_calloc(num_frames, sizeof(*strings)))) {
1245                 return NULL;
1246         }
1247
1248         for (stackfr = 0; stackfr < num_frames; stackfr++) {
1249                 int found = 0, symbolcount;
1250
1251                 msg[0] = '\0';
1252
1253                 if (!dladdr(addresses[stackfr], &dli)) {
1254                         continue;
1255                 }
1256
1257                 if (strcmp(dli.dli_fname, "asterisk") == 0) {
1258                         char asteriskpath[256];
1259                         if (!(dli.dli_fname = ast_utils_which("asterisk", asteriskpath, sizeof(asteriskpath)))) {
1260                                 /* This will fail to find symbols */
1261                                 ast_log(LOG_DEBUG, "Failed to find asterisk binary for debug symbols.\n");
1262                                 dli.dli_fname = "asterisk";
1263                         }
1264                 }
1265
1266                 lastslash = strrchr(dli.dli_fname, '/');
1267                 if (    (bfdobj = bfd_openr(dli.dli_fname, NULL)) &&
1268                                 bfd_check_format(bfdobj, bfd_object) &&
1269                                 (allocsize = bfd_get_symtab_upper_bound(bfdobj)) > 0 &&
1270                                 (syms = ast_malloc(allocsize)) &&
1271                                 (symbolcount = bfd_canonicalize_symtab(bfdobj, syms))) {
1272
1273                         if (bfdobj->flags & DYNAMIC) {
1274                                 offset = addresses[stackfr] - dli.dli_fbase;
1275                         } else {
1276                                 offset = addresses[stackfr] - (void *) 0;
1277                         }
1278
1279                         for (section = bfdobj->sections; section; section = section->next) {
1280                                 if (    !bfd_get_section_flags(bfdobj, section) & SEC_ALLOC ||
1281                                                 section->vma > offset ||
1282                                                 section->size + section->vma < offset) {
1283                                         continue;
1284                                 }
1285
1286                                 if (!bfd_find_nearest_line(bfdobj, section, syms, offset - section->vma, &file, &func, &line)) {
1287                                         continue;
1288                                 }
1289
1290                                 /* Stack trace output */
1291                                 found++;
1292                                 if ((lastslash = strrchr(file, '/'))) {
1293                                         const char *prevslash;
1294                                         for (prevslash = lastslash - 1; *prevslash != '/' && prevslash >= file; prevslash--);
1295                                         if (prevslash >= file) {
1296                                                 lastslash = prevslash;
1297                                         }
1298                                 }
1299                                 if (dli.dli_saddr == NULL) {
1300                                         address_str[0] = '\0';
1301                                 } else {
1302                                         snprintf(address_str, sizeof(address_str), " (%p+%lX)",
1303                                                 dli.dli_saddr,
1304                                                 (unsigned long) (addresses[stackfr] - dli.dli_saddr));
1305                                 }
1306                                 snprintf(msg, sizeof(msg), "%s:%u %s()%s",
1307                                         lastslash ? lastslash + 1 : file, line,
1308                                         S_OR(func, "???"),
1309                                         address_str);
1310
1311                                 break; /* out of section iteration */
1312                         }
1313                 }
1314                 if (bfdobj) {
1315                         bfd_close(bfdobj);
1316                         if (syms) {
1317                                 ast_free(syms);
1318                         }
1319                 }
1320
1321                 /* Default output, if we cannot find the information within BFD */
1322                 if (!found) {
1323                         if (dli.dli_saddr == NULL) {
1324                                 address_str[0] = '\0';
1325                         } else {
1326                                 snprintf(address_str, sizeof(address_str), " (%p+%lX)",
1327                                         dli.dli_saddr,
1328                                         (unsigned long) (addresses[stackfr] - dli.dli_saddr));
1329                         }
1330                         snprintf(msg, sizeof(msg), "%s %s()%s",
1331                                 lastslash ? lastslash + 1 : dli.dli_fname,
1332                                 S_OR(dli.dli_sname, "<unknown>"),
1333                                 address_str);
1334                 }
1335
1336                 if (!ast_strlen_zero(msg)) {
1337                         char **tmp;
1338                         eachlen[stackfr] = strlen(msg);
1339                         if (!(tmp = ast_realloc(strings, strings_size + eachlen[stackfr] + 1))) {
1340                                 ast_free(strings);
1341                                 strings = NULL;
1342                                 break; /* out of stack frame iteration */
1343                         }
1344                         strings = tmp;
1345                         strings[stackfr] = (char *) strings + strings_size;
1346                         ast_copy_string(strings[stackfr], msg, eachlen[stackfr] + 1);
1347                         strings_size += eachlen[stackfr] + 1;
1348                 }
1349         }
1350
1351         if (strings) {
1352                 /* Recalculate the offset pointers */
1353                 strings[0] = (char *) strings + num_frames * sizeof(*strings);
1354                 for (stackfr = 1; stackfr < num_frames; stackfr++) {
1355                         strings[stackfr] = strings[stackfr - 1] + eachlen[stackfr - 1] + 1;
1356                 }
1357         }
1358 #else /* !defined(BETTER_BACKTRACES) */
1359         strings = backtrace_symbols(addresses, num_frames);
1360 #endif /* defined(BETTER_BACKTRACES) */
1361         return strings;
1362 }
1363
1364 #endif /* HAVE_BKTR */
1365
1366 void ast_backtrace(void)
1367 {
1368 #ifdef HAVE_BKTR
1369         struct ast_bt *bt;
1370         int i = 0;
1371         char **strings;
1372
1373         if (!(bt = ast_bt_create())) {
1374                 ast_log(LOG_WARNING, "Unable to allocate space for backtrace structure\n");
1375                 return;
1376         }
1377
1378         if ((strings = ast_bt_get_symbols(bt->addresses, bt->num_frames))) {
1379                 ast_debug(1, "Got %d backtrace record%c\n", bt->num_frames, bt->num_frames != 1 ? 's' : ' ');
1380                 for (i = 3; i < bt->num_frames - 2; i++) {
1381                         ast_log(LOG_DEBUG, "#%d: [%p] %s\n", i - 3, bt->addresses[i], strings[i]);
1382                 }
1383
1384                 /* MALLOC_DEBUG will erroneously report an error here, unless we undef the macro. */
1385 #undef free
1386                 free(strings);
1387         } else {
1388                 ast_debug(1, "Could not allocate memory for backtrace\n");
1389         }
1390         ast_bt_destroy(bt);
1391 #else
1392         ast_log(LOG_WARNING, "Must run configure with '--with-execinfo' for stack backtraces.\n");
1393 #endif /* defined(HAVE_BKTR) */
1394 }
1395
1396 void __ast_verbose_ap(const char *file, int line, const char *func, const char *fmt, va_list ap)
1397 {
1398         struct ast_str *buf = NULL;
1399         int res = 0;
1400
1401         if (!(buf = ast_str_thread_get(&verbose_buf, VERBOSE_BUF_INIT_SIZE)))
1402                 return;
1403
1404         if (ast_opt_timestamp) {
1405                 struct timeval now;
1406                 struct ast_tm tm;
1407                 char date[40];
1408                 char *datefmt;
1409
1410                 now = ast_tvnow();
1411                 ast_localtime(&now, &tm, NULL);
1412                 ast_strftime(date, sizeof(date), dateformat, &tm);
1413                 datefmt = alloca(strlen(date) + 3 + strlen(fmt) + 1);
1414                 sprintf(datefmt, "%c[%s] %s", 127, date, fmt);
1415                 fmt = datefmt;
1416         } else {
1417                 char *tmp = alloca(strlen(fmt) + 2);
1418                 sprintf(tmp, "%c%s", 127, fmt);
1419                 fmt = tmp;
1420         }
1421
1422         /* Build string */
1423         res = ast_str_set_va(&buf, 0, fmt, ap);
1424
1425         /* If the build failed then we can drop this allocated message */
1426         if (res == AST_DYNSTR_BUILD_FAILED)
1427                 return;
1428
1429         ast_log(__LOG_VERBOSE, file, line, func, "%s", ast_str_buffer(buf));
1430 }
1431
1432 void __ast_verbose(const char *file, int line, const char *func, const char *fmt, ...)
1433 {
1434         va_list ap;
1435
1436         va_start(ap, fmt);
1437         __ast_verbose_ap(file, line, func, fmt, ap);
1438         va_end(ap);
1439 }
1440
1441 /* No new code should use this directly, but we have the ABI for backwards compat */
1442 #undef ast_verbose
1443 void __attribute__((format(printf, 1,2))) ast_verbose(const char *fmt, ...);
1444 void ast_verbose(const char *fmt, ...)
1445 {
1446         va_list ap;
1447
1448         va_start(ap, fmt);
1449         __ast_verbose_ap("", 0, "", fmt, ap);
1450         va_end(ap);
1451 }
1452
1453 int ast_register_verbose(void (*v)(const char *string)) 
1454 {
1455         struct verb *verb;
1456
1457         if (!(verb = ast_malloc(sizeof(*verb))))
1458                 return -1;
1459
1460         verb->verboser = v;
1461
1462         AST_RWLIST_WRLOCK(&verbosers);
1463         AST_RWLIST_INSERT_HEAD(&verbosers, verb, list);
1464         AST_RWLIST_UNLOCK(&verbosers);
1465         
1466         return 0;
1467 }
1468
1469 int ast_unregister_verbose(void (*v)(const char *string))
1470 {
1471         struct verb *cur;
1472
1473         AST_RWLIST_WRLOCK(&verbosers);
1474         AST_RWLIST_TRAVERSE_SAFE_BEGIN(&verbosers, cur, list) {
1475                 if (cur->verboser == v) {
1476                         AST_RWLIST_REMOVE_CURRENT(list);
1477                         ast_free(cur);
1478                         break;
1479                 }
1480         }
1481         AST_RWLIST_TRAVERSE_SAFE_END;
1482         AST_RWLIST_UNLOCK(&verbosers);
1483         
1484         return cur ? 0 : -1;
1485 }
1486
1487 static void update_logchannels(void)
1488 {
1489         struct logchannel *cur;
1490
1491         AST_RWLIST_WRLOCK(&logchannels);
1492
1493         global_logmask = 0;
1494
1495         AST_RWLIST_TRAVERSE(&logchannels, cur, list) {
1496                 cur->logmask = make_components(cur->components, cur->lineno);
1497                 global_logmask |= cur->logmask;
1498         }
1499
1500         AST_RWLIST_UNLOCK(&logchannels);
1501 }
1502
1503 int ast_logger_register_level(const char *name)
1504 {
1505         unsigned int level;
1506         unsigned int available = 0;
1507
1508         AST_RWLIST_WRLOCK(&logchannels);
1509
1510         for (level = 0; level < ARRAY_LEN(levels); level++) {
1511                 if ((level >= 16) && !available && !levels[level]) {
1512                         available = level;
1513                         continue;
1514                 }
1515
1516                 if (levels[level] && !strcasecmp(levels[level], name)) {
1517                         ast_log(LOG_WARNING,
1518                                 "Unable to register dynamic logger level '%s': a standard logger level uses that name.\n",
1519                                 name);
1520                         AST_RWLIST_UNLOCK(&logchannels);
1521
1522                         return -1;
1523                 }
1524         }
1525
1526         if (!available) {
1527                 ast_log(LOG_WARNING,
1528                         "Unable to register dynamic logger level '%s'; maximum number of levels registered.\n",
1529                         name);
1530                 AST_RWLIST_UNLOCK(&logchannels);
1531
1532                 return -1;
1533         }
1534
1535         levels[available] = ast_strdup(name);
1536
1537         AST_RWLIST_UNLOCK(&logchannels);
1538
1539         ast_debug(1, "Registered dynamic logger level '%s' with index %d.\n", name, available);
1540
1541         update_logchannels();
1542
1543         return available;
1544 }
1545
1546 void ast_logger_unregister_level(const char *name)
1547 {
1548         unsigned int found = 0;
1549         unsigned int x;
1550
1551         AST_RWLIST_WRLOCK(&logchannels);
1552
1553         for (x = 16; x < ARRAY_LEN(levels); x++) {
1554                 if (!levels[x]) {
1555                         continue;
1556                 }
1557
1558                 if (strcasecmp(levels[x], name)) {
1559                         continue;
1560                 }
1561
1562                 found = 1;
1563                 break;
1564         }
1565
1566         if (found) {
1567                 /* take this level out of the global_logmask, to ensure that no new log messages
1568                  * will be queued for it
1569                  */
1570
1571                 global_logmask &= ~(1 << x);
1572
1573                 free(levels[x]);
1574                 levels[x] = NULL;
1575                 AST_RWLIST_UNLOCK(&logchannels);
1576
1577                 ast_debug(1, "Unregistered dynamic logger level '%s' with index %d.\n", name, x);
1578
1579                 update_logchannels();
1580         } else {
1581                 AST_RWLIST_UNLOCK(&logchannels);
1582         }
1583 }
1584