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