strip color code sequences from verbose messages being sent to files/syslog (bug...
[asterisk/asterisk.git] / logger.c
1 /*
2  * Asterisk Logger
3  * 
4  * Mark Spencer <markster@marko.net>
5  *
6  * Copyright(C)1999, Linux Support Services, Inc.
7  * 
8  * Distributed under the terms of the GNU General Public License (GPL) Version 2
9  *
10  * Logging routines
11  *
12  */
13
14 #include <signal.h>
15 #include <stdarg.h>
16 #include <stdio.h>
17 #include <unistd.h>
18 #include <time.h>
19 #include <asterisk/lock.h>
20 #include <asterisk/options.h>
21 #include <asterisk/channel.h>
22 #include <asterisk/config.h>
23 #include <asterisk/term.h>
24 #include <asterisk/cli.h>
25 #include <asterisk/utils.h>
26 #include <asterisk/manager.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <sys/stat.h>
31 #include "asterisk.h"
32 #include "astconf.h"
33
34 #define SYSLOG_NAMES /* so we can map syslog facilities names to their numeric values,
35                         from <syslog.h> which is included by logger.h */
36 #include <syslog.h>
37 static int syslog_level_map[] = {
38         LOG_DEBUG,
39         LOG_INFO,    /* arbitrary equivalent of LOG_EVENT */
40         LOG_NOTICE,
41         LOG_WARNING,
42         LOG_ERR,
43         LOG_DEBUG
44 };
45
46 #define SYSLOG_NLEVELS 6
47
48 #include <asterisk/logger.h>
49
50 #define MAX_MSG_QUEUE 200
51
52 #if defined(__linux__) && defined(__NR_gettid)
53 #include <asm/unistd.h>
54 #define GETTID() syscall(__NR_gettid)
55 #else
56 #define GETTID() getpid()
57 #endif
58
59 static char dateformat[256] = "%b %e %T";               /* Original Asterisk Format */
60
61 AST_MUTEX_DEFINE_STATIC(msglist_lock);
62 AST_MUTEX_DEFINE_STATIC(loglock);
63 static int pending_logger_reload = 0;
64 static int global_logmask = -1;
65
66 static struct {
67         unsigned int queue_log:1;
68         unsigned int event_log:1;
69 } logfiles = { 1, 1 };
70
71 static struct msglist {
72         char *msg;
73         struct msglist *next;
74 } *list = NULL, *last = NULL;
75
76 static char hostname[256];
77
78 enum logtypes {
79         LOGTYPE_SYSLOG,
80         LOGTYPE_FILE,
81         LOGTYPE_CONSOLE,
82 };
83
84 struct logchannel {
85         int logmask;                    /* What to log to this channel */
86         int disabled;                   /* If this channel is disabled or not */
87         int facility;                   /* syslog facility */
88         enum logtypes type;             /* Type of log channel */
89         FILE *fileptr;                  /* logfile logging file pointer */
90         char filename[256];             /* Filename */
91         struct logchannel *next;        /* Next channel in chain */
92 };
93
94 static struct logchannel *logchannels = NULL;
95
96 static int msgcnt = 0;
97
98 static FILE *eventlog = NULL;
99
100 static char *levels[] = {
101         "DEBUG",
102         "EVENT",
103         "NOTICE",
104         "WARNING",
105         "ERROR",
106         "VERBOSE"
107 };
108
109 static int colors[] = {
110         COLOR_BRGREEN,
111         COLOR_BRBLUE,
112         COLOR_YELLOW,
113         COLOR_BRRED,
114         COLOR_RED,
115         COLOR_GREEN
116 };
117
118 static int make_components(char *s, int lineno)
119 {
120         char *w;
121         int res = 0;
122         char *stringp=NULL;
123         stringp=s;
124         w = strsep(&stringp, ",");
125         while(w) {
126                 while(*w && (*w < 33))
127                         w++;
128                 if (!strcasecmp(w, "error")) 
129                         res |= (1 << __LOG_ERROR);
130                 else if (!strcasecmp(w, "warning"))
131                         res |= (1 << __LOG_WARNING);
132                 else if (!strcasecmp(w, "notice"))
133                         res |= (1 << __LOG_NOTICE);
134                 else if (!strcasecmp(w, "event"))
135                         res |= (1 << __LOG_EVENT);
136                 else if (!strcasecmp(w, "debug"))
137                         res |= (1 << __LOG_DEBUG);
138                 else if (!strcasecmp(w, "verbose"))
139                         res |= (1 << __LOG_VERBOSE);
140                 else {
141                         fprintf(stderr, "Logfile Warning: Unknown keyword '%s' at line %d of logger.conf\n", w, lineno);
142                 }
143                 w = strsep(&stringp, ",");
144         }
145         return res;
146 }
147
148 static struct logchannel *make_logchannel(char *channel, char *components, int lineno)
149 {
150         struct logchannel *chan;
151         char *facility;
152 #ifndef SOLARIS
153         CODE *cptr;
154 #endif
155
156         if (ast_strlen_zero(channel))
157                 return NULL;
158         chan = malloc(sizeof(struct logchannel));
159
160         if (!chan)      /* Can't allocate memory */
161                 return NULL;
162
163         memset(chan, 0, sizeof(struct logchannel));
164         if (!strcasecmp(channel, "console")) {
165                 chan->type = LOGTYPE_CONSOLE;
166         } else if (!strncasecmp(channel, "syslog", 6)) {
167                 /*
168                 * syntax is:
169                 *  syslog.facility => level,level,level
170                 */
171                 facility = strchr(channel, '.');
172                 if(!facility++ || !facility) {
173                         facility = "local0";
174                 }
175
176 #ifndef SOLARIS
177                 /*
178                 * Walk through the list of facilitynames (defined in sys/syslog.h)
179                 * to see if we can find the one we have been given
180                 */
181                 chan->facility = -1;
182                 cptr = facilitynames;
183                 while (cptr->c_name) {
184                         if (!strcasecmp(facility, cptr->c_name)) {
185                                 chan->facility = cptr->c_val;
186                                 break;
187                         }
188                         cptr++;
189                 }
190 #else
191                 chan->facility = -1;
192                 if (!strcasecmp(facility, "kern")) 
193                         chan->facility = LOG_KERN;
194                 else if (!strcasecmp(facility, "USER")) 
195                         chan->facility = LOG_USER;
196                 else if (!strcasecmp(facility, "MAIL")) 
197                         chan->facility = LOG_MAIL;
198                 else if (!strcasecmp(facility, "DAEMON")) 
199                         chan->facility = LOG_DAEMON;
200                 else if (!strcasecmp(facility, "AUTH")) 
201                         chan->facility = LOG_AUTH;
202                 else if (!strcasecmp(facility, "SYSLOG")) 
203                         chan->facility = LOG_SYSLOG;
204                 else if (!strcasecmp(facility, "LPR")) 
205                         chan->facility = LOG_LPR;
206                 else if (!strcasecmp(facility, "NEWS")) 
207                         chan->facility = LOG_NEWS;
208                 else if (!strcasecmp(facility, "UUCP")) 
209                         chan->facility = LOG_UUCP;
210                 else if (!strcasecmp(facility, "CRON")) 
211                         chan->facility = LOG_CRON;
212                 else if (!strcasecmp(facility, "LOCAL0")) 
213                         chan->facility = LOG_LOCAL0;
214                 else if (!strcasecmp(facility, "LOCAL1")) 
215                         chan->facility = LOG_LOCAL1;
216                 else if (!strcasecmp(facility, "LOCAL2")) 
217                         chan->facility = LOG_LOCAL2;
218                 else if (!strcasecmp(facility, "LOCAL3")) 
219                         chan->facility = LOG_LOCAL3;
220                 else if (!strcasecmp(facility, "LOCAL4")) 
221                         chan->facility = LOG_LOCAL4;
222                 else if (!strcasecmp(facility, "LOCAL5")) 
223                         chan->facility = LOG_LOCAL5;
224                 else if (!strcasecmp(facility, "LOCAL6")) 
225                         chan->facility = LOG_LOCAL6;
226                 else if (!strcasecmp(facility, "LOCAL7")) 
227                         chan->facility = LOG_LOCAL7;
228 #endif /* Solaris */
229
230                 if (0 > chan->facility) {
231                         fprintf(stderr, "Logger Warning: bad syslog facility in logger.conf\n");
232                         free(chan);
233                         return NULL;
234                 }
235
236                 chan->type = LOGTYPE_SYSLOG;
237                 snprintf(chan->filename, sizeof(chan->filename), "%s", channel);
238                 openlog("asterisk", LOG_PID, chan->facility);
239         } else {
240                 if (channel[0] == '/') {
241                         if(!ast_strlen_zero(hostname)) { 
242                                 snprintf(chan->filename, sizeof(chan->filename) - 1,"%s.%s", channel, hostname);
243                         } else {
244                                 strncpy(chan->filename, channel, sizeof(chan->filename) - 1);
245                         }
246                 }                 
247                 
248                 if(!ast_strlen_zero(hostname)) {
249                         snprintf(chan->filename, sizeof(chan->filename), "%s/%s.%s",(char *)ast_config_AST_LOG_DIR, channel, hostname);
250                 } else {
251                         snprintf(chan->filename, sizeof(chan->filename), "%s/%s", (char *)ast_config_AST_LOG_DIR, channel);
252                 }
253                 chan->fileptr = fopen(chan->filename, "a");
254                 if (!chan->fileptr) {
255                         /* Can't log here, since we're called with a lock */
256                         fprintf(stderr, "Logger Warning: Unable to open log file '%s': %s\n", chan->filename, strerror(errno));
257                 } 
258                 chan->type = LOGTYPE_FILE;
259         }
260         chan->logmask = make_components(components, lineno);
261         return chan;
262 }
263
264 static void init_logger_chain(void)
265 {
266         struct logchannel *chan, *cur;
267         struct ast_config *cfg;
268         struct ast_variable *var;
269         char *s;
270
271         /* delete our list of log channels */
272         ast_mutex_lock(&loglock);
273         chan = logchannels;
274         while (chan) {
275                 cur = chan->next;
276                 free(chan);
277                 chan = cur;
278         }
279         logchannels = NULL;
280         ast_mutex_unlock(&loglock);
281         
282         global_logmask = 0;
283         /* close syslog */
284         closelog();
285         
286         cfg = ast_config_load("logger.conf");
287         
288         /* If no config file, we're fine */
289         if (!cfg)
290                 return;
291         
292         ast_mutex_lock(&loglock);
293         if ((s = ast_variable_retrieve(cfg, "general", "appendhostname"))) {
294                 if(ast_true(s)) {
295                         if(gethostname(hostname, sizeof(hostname))) {
296                                 strncpy(hostname, "unknown", sizeof(hostname)-1);
297                                 ast_log(LOG_WARNING, "What box has no hostname???\n");
298                         }
299                 } else
300                         hostname[0] = '\0';
301         } else
302                 hostname[0] = '\0';
303         if ((s = ast_variable_retrieve(cfg, "general", "dateformat"))) {
304                 strncpy(dateformat, s, sizeof(dateformat) - 1);
305         } else
306                 strncpy(dateformat, "%b %e %T", sizeof(dateformat) - 1);
307         if ((s = ast_variable_retrieve(cfg, "general", "queue_log"))) {
308                 logfiles.queue_log = ast_true(s);
309         }
310         if ((s = ast_variable_retrieve(cfg, "general", "event_log"))) {
311                 logfiles.event_log = ast_true(s);
312         }
313
314         var = ast_variable_browse(cfg, "logfiles");
315         while(var) {
316                 chan = make_logchannel(var->name, var->value, var->lineno);
317                 if (chan) {
318                         chan->next = logchannels;
319                         logchannels = chan;
320                         global_logmask |= chan->logmask;
321                 }
322                 var = var->next;
323         }
324
325         ast_config_destroy(cfg);
326         ast_mutex_unlock(&loglock);
327 }
328
329 static FILE *qlog = NULL;
330 AST_MUTEX_DEFINE_STATIC(qloglock);
331
332 void ast_queue_log(const char *queuename, const char *callid, const char *agent, const char *event, const char *fmt, ...)
333 {
334         va_list ap;
335         ast_mutex_lock(&qloglock);
336         if (qlog) {
337                 va_start(ap, fmt);
338                 fprintf(qlog, "%ld|%s|%s|%s|%s|", (long)time(NULL), callid, queuename, agent, event);
339                 vfprintf(qlog, fmt, ap);
340                 fprintf(qlog, "\n");
341                 va_end(ap);
342                 fflush(qlog);
343         }
344         ast_mutex_unlock(&qloglock);
345 }
346
347 static void queue_log_init(void)
348 {
349         char filename[256];
350         int reloaded = 0;
351
352         ast_mutex_lock(&qloglock);
353         if (qlog) {
354                 reloaded = 1;
355                 fclose(qlog);
356                 qlog = NULL;
357         }
358         snprintf(filename, sizeof(filename), "%s/%s", (char *)ast_config_AST_LOG_DIR, "queue_log");
359         if (logfiles.queue_log) {
360                 qlog = fopen(filename, "a");
361         }
362         ast_mutex_unlock(&qloglock);
363         if (reloaded) 
364                 ast_queue_log("NONE", "NONE", "NONE", "CONFIGRELOAD", "%s", "");
365         else
366                 ast_queue_log("NONE", "NONE", "NONE", "QUEUESTART", "%s", "");
367 }
368
369 int reload_logger(int rotate)
370 {
371         char old[AST_CONFIG_MAX_PATH] = "";
372         char new[AST_CONFIG_MAX_PATH];
373         struct logchannel *f;
374         FILE *myf;
375         int x;
376
377         ast_mutex_lock(&loglock);
378         if (eventlog) 
379                 fclose(eventlog);
380         else 
381                 rotate = 0;
382         eventlog = NULL;
383
384         mkdir((char *)ast_config_AST_LOG_DIR, 0755);
385         snprintf(old, sizeof(old), "%s/%s", (char *)ast_config_AST_LOG_DIR, EVENTLOG);
386
387         if (logfiles.event_log) {
388                 if (rotate) {
389                         for (x=0;;x++) {
390                                 snprintf(new, sizeof(new), "%s/%s.%d", (char *)ast_config_AST_LOG_DIR, EVENTLOG,x);
391                                 myf = fopen((char *)new, "r");
392                                 if (myf)        /* File exists */
393                                         fclose(myf);
394                                 else
395                                         break;
396                         }
397         
398                         /* do it */
399                         if (rename(old,new))
400                                 fprintf(stderr, "Unable to rename file '%s' to '%s'\n", old, new);
401                 }
402
403                 eventlog = fopen(old, "a");
404         }
405
406         f = logchannels;
407         while(f) {
408                 if (f->disabled) {
409                         f->disabled = 0;        /* Re-enable logging at reload */
410                         manager_event(EVENT_FLAG_SYSTEM, "LogChannel", "Channel: %s\r\nEnabled: Yes\r\n", f->filename);
411                 }
412                 if (f->fileptr && (f->fileptr != stdout) && (f->fileptr != stderr)) {
413                         fclose(f->fileptr);     /* Close file */
414                         f->fileptr = NULL;
415                         if(rotate) {
416                                 strncpy(old, f->filename, sizeof(old) - 1);
417         
418                                 for(x=0;;x++) {
419                                         snprintf(new, sizeof(new), "%s.%d", f->filename, x);
420                                         myf = fopen((char *)new, "r");
421                                         if (myf) {
422                                                 fclose(myf);
423                                         } else {
424                                                 break;
425                                         }
426                                 }
427             
428                                 /* do it */
429                                 if (rename(old,new))
430                                         fprintf(stderr, "Unable to rename file '%s' to '%s'\n", old, new);
431                         }
432                 }
433                 f = f->next;
434         }
435
436         ast_mutex_unlock(&loglock);
437
438         queue_log_init();
439         init_logger_chain();
440
441         if (logfiles.event_log) {
442                 if (eventlog) {
443                         ast_log(LOG_EVENT, "Restarted Asterisk Event Logger\n");
444                         if (option_verbose)
445                                 ast_verbose("Asterisk Event Logger restarted\n");
446                         return 0;
447                 } else 
448                         ast_log(LOG_ERROR, "Unable to create event log: %s\n", strerror(errno));
449         }
450         pending_logger_reload = 0;
451         return -1;
452 }
453
454 static int handle_logger_reload(int fd, int argc, char *argv[])
455 {
456         if(reload_logger(0)) {
457                 ast_cli(fd, "Failed to reload the logger\n");
458                 return RESULT_FAILURE;
459         } else
460                 return RESULT_SUCCESS;
461 }
462
463 static int handle_logger_rotate(int fd, int argc, char *argv[])
464 {
465         if(reload_logger(1)) {
466                 ast_cli(fd, "Failed to reload the logger and rotate log files\n");
467                 return RESULT_FAILURE;
468         } else
469                 return RESULT_SUCCESS;
470 }
471
472 /*--- handle_logger_show_channels: CLI command to show logging system 
473         configuration */
474 static int handle_logger_show_channels(int fd, int argc, char *argv[])
475 {
476 #define FORMATL "%-35.35s %-8.8s %-9.9s "
477         struct logchannel *chan;
478
479         ast_mutex_lock(&loglock);
480
481         chan = logchannels;
482         ast_cli(fd,FORMATL, "Channel", "Type", "Status");
483         ast_cli(fd, "Configuration\n");
484         ast_cli(fd,FORMATL, "-------", "----", "------");
485         ast_cli(fd, "-------------\n");
486         while (chan) {
487                 ast_cli(fd, FORMATL, chan->filename, chan->type==LOGTYPE_CONSOLE ? "Console" : (chan->type==LOGTYPE_SYSLOG ? "Syslog" : "File"),
488                         chan->disabled ? "Disabled" : "Enabled");
489                 ast_cli(fd, " - ");
490                 if (chan->logmask & (1 << __LOG_DEBUG)) 
491                         ast_cli(fd, "Debug ");
492                 if (chan->logmask & (1 << __LOG_VERBOSE)) 
493                         ast_cli(fd, "Verbose ");
494                 if (chan->logmask & (1 << __LOG_WARNING)) 
495                         ast_cli(fd, "Warning ");
496                 if (chan->logmask & (1 << __LOG_NOTICE)) 
497                         ast_cli(fd, "Notice ");
498                 if (chan->logmask & (1 << __LOG_ERROR)) 
499                         ast_cli(fd, "Error ");
500                 if (chan->logmask & (1 << __LOG_EVENT)) 
501                         ast_cli(fd, "Event ");
502                 ast_cli(fd, "\n");
503                 chan = chan->next;
504         }
505         ast_cli(fd, "\n");
506
507         ast_mutex_unlock(&loglock);
508                 
509         return RESULT_SUCCESS;
510 }
511
512 static struct verb {
513         void (*verboser)(const char *string, int opos, int replacelast, int complete);
514         struct verb *next;
515 } *verboser = NULL;
516
517
518 static char logger_reload_help[] =
519 "Usage: logger reload\n"
520 "       Reloads the logger subsystem state.  Use after restarting syslogd(8) if you are using syslog logging.\n";
521
522 static char logger_rotate_help[] =
523 "Usage: logger rotate\n"
524 "       Rotates and Reopens the log files.\n";
525
526 static char logger_show_channels_help[] =
527 "Usage: logger show channels\n"
528 "       Show configured logger channels.\n";
529
530 static struct ast_cli_entry logger_show_channels_cli = 
531         { { "logger", "show", "channels", NULL }, 
532         handle_logger_show_channels, "List configured log channels",
533         logger_show_channels_help };
534
535 static struct ast_cli_entry reload_logger_cli = 
536         { { "logger", "reload", NULL }, 
537         handle_logger_reload, "Reopens the log files",
538         logger_reload_help };
539
540 static struct ast_cli_entry rotate_logger_cli = 
541         { { "logger", "rotate", NULL }, 
542         handle_logger_rotate, "Rotates and reopens the log files",
543         logger_rotate_help };
544
545 static int handle_SIGXFSZ(int sig) 
546 {
547         /* Indicate need to reload */
548         pending_logger_reload = 1;
549         return 0;
550 }
551
552 int init_logger(void)
553 {
554         char tmp[256];
555
556         /* auto rotate if sig SIGXFSZ comes a-knockin */
557         (void) signal(SIGXFSZ,(void *) handle_SIGXFSZ);
558
559         /* register the relaod logger cli command */
560         ast_cli_register(&reload_logger_cli);
561         ast_cli_register(&rotate_logger_cli);
562         ast_cli_register(&logger_show_channels_cli);
563
564         /* initialize queue logger */
565         queue_log_init();
566
567         /* create log channels */
568         init_logger_chain();
569
570         /* create the eventlog */
571         if (logfiles.event_log) {
572                 mkdir((char *)ast_config_AST_LOG_DIR, 0755);
573                 snprintf(tmp, sizeof(tmp), "%s/%s", (char *)ast_config_AST_LOG_DIR, EVENTLOG);
574                 eventlog = fopen((char *)tmp, "a");
575                 if (eventlog) {
576                         ast_log(LOG_EVENT, "Started Asterisk Event Logger\n");
577                         if (option_verbose)
578                                 ast_verbose("Asterisk Event Logger Started %s\n",(char *)tmp);
579                         return 0;
580                 } else 
581                         ast_log(LOG_ERROR, "Unable to create event log: %s\n", strerror(errno));
582         }
583
584         return -1;
585 }
586
587 void close_logger(void)
588 {
589         struct msglist *m, *tmp;
590
591         ast_mutex_lock(&msglist_lock);
592         m = list;
593         while(m) {
594                 if (m->msg) {
595                         free(m->msg);
596                 }
597                 tmp = m->next;
598                 free(m);
599                 m = tmp;
600         }
601         list = last = NULL;
602         msgcnt = 0;
603         ast_mutex_unlock(&msglist_lock);
604         return;
605 }
606
607 static void strip_coloring(char *str)
608 {
609         char *src = str, *dest, *end;
610         
611         if (!src)
612                 return;
613
614         /* find the first potential escape sequence in the string */
615
616         while (*src && (*src != '\033'))
617                 src++;
618         if (!*src)
619                 return;
620
621         dest = src;
622         while (*src) {
623                 /* at the top of this loop, *src will always be an ESC character */
624                 if ((src[1] == '[') && ((end = strchr(src + 2, 'm'))))
625                         src = end + 1;
626                 else
627                         *dest++ = *src++;
628
629                 /* copy characters, checking for ESC as we go */
630                 while (*src && (*src != '\033'))
631                         *dest++ = *src++;
632         }
633
634         *dest = '\0';
635 }
636
637 static void ast_log_vsyslog(int level, const char *file, int line, const char *function, const char *fmt, va_list args) 
638 {
639         char buf[BUFSIZ];
640         char *s;
641
642         if (level >= SYSLOG_NLEVELS) {
643                 /* we are locked here, so cannot ast_log() */
644                 fprintf(stderr, "ast_log_vsyslog called with bogus level: %d\n", level);
645                 return;
646         }
647         if (level == __LOG_VERBOSE) {
648                 snprintf(buf, sizeof(buf), "VERBOSE[%ld]: ", (long)GETTID());
649                 level = __LOG_DEBUG;
650         } else {
651                 snprintf(buf, sizeof(buf), "%s[%ld]: %s:%d in %s: ",
652                          levels[level], (long)GETTID(), file, line, function);
653         }
654         s = buf + strlen(buf);
655         vsnprintf(s, sizeof(buf) - strlen(buf), fmt, args);
656         strip_coloring(s);
657         syslog(syslog_level_map[level], "%s", buf);
658 }
659
660 /*
661  * send log messages to syslog and/or the console
662  */
663 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
664 {
665         struct logchannel *chan;
666         char buf[BUFSIZ];
667         time_t t;
668         struct tm tm;
669         char date[256];
670
671         va_list ap;
672         
673         if (!option_verbose && !option_debug && (level == __LOG_DEBUG)) {
674                 return;
675         }
676         /* Ignore anything that never gets logged anywhere */
677         if (!(global_logmask & (1 << level)))
678                 return;
679         
680         /* Ignore anything other than the currently debugged file if there is one */
681         if ((level == __LOG_DEBUG) && !ast_strlen_zero(debug_filename) && strcasecmp(debug_filename, file))
682                 return;
683
684         /* begin critical section */
685         ast_mutex_lock(&loglock);
686
687         time(&t);
688         localtime_r(&t, &tm);
689         strftime(date, sizeof(date), dateformat, &tm);
690
691         if (logfiles.event_log && level == __LOG_EVENT) {
692                 va_start(ap, fmt);
693
694                 fprintf(eventlog, "%s asterisk[%d]: ", date, getpid());
695                 vfprintf(eventlog, fmt, ap);
696                 fflush(eventlog);
697
698                 va_end(ap);
699                 ast_mutex_unlock(&loglock);
700                 return;
701         }
702
703         if (logchannels) {
704                 chan = logchannels;
705                 while(chan && !chan->disabled) {
706                         /* Check syslog channels */
707                         if (chan->type == LOG_SYSLOG && (chan->logmask & (1 << level))) {
708                                 va_start(ap, fmt);
709                                 ast_log_vsyslog(level, file, line, function, fmt, ap);
710                                 va_end(ap);
711                         /* Console channels */
712                         } else if ((chan->logmask & (1 << level)) && (chan->type == LOGTYPE_CONSOLE)) {
713                                 char linestr[128];
714                                 char tmp1[80], tmp2[80], tmp3[80], tmp4[80];
715
716                                 if (level != __LOG_VERBOSE) {
717                                         sprintf(linestr, "%d", line);
718                                         snprintf(buf, sizeof(buf), option_timestamp ? "[%s] %s[%ld]: %s:%s %s: " : "%s %s[%ld]: %s:%s %s: ",
719                                                 date,
720                                                 term_color(tmp1, levels[level], colors[level], 0, sizeof(tmp1)),
721                                                 (long)GETTID(),
722                                                 term_color(tmp2, file, COLOR_BRWHITE, 0, sizeof(tmp2)),
723                                                 term_color(tmp3, linestr, COLOR_BRWHITE, 0, sizeof(tmp3)),
724                                                 term_color(tmp4, function, COLOR_BRWHITE, 0, sizeof(tmp4)));
725                                         
726                                         ast_console_puts(buf);
727                                         va_start(ap, fmt);
728                                         vsnprintf(buf, sizeof(buf), fmt, ap);
729                                         va_end(ap);
730                                         ast_console_puts(buf);
731                                 }
732                         /* File channels */
733                         } else if ((chan->logmask & (1 << level)) && (chan->fileptr)) {
734                                 int res;
735                                 snprintf(buf, sizeof(buf), option_timestamp ? "[%s] %s[%ld]: " : "%s %s[%ld] %s: ", date,
736                                         levels[level], (long)GETTID(), file);
737                                 res = fprintf(chan->fileptr, buf);
738                                 if (res <= 0 && buf[0] != '\0') {       /* Error, no characters printed */
739                                         fprintf(stderr,"**** Asterisk Logging Error: ***********\n");
740                                         if (errno == ENOMEM || errno == ENOSPC) {
741                                                 fprintf(stderr, "Asterisk logging error: Out of disk space, can't log to log file %s\n", chan->filename);
742                                         } else
743                                                 fprintf(stderr, "Logger Warning: Unable to write to log file '%s': %s (disabled)\n", chan->filename, strerror(errno));
744                                         manager_event(EVENT_FLAG_SYSTEM, "LogChannel", "Channel: %s\r\nEnabled: No\r\nReason: %d - %s\r\n", chan->filename, errno, strerror(errno));
745                                         chan->disabled = 1;     
746                                 } else {
747                                         /* No error message, continue printing */
748                                         va_start(ap, fmt);
749                                         vsnprintf(buf, sizeof(buf), fmt, ap);
750                                         va_end(ap);
751                                         strip_coloring(buf);
752                                         fputs(buf, chan->fileptr);
753                                         fflush(chan->fileptr);
754                                 }
755                         }
756                         chan = chan->next;
757                 }
758         } else {
759                 /* 
760                  * we don't have the logger chain configured yet,
761                  * so just log to stdout 
762                 */
763                 if (level != __LOG_VERBOSE) {
764                         va_start(ap, fmt);
765                         vsnprintf(buf, sizeof(buf), fmt, ap);
766                         va_end(ap);
767                         fputs(buf, stdout);
768                 }
769         }
770
771         ast_mutex_unlock(&loglock);
772         /* end critical section */
773         if (pending_logger_reload) {
774                 reload_logger(1);
775                 ast_log(LOG_EVENT,"Rotated Logs Per SIGXFSZ (Exceeded file size limit)\n");
776                 if (option_verbose)
777                         ast_verbose("Rotated Logs Per SIGXFSZ (Exceeded file size limit)\n");
778         }
779 }
780
781 extern void ast_verbose(const char *fmt, ...)
782 {
783         static char stuff[4096];
784         static int pos = 0, opos;
785         static int replacelast = 0, complete;
786         struct msglist *m;
787         struct verb *v;
788         time_t t;
789         struct tm tm;
790         char date[40];
791         char *datefmt;
792         
793         va_list ap;
794         va_start(ap, fmt);
795         ast_mutex_lock(&msglist_lock);
796         time(&t);
797         localtime_r(&t, &tm);
798         strftime(date, sizeof(date), dateformat, &tm);
799
800         if (option_timestamp) {
801                 datefmt = alloca(strlen(date) + 3 + strlen(fmt) + 1);
802                 if (datefmt) {
803                         sprintf(datefmt, "[%s] %s", date, fmt);
804                         fmt = datefmt;
805                 }
806         }
807         vsnprintf(stuff + pos, sizeof(stuff) - pos, fmt, ap);
808         opos = pos;
809         pos = strlen(stuff);
810
811
812         if (stuff[strlen(stuff)-1] == '\n') 
813                 complete = 1;
814         else
815                 complete=0;
816         if (complete) {
817                 if (msgcnt < MAX_MSG_QUEUE) {
818                         /* Allocate new structure */
819                         m = malloc(sizeof(struct msglist));
820                         msgcnt++;
821                 } else {
822                         /* Recycle the oldest entry */
823                         m = list;
824                         list = list->next;
825                         free(m->msg);
826                 }
827                 if (m) {
828                         m->msg = strdup(stuff);
829                         if (m->msg) {
830                                 if (last)
831                                         last->next = m;
832                                 else
833                                         list = m;
834                                 m->next = NULL;
835                                 last = m;
836                         } else {
837                                 msgcnt--;
838                                 ast_log(LOG_ERROR, "Out of memory\n");
839                                 free(m);
840                         }
841                 }
842         }
843         if (verboser) {
844                 v = verboser;
845                 while(v) {
846                         v->verboser(stuff, opos, replacelast, complete);
847                         v = v->next;
848                 }
849         } /* else
850                 fprintf(stdout, stuff + opos); */
851         ast_log(LOG_VERBOSE, "%s", stuff);
852         if (strlen(stuff)) {
853                 if (stuff[strlen(stuff)-1] != '\n') 
854                         replacelast = 1;
855                 else 
856                         replacelast = pos = 0;
857         }
858         va_end(ap);
859
860         ast_mutex_unlock(&msglist_lock);
861 }
862
863 int ast_verbose_dmesg(void (*v)(const char *string, int opos, int replacelast, int complete))
864 {
865         struct msglist *m;
866         ast_mutex_lock(&msglist_lock);
867         m = list;
868         while(m) {
869                 /* Send all the existing entries that we have queued (i.e. they're likely to have missed) */
870                 v(m->msg, 0, 0, 1);
871                 m = m->next;
872         }
873         ast_mutex_unlock(&msglist_lock);
874         return 0;
875 }
876
877 int ast_register_verbose(void (*v)(const char *string, int opos, int replacelast, int complete)) 
878 {
879         struct msglist *m;
880         struct verb *tmp;
881         /* XXX Should be more flexible here, taking > 1 verboser XXX */
882         if ((tmp = malloc(sizeof (struct verb)))) {
883                 tmp->verboser = v;
884                 ast_mutex_lock(&msglist_lock);
885                 tmp->next = verboser;
886                 verboser = tmp;
887                 m = list;
888                 while(m) {
889                         /* Send all the existing entries that we have queued (i.e. they're likely to have missed) */
890                         v(m->msg, 0, 0, 1);
891                         m = m->next;
892                 }
893                 ast_mutex_unlock(&msglist_lock);
894                 return 0;
895         }
896         return -1;
897 }
898
899 int ast_unregister_verbose(void (*v)(const char *string, int opos, int replacelast, int complete))
900 {
901         int res = -1;
902         struct verb *tmp, *tmpl=NULL;
903         ast_mutex_lock(&msglist_lock);
904         tmp = verboser;
905         while(tmp) {
906                 if (tmp->verboser == v) {
907                         if (tmpl)
908                                 tmpl->next = tmp->next;
909                         else
910                                 verboser = tmp->next;
911                         free(tmp);
912                         break;
913                 }
914                 tmpl = tmp;
915                 tmp = tmp->next;
916         }
917         if (tmp)
918                 res = 0;
919         ast_mutex_unlock(&msglist_lock);
920         return res;
921 }