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