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