add FIELDQTY function to split variable contents and get any desired field (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 ast_log_vsyslog(int level, const char *file, int line, const char *function, const char *fmt, va_list args) 
608 {
609         char buf[BUFSIZ];
610
611         if (level >= SYSLOG_NLEVELS) {
612                 /* we are locked here, so cannot ast_log() */
613                 fprintf(stderr, "ast_log_vsyslog called with bogus level: %d\n", level);
614                 return;
615         }
616         if (level == __LOG_VERBOSE) {
617                 snprintf(buf, sizeof(buf), "VERBOSE[%ld]: ", (long)GETTID());
618                 level = __LOG_DEBUG;
619         } else {
620                 snprintf(buf, sizeof(buf), "%s[%ld]: %s:%d in %s: ",
621                         levels[level], (long)GETTID(), file, line, function);
622         }
623         vsnprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), fmt, args);
624         syslog(syslog_level_map[level], "%s", buf);
625 }
626
627 /*
628  * send log messages to syslog and/or the console
629  */
630 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
631 {
632         struct logchannel *chan;
633         char buf[BUFSIZ];
634         time_t t;
635         struct tm tm;
636         char date[256];
637
638         va_list ap;
639         
640         if (!option_verbose && !option_debug && (level == __LOG_DEBUG)) {
641                 return;
642         }
643         /* Ignore anything that never gets logged anywhere */
644         if (!(global_logmask & (1 << level)))
645                 return;
646         
647         /* Ignore anything other than the currently debugged file if there is one */
648         if ((level == __LOG_DEBUG) && !ast_strlen_zero(debug_filename) && strcasecmp(debug_filename, file))
649                 return;
650
651         /* begin critical section */
652         ast_mutex_lock(&loglock);
653
654         time(&t);
655         localtime_r(&t, &tm);
656         strftime(date, sizeof(date), dateformat, &tm);
657
658         if (logfiles.event_log && level == __LOG_EVENT) {
659                 va_start(ap, fmt);
660
661                 fprintf(eventlog, "%s asterisk[%d]: ", date, getpid());
662                 vfprintf(eventlog, fmt, ap);
663                 fflush(eventlog);
664
665                 va_end(ap);
666                 ast_mutex_unlock(&loglock);
667                 return;
668         }
669
670         if (logchannels) {
671                 chan = logchannels;
672                 while(chan && !chan->disabled) {
673                         /* Check syslog channels */
674                         if (chan->type == LOG_SYSLOG && (chan->logmask & (1 << level))) {
675                                 va_start(ap, fmt);
676                                 ast_log_vsyslog(level, file, line, function, fmt, ap);
677                                 va_end(ap);
678                         /* Console channels */
679                         } else if ((chan->logmask & (1 << level)) && (chan->type == LOGTYPE_CONSOLE)) {
680                                 char linestr[128];
681                                 char tmp1[80], tmp2[80], tmp3[80], tmp4[80];
682
683                                 if (level != __LOG_VERBOSE) {
684                                         sprintf(linestr, "%d", line);
685                                         snprintf(buf, sizeof(buf), option_timestamp ? "[%s] %s[%ld]: %s:%s %s: " : "%s %s[%ld]: %s:%s %s: ",
686                                                 date,
687                                                 term_color(tmp1, levels[level], colors[level], 0, sizeof(tmp1)),
688                                                 (long)GETTID(),
689                                                 term_color(tmp2, file, COLOR_BRWHITE, 0, sizeof(tmp2)),
690                                                 term_color(tmp3, linestr, COLOR_BRWHITE, 0, sizeof(tmp3)),
691                                                 term_color(tmp4, function, COLOR_BRWHITE, 0, sizeof(tmp4)));
692                                         
693                                         ast_console_puts(buf);
694                                         va_start(ap, fmt);
695                                         vsnprintf(buf, sizeof(buf), fmt, ap);
696                                         va_end(ap);
697                                         ast_console_puts(buf);
698                                 }
699                         /* File channels */
700                         } else if ((chan->logmask & (1 << level)) && (chan->fileptr)) {
701                                 int res;
702                                 snprintf(buf, sizeof(buf), option_timestamp ? "[%s] %s[%ld]: " : "%s %s[%ld] %s: ", date,
703                                         levels[level], (long)GETTID(), file);
704                                 res = fprintf(chan->fileptr, buf);
705                                 if (res <= 0 && buf[0] != '\0') {       /* Error, no characters printed */
706                                         fprintf(stderr,"**** Asterisk Logging Error: ***********\n");
707                                         if (errno == ENOMEM || errno == ENOSPC) {
708                                                 fprintf(stderr, "Asterisk logging error: Out of disk space, can't log to log file %s\n", chan->filename);
709                                         } else
710                                                 fprintf(stderr, "Logger Warning: Unable to write to log file '%s': %s (disabled)\n", chan->filename, strerror(errno));
711                                         manager_event(EVENT_FLAG_SYSTEM, "LogChannel", "Channel: %s\r\nEnabled: No\r\nReason: %d - %s\r\n", chan->filename, errno, strerror(errno));
712                                         chan->disabled = 1;     
713                                 } else {
714                                         /* No error message, continue printing */
715                                         va_start(ap, fmt);
716                                         vsnprintf(buf, sizeof(buf), fmt, ap);
717                                         va_end(ap);
718                                         fputs(buf, chan->fileptr);
719                                         fflush(chan->fileptr);
720                                 }
721                         }
722                         chan = chan->next;
723                 }
724         } else {
725                 /* 
726                  * we don't have the logger chain configured yet,
727                  * so just log to stdout 
728                 */
729                 if (level != __LOG_VERBOSE) {
730                         va_start(ap, fmt);
731                         vsnprintf(buf, sizeof(buf), fmt, ap);
732                         va_end(ap);
733                         fputs(buf, stdout);
734                 }
735         }
736
737         ast_mutex_unlock(&loglock);
738         /* end critical section */
739         if (pending_logger_reload) {
740                 reload_logger(1);
741                 ast_log(LOG_EVENT,"Rotated Logs Per SIGXFSZ (Exceeded file size limit)\n");
742                 if (option_verbose)
743                         ast_verbose("Rotated Logs Per SIGXFSZ (Exceeded file size limit)\n");
744         }
745 }
746
747 extern void ast_verbose(const char *fmt, ...)
748 {
749         static char stuff[4096];
750         static int pos = 0, opos;
751         static int replacelast = 0, complete;
752         struct msglist *m;
753         struct verb *v;
754         time_t t;
755         struct tm tm;
756         char date[40];
757         char *datefmt;
758         
759         va_list ap;
760         va_start(ap, fmt);
761         ast_mutex_lock(&msglist_lock);
762         time(&t);
763         localtime_r(&t, &tm);
764         strftime(date, sizeof(date), dateformat, &tm);
765
766         if (option_timestamp) {
767                 datefmt = alloca(strlen(date) + 3 + strlen(fmt) + 1);
768                 if (datefmt) {
769                         sprintf(datefmt, "[%s] %s", date, fmt);
770                         fmt = datefmt;
771                 }
772         }
773         vsnprintf(stuff + pos, sizeof(stuff) - pos, fmt, ap);
774         opos = pos;
775         pos = strlen(stuff);
776
777
778         if (stuff[strlen(stuff)-1] == '\n') 
779                 complete = 1;
780         else
781                 complete=0;
782         if (complete) {
783                 if (msgcnt < MAX_MSG_QUEUE) {
784                         /* Allocate new structure */
785                         m = malloc(sizeof(struct msglist));
786                         msgcnt++;
787                 } else {
788                         /* Recycle the oldest entry */
789                         m = list;
790                         list = list->next;
791                         free(m->msg);
792                 }
793                 if (m) {
794                         m->msg = strdup(stuff);
795                         if (m->msg) {
796                                 if (last)
797                                         last->next = m;
798                                 else
799                                         list = m;
800                                 m->next = NULL;
801                                 last = m;
802                         } else {
803                                 msgcnt--;
804                                 ast_log(LOG_ERROR, "Out of memory\n");
805                                 free(m);
806                         }
807                 }
808         }
809         if (verboser) {
810                 v = verboser;
811                 while(v) {
812                         v->verboser(stuff, opos, replacelast, complete);
813                         v = v->next;
814                 }
815         } /* else
816                 fprintf(stdout, stuff + opos); */
817         ast_log(LOG_VERBOSE, "%s", stuff);
818         if (strlen(stuff)) {
819                 if (stuff[strlen(stuff)-1] != '\n') 
820                         replacelast = 1;
821                 else 
822                         replacelast = pos = 0;
823         }
824         va_end(ap);
825
826         ast_mutex_unlock(&msglist_lock);
827 }
828
829 int ast_verbose_dmesg(void (*v)(const char *string, int opos, int replacelast, int complete))
830 {
831         struct msglist *m;
832         ast_mutex_lock(&msglist_lock);
833         m = list;
834         while(m) {
835                 /* Send all the existing entries that we have queued (i.e. they're likely to have missed) */
836                 v(m->msg, 0, 0, 1);
837                 m = m->next;
838         }
839         ast_mutex_unlock(&msglist_lock);
840         return 0;
841 }
842
843 int ast_register_verbose(void (*v)(const char *string, int opos, int replacelast, int complete)) 
844 {
845         struct msglist *m;
846         struct verb *tmp;
847         /* XXX Should be more flexible here, taking > 1 verboser XXX */
848         if ((tmp = malloc(sizeof (struct verb)))) {
849                 tmp->verboser = v;
850                 ast_mutex_lock(&msglist_lock);
851                 tmp->next = verboser;
852                 verboser = tmp;
853                 m = list;
854                 while(m) {
855                         /* Send all the existing entries that we have queued (i.e. they're likely to have missed) */
856                         v(m->msg, 0, 0, 1);
857                         m = m->next;
858                 }
859                 ast_mutex_unlock(&msglist_lock);
860                 return 0;
861         }
862         return -1;
863 }
864
865 int ast_unregister_verbose(void (*v)(const char *string, int opos, int replacelast, int complete))
866 {
867         int res = -1;
868         struct verb *tmp, *tmpl=NULL;
869         ast_mutex_lock(&msglist_lock);
870         tmp = verboser;
871         while(tmp) {
872                 if (tmp->verboser == v) {
873                         if (tmpl)
874                                 tmpl->next = tmp->next;
875                         else
876                                 verboser = tmp->next;
877                         free(tmp);
878                         break;
879                 }
880                 tmpl = tmp;
881                 tmp = tmp->next;
882         }
883         if (tmp)
884                 res = 0;
885         ast_mutex_unlock(&msglist_lock);
886         return res;
887 }