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