Make logger respond better to lack of disk space and add "logger show channels" CLI...
[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) {
161                 memset(chan, 0, sizeof(struct logchannel));
162                 if (!strcasecmp(channel, "console")) {
163                         chan->type = LOGTYPE_CONSOLE;
164                 } else if (!strncasecmp(channel, "syslog", 6)) {
165                         /*
166                         * syntax is:
167                         *  syslog.facility => level,level,level
168                         */
169                         facility = strchr(channel, '.');
170                         if(!facility++ || !facility) {
171                                 facility = "local0";
172                         }
173
174 #ifndef SOLARIS
175                         /*
176                         * Walk through the list of facilitynames (defined in sys/syslog.h)
177                         * to see if we can find the one we have been given
178                         */
179                         chan->facility = -1;
180                         cptr = facilitynames;
181                         while (cptr->c_name) {
182                                 if (!strcasecmp(facility, cptr->c_name)) {
183                                             chan->facility = cptr->c_val;
184                                             break;
185                                 }
186                                 cptr++;
187                         }
188 #else
189                         chan->facility = -1;
190                         if (!strcasecmp(facility, "kern")) 
191                                 chan->facility = LOG_KERN;
192                         else if (!strcasecmp(facility, "USER")) 
193                                 chan->facility = LOG_USER;
194                         else if (!strcasecmp(facility, "MAIL")) 
195                                 chan->facility = LOG_MAIL;
196                         else if (!strcasecmp(facility, "DAEMON")) 
197                                 chan->facility = LOG_DAEMON;
198                         else if (!strcasecmp(facility, "AUTH")) 
199                                 chan->facility = LOG_AUTH;
200                         else if (!strcasecmp(facility, "SYSLOG")) 
201                                 chan->facility = LOG_SYSLOG;
202                         else if (!strcasecmp(facility, "LPR")) 
203                                 chan->facility = LOG_LPR;
204                         else if (!strcasecmp(facility, "NEWS")) 
205                                 chan->facility = LOG_NEWS;
206                         else if (!strcasecmp(facility, "UUCP")) 
207                                 chan->facility = LOG_UUCP;
208                         else if (!strcasecmp(facility, "CRON")) 
209                                 chan->facility = LOG_CRON;
210                         else if (!strcasecmp(facility, "LOCAL0")) 
211                                 chan->facility = LOG_LOCAL0;
212                         else if (!strcasecmp(facility, "LOCAL1")) 
213                                 chan->facility = LOG_LOCAL1;
214                         else if (!strcasecmp(facility, "LOCAL2")) 
215                                 chan->facility = LOG_LOCAL2;
216                         else if (!strcasecmp(facility, "LOCAL3")) 
217                                 chan->facility = LOG_LOCAL3;
218                         else if (!strcasecmp(facility, "LOCAL4")) 
219                                 chan->facility = LOG_LOCAL4;
220                         else if (!strcasecmp(facility, "LOCAL5")) 
221                                 chan->facility = LOG_LOCAL5;
222                         else if (!strcasecmp(facility, "LOCAL6")) 
223                                 chan->facility = LOG_LOCAL6;
224                         else if (!strcasecmp(facility, "LOCAL7")) 
225                                 chan->facility = LOG_LOCAL7;
226 #endif /* Solaris */
227
228                         if (0 > chan->facility) {
229                                 fprintf(stderr, "Logger Warning: bad syslog facility in logger.conf\n");
230                                 free(chan);
231                                 return NULL;
232                         }
233
234                         chan->type = LOGTYPE_SYSLOG;
235                         openlog("asterisk", LOG_PID, chan->facility);
236                 } else {
237                         if (channel[0] == '/') {
238                                 if(!ast_strlen_zero(hostname)) { 
239                                         snprintf(chan->filename, sizeof(chan->filename) - 1,"%s.%s", channel, hostname);
240                                 } else {
241                                         strncpy(chan->filename, channel, sizeof(chan->filename) - 1);
242                                 }
243                         }                 
244                         
245                         if(!ast_strlen_zero(hostname)) {
246                                 snprintf(chan->filename, sizeof(chan->filename), "%s/%s.%s",(char *)ast_config_AST_LOG_DIR, channel, hostname);
247                         } else {
248                                 snprintf(chan->filename, sizeof(chan->filename), "%s/%s", (char *)ast_config_AST_LOG_DIR, channel);
249                         }
250                         chan->fileptr = fopen(chan->filename, "a");
251                         if (!chan->fileptr) {
252                                 /* Can't log here, since we're called with a lock */
253                                 fprintf(stderr, "Logger Warning: Unable to open log file '%s': %s\n", chan->filename, strerror(errno));
254                         } 
255                         chan->type = LOGTYPE_FILE;
256                 }
257                 chan->logmask = make_components(components, lineno);
258         }
259         return chan;
260 }
261
262 static void init_logger_chain(void)
263 {
264         struct logchannel *chan, *cur;
265         struct ast_config *cfg;
266         struct ast_variable *var;
267         char *s;
268
269         /* delete our list of log channels */
270         ast_mutex_lock(&loglock);
271         chan = logchannels;
272         while (chan) {
273                 cur = chan->next;
274                 free(chan);
275                 chan = cur;
276         }
277         logchannels = NULL;
278         ast_mutex_unlock(&loglock);
279         
280         global_logmask = 0;
281         /* close syslog */
282         closelog();
283         
284         cfg = ast_config_load("logger.conf");
285         
286         /* If no config file, we're fine */
287         if (!cfg)
288                 return;
289         
290         ast_mutex_lock(&loglock);
291         if ((s = ast_variable_retrieve(cfg, "general", "appendhostname"))) {
292                 if(ast_true(s)) {
293                         if(gethostname(hostname, sizeof(hostname))) {
294                                 strncpy(hostname, "unknown", sizeof(hostname)-1);
295                                 ast_log(LOG_WARNING, "What box has no hostname???\n");
296                         }
297                 } else
298                         hostname[0] = '\0';
299         } else
300                 hostname[0] = '\0';
301         if ((s = ast_variable_retrieve(cfg, "general", "dateformat"))) {
302                 strncpy(dateformat, s, sizeof(dateformat) - 1);
303         } else
304                 strncpy(dateformat, "%b %e %T", sizeof(dateformat) - 1);
305         if ((s = ast_variable_retrieve(cfg, "general", "queue_log"))) {
306                 logfiles.queue_log = ast_true(s);
307         }
308         if ((s = ast_variable_retrieve(cfg, "general", "event_log"))) {
309                 logfiles.event_log = ast_true(s);
310         }
311
312         var = ast_variable_browse(cfg, "logfiles");
313         while(var) {
314                 chan = make_logchannel(var->name, var->value, var->lineno);
315                 if (chan) {
316                         chan->next = logchannels;
317                         logchannels = chan;
318                         global_logmask |= chan->logmask;
319                 }
320                 var = var->next;
321         }
322
323         ast_config_destroy(cfg);
324         ast_mutex_unlock(&loglock);
325 }
326
327 static FILE *qlog = NULL;
328 AST_MUTEX_DEFINE_STATIC(qloglock);
329
330 void ast_queue_log(const char *queuename, const char *callid, const char *agent, const char *event, const char *fmt, ...)
331 {
332         va_list ap;
333         ast_mutex_lock(&qloglock);
334         if (qlog) {
335                 va_start(ap, fmt);
336                 fprintf(qlog, "%ld|%s|%s|%s|%s|", (long)time(NULL), callid, queuename, agent, event);
337                 vfprintf(qlog, fmt, ap);
338                 fprintf(qlog, "\n");
339                 va_end(ap);
340                 fflush(qlog);
341         }
342         ast_mutex_unlock(&qloglock);
343 }
344
345 static void queue_log_init(void)
346 {
347         char filename[256];
348         int reloaded = 0;
349
350         ast_mutex_lock(&qloglock);
351         if (qlog) {
352                 reloaded = 1;
353                 fclose(qlog);
354                 qlog = NULL;
355         }
356         snprintf(filename, sizeof(filename), "%s/%s", (char *)ast_config_AST_LOG_DIR, "queue_log");
357         if (logfiles.queue_log) {
358                 qlog = fopen(filename, "a");
359         }
360         ast_mutex_unlock(&qloglock);
361         if (reloaded) 
362                 ast_queue_log("NONE", "NONE", "NONE", "CONFIGRELOAD", "%s", "");
363         else
364                 ast_queue_log("NONE", "NONE", "NONE", "QUEUESTART", "%s", "");
365 }
366
367 int reload_logger(int rotate)
368 {
369         char old[AST_CONFIG_MAX_PATH] = "";
370         char new[AST_CONFIG_MAX_PATH];
371         struct logchannel *f;
372         FILE *myf;
373         int x;
374
375         ast_mutex_lock(&loglock);
376         if (eventlog) 
377                 fclose(eventlog);
378         else 
379                 rotate = 0;
380         eventlog = NULL;
381
382         mkdir((char *)ast_config_AST_LOG_DIR, 0755);
383         snprintf(old, sizeof(old), "%s/%s", (char *)ast_config_AST_LOG_DIR, EVENTLOG);
384
385         if (logfiles.event_log) {
386                 if (rotate) {
387                         for (x=0;;x++) {
388                                 snprintf(new, sizeof(new), "%s/%s.%d", (char *)ast_config_AST_LOG_DIR, EVENTLOG,x);
389                                 myf = fopen((char *)new, "r");
390                                 if (myf)        /* File exists */
391                                         fclose(myf);
392                                 else
393                                         break;
394                         }
395         
396                         /* do it */
397                         if (rename(old,new))
398                                 fprintf(stderr, "Unable to rename file '%s' to '%s'\n", old, new);
399                 }
400
401                 eventlog = fopen(old, "a");
402         }
403
404         f = logchannels;
405         while(f) {
406                 if (f->disabled) {
407                         f->disabled = 0;        /* Re-enable logging at reload */
408                         manager_event(EVENT_FLAG_SYSTEM, "LogChannel", "Channel: %s\r\nEnabled: Yes\r\n", f->filename);
409                 }
410                 if (f->fileptr && (f->fileptr != stdout) && (f->fileptr != stderr)) {
411                         fclose(f->fileptr);     /* Close file */
412                         f->fileptr = NULL;
413                         if(rotate) {
414                                 strncpy(old, f->filename, sizeof(old) - 1);
415         
416                                 for(x=0;;x++) {
417                                         snprintf(new, sizeof(new), "%s.%d", f->filename, x);
418                                         myf = fopen((char *)new, "r");
419                                         if (myf) {
420                                                 fclose(myf);
421                                         } else {
422                                                 break;
423                                         }
424                                 }
425             
426                                 /* do it */
427                                 if (rename(old,new))
428                                         fprintf(stderr, "Unable to rename file '%s' to '%s'\n", old, new);
429                         }
430                 }
431                 f = f->next;
432         }
433
434         ast_mutex_unlock(&loglock);
435
436         queue_log_init();
437         init_logger_chain();
438
439         if (logfiles.event_log) {
440                 if (eventlog) {
441                         ast_log(LOG_EVENT, "Restarted Asterisk Event Logger\n");
442                         if (option_verbose)
443                                 ast_verbose("Asterisk Event Logger restarted\n");
444                         return 0;
445                 } else 
446                         ast_log(LOG_ERROR, "Unable to create event log: %s\n", strerror(errno));
447         }
448         pending_logger_reload = 0;
449         return -1;
450 }
451
452 static int handle_logger_reload(int fd, int argc, char *argv[])
453 {
454         if(reload_logger(0)) {
455                 ast_cli(fd, "Failed to reload the logger\n");
456                 return RESULT_FAILURE;
457         } else
458                 return RESULT_SUCCESS;
459 }
460
461 static int handle_logger_rotate(int fd, int argc, char *argv[])
462 {
463         if(reload_logger(1)) {
464                 ast_cli(fd, "Failed to reload the logger and rotate log files\n");
465                 return RESULT_FAILURE;
466         } else
467                 return RESULT_SUCCESS;
468 }
469
470 /*--- handle_logger_show_channels: CLI command to show logging system 
471         configuration */
472 static int handle_logger_show_channels(int fd, int argc, char *argv[])
473 {
474 #define FORMATL "%-35.35s %-8.8s %-9.9s "
475         struct logchannel *chan;
476
477         ast_mutex_lock(&loglock);
478
479         chan = logchannels;
480         ast_cli(fd,FORMATL, "Channel", "Type", "Status");
481         ast_cli(fd, "Configuration\n");
482         ast_cli(fd,FORMATL, "-------", "----", "------");
483         ast_cli(fd, "-------------\n");
484         while (chan) {
485                 ast_cli(fd, FORMATL, chan->filename, chan->type==LOGTYPE_CONSOLE ? "Console" : (chan->type==LOGTYPE_SYSLOG ? "Syslog" : "File"),
486                         chan->disabled ? "Disabled" : "Enabled");
487                 ast_cli(fd, " - ");
488                 if (chan->logmask & (1 << __LOG_DEBUG)) 
489                         ast_cli(fd, "Debug ");
490                 if (chan->logmask & (1 << __LOG_VERBOSE)) 
491                         ast_cli(fd, "Verbose ");
492                 if (chan->logmask & (1 << __LOG_WARNING)) 
493                         ast_cli(fd, "Warning ");
494                 if (chan->logmask & (1 << __LOG_NOTICE)) 
495                         ast_cli(fd, "Notice ");
496                 if (chan->logmask & (1 << __LOG_ERROR)) 
497                         ast_cli(fd, "Error ");
498                 if (chan->logmask & (1 << __LOG_EVENT)) 
499                         ast_cli(fd, "Event ");
500                 ast_cli(fd, "\n");
501                 chan = chan->next;
502         }
503         ast_cli(fd, "\n");
504
505         ast_mutex_unlock(&loglock);
506                 
507         return RESULT_SUCCESS;
508 }
509
510 static struct verb {
511         void (*verboser)(const char *string, int opos, int replacelast, int complete);
512         struct verb *next;
513 } *verboser = NULL;
514
515
516 static char logger_reload_help[] =
517 "Usage: logger reload\n"
518 "       Reloads the logger subsystem state.  Use after restarting syslogd(8) if you are using syslog logging.\n";
519
520 static char logger_rotate_help[] =
521 "Usage: logger rotate\n"
522 "       Rotates and Reopens the log files.\n";
523
524 static char logger_show_channels_help[] =
525 "Usage: logger show channels\n"
526 "       Show configured logger channels.\n";
527
528 static struct ast_cli_entry logger_show_channels_cli = 
529         { { "logger", "show", "channels", NULL }, 
530         handle_logger_show_channels, "List configured log channels",
531         logger_show_channels_help };
532
533 static struct ast_cli_entry reload_logger_cli = 
534         { { "logger", "reload", NULL }, 
535         handle_logger_reload, "Reopens the log files",
536         logger_reload_help };
537
538 static struct ast_cli_entry rotate_logger_cli = 
539         { { "logger", "rotate", NULL }, 
540         handle_logger_rotate, "Rotates and reopens the log files",
541         logger_rotate_help };
542
543 static int handle_SIGXFSZ(int sig) 
544 {
545         /* Indicate need to reload */
546         pending_logger_reload = 1;
547         return 0;
548 }
549
550 int init_logger(void)
551 {
552         char tmp[256];
553
554         /* auto rotate if sig SIGXFSZ comes a-knockin */
555         (void) signal(SIGXFSZ,(void *) handle_SIGXFSZ);
556
557         /* register the relaod logger cli command */
558         ast_cli_register(&reload_logger_cli);
559         ast_cli_register(&rotate_logger_cli);
560         ast_cli_register(&logger_show_channels_cli);
561
562         /* initialize queue logger */
563         queue_log_init();
564
565         /* create log channels */
566         init_logger_chain();
567
568         /* create the eventlog */
569         if (logfiles.event_log) {
570                 mkdir((char *)ast_config_AST_LOG_DIR, 0755);
571                 snprintf(tmp, sizeof(tmp), "%s/%s", (char *)ast_config_AST_LOG_DIR, EVENTLOG);
572                 eventlog = fopen((char *)tmp, "a");
573                 if (eventlog) {
574                         ast_log(LOG_EVENT, "Started Asterisk Event Logger\n");
575                         if (option_verbose)
576                                 ast_verbose("Asterisk Event Logger Started %s\n",(char *)tmp);
577                         return 0;
578                 } else 
579                         ast_log(LOG_ERROR, "Unable to create event log: %s\n", strerror(errno));
580         }
581
582         return -1;
583 }
584
585 void close_logger(void)
586 {
587         struct msglist *m, *tmp;
588
589         ast_mutex_lock(&msglist_lock);
590         m = list;
591         while(m) {
592                 if (m->msg) {
593                         free(m->msg);
594                 }
595                 tmp = m->next;
596                 free(m);
597                 m = tmp;
598         }
599         list = last = NULL;
600         msgcnt = 0;
601         ast_mutex_unlock(&msglist_lock);
602         return;
603 }
604
605 static void ast_log_vsyslog(int level, const char *file, int line, const char *function, const char *fmt, va_list args) 
606 {
607         char buf[BUFSIZ];
608
609         if (level >= SYSLOG_NLEVELS) {
610                 /* we are locked here, so cannot ast_log() */
611                 fprintf(stderr, "ast_log_vsyslog called with bogus level: %d\n", level);
612                 return;
613         }
614         if (level == __LOG_VERBOSE) {
615                 snprintf(buf, sizeof(buf), "VERBOSE[%ld]: ", (long)GETTID());
616                 level = __LOG_DEBUG;
617         } else {
618                 snprintf(buf, sizeof(buf), "%s[%ld]: %s:%d in %s: ",
619                         levels[level], (long)GETTID(), file, line, function);
620         }
621         vsnprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), fmt, args);
622         syslog(syslog_level_map[level], "%s", buf);
623 }
624
625 /*
626  * send log messages to syslog and/or the console
627  */
628 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
629 {
630         struct logchannel *chan;
631         char buf[BUFSIZ];
632         time_t t;
633         struct tm tm;
634         char date[256];
635
636         va_list ap;
637         
638         if (!option_verbose && !option_debug && (level == __LOG_DEBUG)) {
639                 return;
640         }
641         /* Ignore anything that never gets logged anywhere */
642         if (!(global_logmask & (1 << level)))
643                 return;
644         
645         /* Ignore anything other than the currently debugged file if there is one */
646         if ((level == __LOG_DEBUG) && !ast_strlen_zero(debug_filename) && strcasecmp(debug_filename, file))
647                 return;
648
649         /* begin critical section */
650         ast_mutex_lock(&loglock);
651
652         time(&t);
653         localtime_r(&t, &tm);
654         strftime(date, sizeof(date), dateformat, &tm);
655
656         if (logfiles.event_log && level == __LOG_EVENT) {
657                 va_start(ap, fmt);
658
659                 fprintf(eventlog, "%s asterisk[%d]: ", date, getpid());
660                 vfprintf(eventlog, fmt, ap);
661                 fflush(eventlog);
662
663                 va_end(ap);
664                 ast_mutex_unlock(&loglock);
665                 return;
666         }
667
668         if (logchannels) {
669                 chan = logchannels;
670                 while(chan && !chan->disabled) {
671                         /* Check syslog channels */
672                         if (chan->type == LOG_SYSLOG && (chan->logmask & (1 << level))) {
673                                 va_start(ap, fmt);
674                                 ast_log_vsyslog(level, file, line, function, fmt, ap);
675                                 va_end(ap);
676                         /* Console channels */
677                         } else if ((chan->logmask & (1 << level)) && (chan->type == LOGTYPE_CONSOLE)) {
678                                 char linestr[128];
679                                 char tmp1[80], tmp2[80], tmp3[80], tmp4[80];
680
681                                 if (level != __LOG_VERBOSE) {
682                                         sprintf(linestr, "%d", line);
683                                         snprintf(buf, sizeof(buf), option_timestamp ? "[%s] %s[%ld]: %s:%s %s: " : "%s %s[%ld]: %s:%s %s: ",
684                                                 date,
685                                                 term_color(tmp1, levels[level], colors[level], 0, sizeof(tmp1)),
686                                                 (long)GETTID(),
687                                                 term_color(tmp2, file, COLOR_BRWHITE, 0, sizeof(tmp2)),
688                                                 term_color(tmp3, linestr, COLOR_BRWHITE, 0, sizeof(tmp3)),
689                                                 term_color(tmp4, function, COLOR_BRWHITE, 0, sizeof(tmp4)));
690                                         
691                                         ast_console_puts(buf);
692                                         va_start(ap, fmt);
693                                         vsnprintf(buf, sizeof(buf), fmt, ap);
694                                         va_end(ap);
695                                         ast_console_puts(buf);
696                                 }
697                         /* File channels */
698                         } else if ((chan->logmask & (1 << level)) && (chan->fileptr)) {
699                                 int res;
700                                 snprintf(buf, sizeof(buf), option_timestamp ? "[%s] %s[%ld]: " : "%s %s[%ld] %s: ", date,
701                                         levels[level], (long)GETTID(), file);
702                                 res = fprintf(chan->fileptr, buf);
703                                 if (res <= 0 && buf[0] != '\0') {       /* Error, no characters printed */
704                                         fprintf(stderr,"**** Asterisk Logging Error: ***********\n");
705                                         if (errno == ENOMEM || errno == ENOSPC) {
706                                                 fprintf(stderr, "Asterisk logging error: Out of disk space, can't log to log file %s\n", chan->filename);
707                                         } else
708                                                 fprintf(stderr, "Logger Warning: Unable to write to log file '%s': %s (disabled)\n", chan->filename, strerror(errno));
709                                         manager_event(EVENT_FLAG_SYSTEM, "LogChannel", "Channel: %s\r\nEnabled: No\r\nReason: %d - %s\r\n", chan->filename, errno, strerror(errno));
710                                         chan->disabled = 1;     
711                                 } else {
712                                         /* No error message, continue printing */
713                                         va_start(ap, fmt);
714                                         vsnprintf(buf, sizeof(buf), fmt, ap);
715                                         va_end(ap);
716                                         fputs(buf, chan->fileptr);
717                                         fflush(chan->fileptr);
718                                 }
719                         }
720                         chan = chan->next;
721                 }
722         } else {
723                 /* 
724                  * we don't have the logger chain configured yet,
725                  * so just log to stdout 
726                 */
727                 if (level != __LOG_VERBOSE) {
728                         va_start(ap, fmt);
729                         vsnprintf(buf, sizeof(buf), fmt, ap);
730                         va_end(ap);
731                         fputs(buf, stdout);
732                 }
733         }
734
735         ast_mutex_unlock(&loglock);
736         /* end critical section */
737         if (pending_logger_reload) {
738                 reload_logger(1);
739                 ast_log(LOG_EVENT,"Rotated Logs Per SIGXFSZ (Exceeded file size limit)\n");
740                 if (option_verbose)
741                         ast_verbose("Rotated Logs Per SIGXFSZ (Exceeded file size limit)\n");
742         }
743 }
744
745 extern void ast_verbose(const char *fmt, ...)
746 {
747         static char stuff[4096];
748         static int pos = 0, opos;
749         static int replacelast = 0, complete;
750         struct msglist *m;
751         struct verb *v;
752         time_t t;
753         struct tm tm;
754         char date[40];
755         char *datefmt;
756         
757         va_list ap;
758         va_start(ap, fmt);
759         ast_mutex_lock(&msglist_lock);
760         time(&t);
761         localtime_r(&t, &tm);
762         strftime(date, sizeof(date), dateformat, &tm);
763
764         if (option_timestamp) {
765                 datefmt = alloca(strlen(date) + 3 + strlen(fmt) + 1);
766                 if (datefmt) {
767                         sprintf(datefmt, "[%s] %s", date, fmt);
768                         fmt = datefmt;
769                 }
770         }
771         vsnprintf(stuff + pos, sizeof(stuff) - pos, fmt, ap);
772         opos = pos;
773         pos = strlen(stuff);
774
775
776         if (stuff[strlen(stuff)-1] == '\n') 
777                 complete = 1;
778         else
779                 complete=0;
780         if (complete) {
781                 if (msgcnt < MAX_MSG_QUEUE) {
782                         /* Allocate new structure */
783                         m = malloc(sizeof(struct msglist));
784                         msgcnt++;
785                 } else {
786                         /* Recycle the oldest entry */
787                         m = list;
788                         list = list->next;
789                         free(m->msg);
790                 }
791                 if (m) {
792                         m->msg = strdup(stuff);
793                         if (m->msg) {
794                                 if (last)
795                                         last->next = m;
796                                 else
797                                         list = m;
798                                 m->next = NULL;
799                                 last = m;
800                         } else {
801                                 msgcnt--;
802                                 ast_log(LOG_ERROR, "Out of memory\n");
803                                 free(m);
804                         }
805                 }
806         }
807         if (verboser) {
808                 v = verboser;
809                 while(v) {
810                         v->verboser(stuff, opos, replacelast, complete);
811                         v = v->next;
812                 }
813         } /* else
814                 fprintf(stdout, stuff + opos); */
815         ast_log(LOG_VERBOSE, "%s", stuff);
816         if (strlen(stuff)) {
817                 if (stuff[strlen(stuff)-1] != '\n') 
818                         replacelast = 1;
819                 else 
820                         replacelast = pos = 0;
821         }
822         va_end(ap);
823
824         ast_mutex_unlock(&msglist_lock);
825 }
826
827 int ast_verbose_dmesg(void (*v)(const char *string, int opos, int replacelast, int complete))
828 {
829         struct msglist *m;
830         ast_mutex_lock(&msglist_lock);
831         m = list;
832         while(m) {
833                 /* Send all the existing entries that we have queued (i.e. they're likely to have missed) */
834                 v(m->msg, 0, 0, 1);
835                 m = m->next;
836         }
837         ast_mutex_unlock(&msglist_lock);
838         return 0;
839 }
840
841 int ast_register_verbose(void (*v)(const char *string, int opos, int replacelast, int complete)) 
842 {
843         struct msglist *m;
844         struct verb *tmp;
845         /* XXX Should be more flexible here, taking > 1 verboser XXX */
846         if ((tmp = malloc(sizeof (struct verb)))) {
847                 tmp->verboser = v;
848                 ast_mutex_lock(&msglist_lock);
849                 tmp->next = verboser;
850                 verboser = tmp;
851                 m = list;
852                 while(m) {
853                         /* Send all the existing entries that we have queued (i.e. they're likely to have missed) */
854                         v(m->msg, 0, 0, 1);
855                         m = m->next;
856                 }
857                 ast_mutex_unlock(&msglist_lock);
858                 return 0;
859         }
860         return -1;
861 }
862
863 int ast_unregister_verbose(void (*v)(const char *string, int opos, int replacelast, int complete))
864 {
865         int res = -1;
866         struct verb *tmp, *tmpl=NULL;
867         ast_mutex_lock(&msglist_lock);
868         tmp = verboser;
869         while(tmp) {
870                 if (tmp->verboser == v) {
871                         if (tmpl)
872                                 tmpl->next = tmp->next;
873                         else
874                                 verboser = tmp->next;
875                         free(tmp);
876                         break;
877                 }
878                 tmpl = tmp;
879                 tmp = tmp->next;
880         }
881         if (tmp)
882                 res = 0;
883         ast_mutex_unlock(&msglist_lock);
884         return res;
885 }