Let's also include aclocal.m4
[asterisk-bristuff.git] / main / logger.c
blob8f6ebaf80d2de2f50d5b765870c4546047b3b488
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
19 /*! \file
21 * \brief Asterisk Logger
23 * Logging routines
25 * \author Mark Spencer <markster@digium.com>
28 #include "asterisk.h"
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32 #include <signal.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <unistd.h>
36 #include <time.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include <errno.h>
40 #include <sys/stat.h>
41 #if ((defined(AST_DEVMODE)) && (defined(linux)))
42 #include <execinfo.h>
43 #define MAX_BACKTRACE_FRAMES 20
44 #endif
46 #define SYSLOG_NAMES /* so we can map syslog facilities names to their numeric values,
47 from <syslog.h> which is included by logger.h */
48 #include <syslog.h>
50 static int syslog_level_map[] = {
51 LOG_DEBUG,
52 LOG_INFO, /* arbitrary equivalent of LOG_EVENT */
53 LOG_NOTICE,
54 LOG_WARNING,
55 LOG_ERR,
56 LOG_DEBUG,
57 LOG_DEBUG
60 #define SYSLOG_NLEVELS sizeof(syslog_level_map) / sizeof(int)
62 #include "asterisk/logger.h"
63 #include "asterisk/lock.h"
64 #include "asterisk/options.h"
65 #include "asterisk/channel.h"
66 #include "asterisk/config.h"
67 #include "asterisk/term.h"
68 #include "asterisk/cli.h"
69 #include "asterisk/utils.h"
70 #include "asterisk/manager.h"
71 #include "asterisk/threadstorage.h"
73 #if defined(__linux__) && !defined(__NR_gettid)
74 #include <asm/unistd.h>
75 #endif
77 #if defined(__linux__) && defined(__NR_gettid)
78 #define GETTID() syscall(__NR_gettid)
79 #else
80 #define GETTID() getpid()
81 #endif
84 static char dateformat[256] = "%b %e %T"; /* Original Asterisk Format */
86 static int filesize_reload_needed;
87 static int global_logmask = -1;
89 static struct {
90 unsigned int queue_log:1;
91 unsigned int event_log:1;
92 } logfiles = { 1, 1 };
94 static char hostname[MAXHOSTNAMELEN];
96 enum logtypes {
97 LOGTYPE_SYSLOG,
98 LOGTYPE_FILE,
99 LOGTYPE_CONSOLE,
102 struct logchannel {
103 int logmask; /* What to log to this channel */
104 int disabled; /* If this channel is disabled or not */
105 int facility; /* syslog facility */
106 enum logtypes type; /* Type of log channel */
107 FILE *fileptr; /* logfile logging file pointer */
108 char filename[256]; /* Filename */
109 AST_LIST_ENTRY(logchannel) list;
112 static AST_LIST_HEAD_STATIC(logchannels, logchannel);
114 static FILE *eventlog;
115 static FILE *qlog;
117 static char *levels[] = {
118 "DEBUG",
119 "EVENT",
120 "NOTICE",
121 "WARNING",
122 "ERROR",
123 "VERBOSE",
124 "DTMF"
127 static int colors[] = {
128 COLOR_BRGREEN,
129 COLOR_BRBLUE,
130 COLOR_YELLOW,
131 COLOR_BRRED,
132 COLOR_RED,
133 COLOR_GREEN,
134 COLOR_BRGREEN
137 AST_THREADSTORAGE(verbose_buf, verbose_buf_init);
138 #define VERBOSE_BUF_INIT_SIZE 128
140 AST_THREADSTORAGE(log_buf, log_buf_init);
141 #define LOG_BUF_INIT_SIZE 128
143 static int make_components(char *s, int lineno)
145 char *w;
146 int res = 0;
147 char *stringp = s;
149 while ((w = strsep(&stringp, ","))) {
150 w = ast_skip_blanks(w);
151 if (!strcasecmp(w, "error"))
152 res |= (1 << __LOG_ERROR);
153 else if (!strcasecmp(w, "warning"))
154 res |= (1 << __LOG_WARNING);
155 else if (!strcasecmp(w, "notice"))
156 res |= (1 << __LOG_NOTICE);
157 else if (!strcasecmp(w, "event"))
158 res |= (1 << __LOG_EVENT);
159 else if (!strcasecmp(w, "debug"))
160 res |= (1 << __LOG_DEBUG);
161 else if (!strcasecmp(w, "verbose"))
162 res |= (1 << __LOG_VERBOSE);
163 else if (!strcasecmp(w, "dtmf"))
164 res |= (1 << __LOG_DTMF);
165 else {
166 fprintf(stderr, "Logfile Warning: Unknown keyword '%s' at line %d of logger.conf\n", w, lineno);
170 return res;
173 static struct logchannel *make_logchannel(char *channel, char *components, int lineno)
175 struct logchannel *chan;
176 char *facility;
177 #ifndef SOLARIS
178 CODE *cptr;
179 #endif
181 if (ast_strlen_zero(channel) || !(chan = ast_calloc(1, sizeof(*chan))))
182 return NULL;
184 if (!strcasecmp(channel, "console")) {
185 chan->type = LOGTYPE_CONSOLE;
186 } else if (!strncasecmp(channel, "syslog", 6)) {
188 * syntax is:
189 * syslog.facility => level,level,level
191 facility = strchr(channel, '.');
192 if(!facility++ || !facility) {
193 facility = "local0";
196 #ifndef SOLARIS
198 * Walk through the list of facilitynames (defined in sys/syslog.h)
199 * to see if we can find the one we have been given
201 chan->facility = -1;
202 cptr = facilitynames;
203 while (cptr->c_name) {
204 if (!strcasecmp(facility, cptr->c_name)) {
205 chan->facility = cptr->c_val;
206 break;
208 cptr++;
210 #else
211 chan->facility = -1;
212 if (!strcasecmp(facility, "kern"))
213 chan->facility = LOG_KERN;
214 else if (!strcasecmp(facility, "USER"))
215 chan->facility = LOG_USER;
216 else if (!strcasecmp(facility, "MAIL"))
217 chan->facility = LOG_MAIL;
218 else if (!strcasecmp(facility, "DAEMON"))
219 chan->facility = LOG_DAEMON;
220 else if (!strcasecmp(facility, "AUTH"))
221 chan->facility = LOG_AUTH;
222 else if (!strcasecmp(facility, "SYSLOG"))
223 chan->facility = LOG_SYSLOG;
224 else if (!strcasecmp(facility, "LPR"))
225 chan->facility = LOG_LPR;
226 else if (!strcasecmp(facility, "NEWS"))
227 chan->facility = LOG_NEWS;
228 else if (!strcasecmp(facility, "UUCP"))
229 chan->facility = LOG_UUCP;
230 else if (!strcasecmp(facility, "CRON"))
231 chan->facility = LOG_CRON;
232 else if (!strcasecmp(facility, "LOCAL0"))
233 chan->facility = LOG_LOCAL0;
234 else if (!strcasecmp(facility, "LOCAL1"))
235 chan->facility = LOG_LOCAL1;
236 else if (!strcasecmp(facility, "LOCAL2"))
237 chan->facility = LOG_LOCAL2;
238 else if (!strcasecmp(facility, "LOCAL3"))
239 chan->facility = LOG_LOCAL3;
240 else if (!strcasecmp(facility, "LOCAL4"))
241 chan->facility = LOG_LOCAL4;
242 else if (!strcasecmp(facility, "LOCAL5"))
243 chan->facility = LOG_LOCAL5;
244 else if (!strcasecmp(facility, "LOCAL6"))
245 chan->facility = LOG_LOCAL6;
246 else if (!strcasecmp(facility, "LOCAL7"))
247 chan->facility = LOG_LOCAL7;
248 #endif /* Solaris */
250 if (0 > chan->facility) {
251 fprintf(stderr, "Logger Warning: bad syslog facility in logger.conf\n");
252 free(chan);
253 return NULL;
256 chan->type = LOGTYPE_SYSLOG;
257 snprintf(chan->filename, sizeof(chan->filename), "%s", channel);
258 openlog("asterisk", LOG_PID, chan->facility);
259 } else {
260 if (channel[0] == '/') {
261 if(!ast_strlen_zero(hostname)) {
262 snprintf(chan->filename, sizeof(chan->filename) - 1,"%s.%s", channel, hostname);
263 } else {
264 ast_copy_string(chan->filename, channel, sizeof(chan->filename));
268 if(!ast_strlen_zero(hostname)) {
269 snprintf(chan->filename, sizeof(chan->filename), "%s/%s.%s",(char *)ast_config_AST_LOG_DIR, channel, hostname);
270 } else {
271 snprintf(chan->filename, sizeof(chan->filename), "%s/%s", (char *)ast_config_AST_LOG_DIR, channel);
273 chan->fileptr = fopen(chan->filename, "a");
274 if (!chan->fileptr) {
275 /* Can't log here, since we're called with a lock */
276 fprintf(stderr, "Logger Warning: Unable to open log file '%s': %s\n", chan->filename, strerror(errno));
278 chan->type = LOGTYPE_FILE;
280 chan->logmask = make_components(components, lineno);
281 return chan;
284 static void init_logger_chain(void)
286 struct logchannel *chan;
287 struct ast_config *cfg;
288 struct ast_variable *var;
289 const char *s;
291 /* delete our list of log channels */
292 AST_LIST_LOCK(&logchannels);
293 while ((chan = AST_LIST_REMOVE_HEAD(&logchannels, list)))
294 free(chan);
295 AST_LIST_UNLOCK(&logchannels);
297 global_logmask = 0;
298 errno = 0;
299 /* close syslog */
300 closelog();
302 cfg = ast_config_load("logger.conf");
304 /* If no config file, we're fine, set default options. */
305 if (!cfg) {
306 if (errno)
307 fprintf(stderr, "Unable to open logger.conf: %s; default settings will be used.\n", strerror(errno));
308 else
309 fprintf(stderr, "Errors detected in logger.conf: see above; default settings will be used.\n");
310 if (!(chan = ast_calloc(1, sizeof(*chan))))
311 return;
312 chan->type = LOGTYPE_CONSOLE;
313 chan->logmask = 28; /*warning,notice,error */
314 AST_LIST_LOCK(&logchannels);
315 AST_LIST_INSERT_HEAD(&logchannels, chan, list);
316 AST_LIST_UNLOCK(&logchannels);
317 global_logmask |= chan->logmask;
318 return;
321 if ((s = ast_variable_retrieve(cfg, "general", "appendhostname"))) {
322 if (ast_true(s)) {
323 if (gethostname(hostname, sizeof(hostname) - 1)) {
324 ast_copy_string(hostname, "unknown", sizeof(hostname));
325 ast_log(LOG_WARNING, "What box has no hostname???\n");
327 } else
328 hostname[0] = '\0';
329 } else
330 hostname[0] = '\0';
331 if ((s = ast_variable_retrieve(cfg, "general", "dateformat")))
332 ast_copy_string(dateformat, s, sizeof(dateformat));
333 else
334 ast_copy_string(dateformat, "%b %e %T", sizeof(dateformat));
335 if ((s = ast_variable_retrieve(cfg, "general", "queue_log")))
336 logfiles.queue_log = ast_true(s);
337 if ((s = ast_variable_retrieve(cfg, "general", "event_log")))
338 logfiles.event_log = ast_true(s);
340 AST_LIST_LOCK(&logchannels);
341 var = ast_variable_browse(cfg, "logfiles");
342 for (; var; var = var->next) {
343 if (!(chan = make_logchannel(var->name, var->value, var->lineno)))
344 continue;
345 AST_LIST_INSERT_HEAD(&logchannels, chan, list);
346 global_logmask |= chan->logmask;
348 AST_LIST_UNLOCK(&logchannels);
350 ast_config_destroy(cfg);
353 void ast_queue_log(const char *queuename, const char *callid, const char *agent, const char *event, const char *fmt, ...)
355 va_list ap;
356 AST_LIST_LOCK(&logchannels);
357 if (qlog) {
358 va_start(ap, fmt);
359 fprintf(qlog, "%ld|%s|%s|%s|%s|", (long)time(NULL), callid, queuename, agent, event);
360 vfprintf(qlog, fmt, ap);
361 fprintf(qlog, "\n");
362 va_end(ap);
363 fflush(qlog);
365 AST_LIST_UNLOCK(&logchannels);
368 int reload_logger(int rotate)
370 char old[PATH_MAX] = "";
371 char new[PATH_MAX];
372 int event_rotate = rotate, queue_rotate = rotate;
373 struct logchannel *f;
374 FILE *myf;
375 int x, res = 0;
377 AST_LIST_LOCK(&logchannels);
379 if (eventlog)
380 fclose(eventlog);
381 else
382 event_rotate = 0;
383 eventlog = NULL;
385 if (qlog)
386 fclose(qlog);
387 else
388 queue_rotate = 0;
389 qlog = NULL;
391 mkdir((char *)ast_config_AST_LOG_DIR, 0755);
393 AST_LIST_TRAVERSE(&logchannels, f, list) {
394 if (f->disabled) {
395 f->disabled = 0; /* Re-enable logging at reload */
396 manager_event(EVENT_FLAG_SYSTEM, "LogChannel", "Channel: %s\r\nEnabled: Yes\r\n", f->filename);
398 if (f->fileptr && (f->fileptr != stdout) && (f->fileptr != stderr)) {
399 fclose(f->fileptr); /* Close file */
400 f->fileptr = NULL;
401 if (rotate) {
402 ast_copy_string(old, f->filename, sizeof(old));
404 for (x = 0; ; x++) {
405 snprintf(new, sizeof(new), "%s.%d", f->filename, x);
406 myf = fopen((char *)new, "r");
407 if (myf)
408 fclose(myf);
409 else
410 break;
413 /* do it */
414 if (rename(old,new))
415 fprintf(stderr, "Unable to rename file '%s' to '%s'\n", old, new);
420 filesize_reload_needed = 0;
422 init_logger_chain();
424 if (logfiles.event_log) {
425 snprintf(old, sizeof(old), "%s/%s", (char *)ast_config_AST_LOG_DIR, EVENTLOG);
426 if (event_rotate) {
427 for (x=0;;x++) {
428 snprintf(new, sizeof(new), "%s/%s.%d", (char *)ast_config_AST_LOG_DIR, EVENTLOG,x);
429 myf = fopen((char *)new, "r");
430 if (myf) /* File exists */
431 fclose(myf);
432 else
433 break;
436 /* do it */
437 if (rename(old,new))
438 ast_log(LOG_ERROR, "Unable to rename file '%s' to '%s'\n", old, new);
441 eventlog = fopen(old, "a");
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 } else {
447 ast_log(LOG_ERROR, "Unable to create event log: %s\n", strerror(errno));
448 res = -1;
452 if (logfiles.queue_log) {
453 snprintf(old, sizeof(old), "%s/%s", (char *)ast_config_AST_LOG_DIR, QUEUELOG);
454 if (queue_rotate) {
455 for (x = 0; ; x++) {
456 snprintf(new, sizeof(new), "%s/%s.%d", (char *)ast_config_AST_LOG_DIR, QUEUELOG, x);
457 myf = fopen((char *)new, "r");
458 if (myf) /* File exists */
459 fclose(myf);
460 else
461 break;
464 /* do it */
465 if (rename(old, new))
466 ast_log(LOG_ERROR, "Unable to rename file '%s' to '%s'\n", old, new);
469 qlog = fopen(old, "a");
470 if (qlog) {
471 ast_queue_log("NONE", "NONE", "NONE", "CONFIGRELOAD", "%s", "");
472 ast_log(LOG_EVENT, "Restarted Asterisk Queue Logger\n");
473 if (option_verbose)
474 ast_verbose("Asterisk Queue Logger restarted\n");
475 } else {
476 ast_log(LOG_ERROR, "Unable to create queue log: %s\n", strerror(errno));
477 res = -1;
481 AST_LIST_UNLOCK(&logchannels);
483 return res;
486 /*! \brief Reload the logger module without rotating log files (also used from loader.c during
487 a full Asterisk reload) */
488 int logger_reload(void)
490 if(reload_logger(0))
491 return RESULT_FAILURE;
492 return RESULT_SUCCESS;
495 static int handle_logger_reload(int fd, int argc, char *argv[])
497 int result = logger_reload();
498 if (result == RESULT_FAILURE)
499 ast_cli(fd, "Failed to reload the logger\n");
500 return result;
503 static int handle_logger_rotate(int fd, int argc, char *argv[])
505 if(reload_logger(1)) {
506 ast_cli(fd, "Failed to reload the logger and rotate log files\n");
507 return RESULT_FAILURE;
509 return RESULT_SUCCESS;
512 /*! \brief CLI command to show logging system configuration */
513 static int handle_logger_show_channels(int fd, int argc, char *argv[])
515 #define FORMATL "%-35.35s %-8.8s %-9.9s "
516 struct logchannel *chan;
518 ast_cli(fd,FORMATL, "Channel", "Type", "Status");
519 ast_cli(fd, "Configuration\n");
520 ast_cli(fd,FORMATL, "-------", "----", "------");
521 ast_cli(fd, "-------------\n");
522 AST_LIST_LOCK(&logchannels);
523 AST_LIST_TRAVERSE(&logchannels, chan, list) {
524 ast_cli(fd, FORMATL, chan->filename, chan->type==LOGTYPE_CONSOLE ? "Console" : (chan->type==LOGTYPE_SYSLOG ? "Syslog" : "File"),
525 chan->disabled ? "Disabled" : "Enabled");
526 ast_cli(fd, " - ");
527 if (chan->logmask & (1 << __LOG_DEBUG))
528 ast_cli(fd, "Debug ");
529 if (chan->logmask & (1 << __LOG_DTMF))
530 ast_cli(fd, "DTMF ");
531 if (chan->logmask & (1 << __LOG_VERBOSE))
532 ast_cli(fd, "Verbose ");
533 if (chan->logmask & (1 << __LOG_WARNING))
534 ast_cli(fd, "Warning ");
535 if (chan->logmask & (1 << __LOG_NOTICE))
536 ast_cli(fd, "Notice ");
537 if (chan->logmask & (1 << __LOG_ERROR))
538 ast_cli(fd, "Error ");
539 if (chan->logmask & (1 << __LOG_EVENT))
540 ast_cli(fd, "Event ");
541 ast_cli(fd, "\n");
543 AST_LIST_UNLOCK(&logchannels);
544 ast_cli(fd, "\n");
546 return RESULT_SUCCESS;
549 struct verb {
550 void (*verboser)(const char *string);
551 AST_LIST_ENTRY(verb) list;
554 static AST_LIST_HEAD_STATIC(verbosers, verb);
556 static char logger_reload_help[] =
557 "Usage: logger reload\n"
558 " Reloads the logger subsystem state. Use after restarting syslogd(8) if you are using syslog logging.\n";
560 static char logger_rotate_help[] =
561 "Usage: logger rotate\n"
562 " Rotates and Reopens the log files.\n";
564 static char logger_show_channels_help[] =
565 "Usage: logger show channels\n"
566 " List configured logger channels.\n";
568 static struct ast_cli_entry cli_logger[] = {
569 { { "logger", "show", "channels", NULL },
570 handle_logger_show_channels, "List configured log channels",
571 logger_show_channels_help },
573 { { "logger", "reload", NULL },
574 handle_logger_reload, "Reopens the log files",
575 logger_reload_help },
577 { { "logger", "rotate", NULL },
578 handle_logger_rotate, "Rotates and reopens the log files",
579 logger_rotate_help },
582 static int handle_SIGXFSZ(int sig)
584 /* Indicate need to reload */
585 filesize_reload_needed = 1;
586 return 0;
589 int init_logger(void)
591 char tmp[256];
592 int res = 0;
594 /* auto rotate if sig SIGXFSZ comes a-knockin */
595 (void) signal(SIGXFSZ,(void *) handle_SIGXFSZ);
597 /* register the logger cli commands */
598 ast_cli_register_multiple(cli_logger, sizeof(cli_logger) / sizeof(struct ast_cli_entry));
600 mkdir((char *)ast_config_AST_LOG_DIR, 0755);
602 /* create log channels */
603 init_logger_chain();
605 /* create the eventlog */
606 if (logfiles.event_log) {
607 mkdir((char *)ast_config_AST_LOG_DIR, 0755);
608 snprintf(tmp, sizeof(tmp), "%s/%s", (char *)ast_config_AST_LOG_DIR, EVENTLOG);
609 eventlog = fopen((char *)tmp, "a");
610 if (eventlog) {
611 ast_log(LOG_EVENT, "Started Asterisk Event Logger\n");
612 if (option_verbose)
613 ast_verbose("Asterisk Event Logger Started %s\n",(char *)tmp);
614 } else {
615 ast_log(LOG_ERROR, "Unable to create event log: %s\n", strerror(errno));
616 res = -1;
620 if (logfiles.queue_log) {
621 snprintf(tmp, sizeof(tmp), "%s/%s", (char *)ast_config_AST_LOG_DIR, QUEUELOG);
622 qlog = fopen(tmp, "a");
623 ast_queue_log("NONE", "NONE", "NONE", "QUEUESTART", "%s", "");
625 return res;
628 void close_logger(void)
630 struct logchannel *f;
632 AST_LIST_LOCK(&logchannels);
634 if (eventlog) {
635 fclose(eventlog);
636 eventlog = NULL;
639 if (qlog) {
640 fclose(qlog);
641 qlog = NULL;
644 AST_LIST_TRAVERSE(&logchannels, f, list) {
645 if (f->fileptr && (f->fileptr != stdout) && (f->fileptr != stderr)) {
646 fclose(f->fileptr);
647 f->fileptr = NULL;
651 closelog(); /* syslog */
653 AST_LIST_UNLOCK(&logchannels);
655 return;
658 static void ast_log_vsyslog(int level, const char *file, int line, const char *function, const char *fmt, va_list args)
660 char buf[BUFSIZ];
661 char *s;
663 if (level >= SYSLOG_NLEVELS) {
664 /* we are locked here, so cannot ast_log() */
665 fprintf(stderr, "ast_log_vsyslog called with bogus level: %d\n", level);
666 return;
668 if (level == __LOG_VERBOSE) {
669 snprintf(buf, sizeof(buf), "VERBOSE[%ld]: ", (long)GETTID());
670 level = __LOG_DEBUG;
671 } else if (level == __LOG_DTMF) {
672 snprintf(buf, sizeof(buf), "DTMF[%ld]: ", (long)GETTID());
673 level = __LOG_DEBUG;
674 } else {
675 snprintf(buf, sizeof(buf), "%s[%ld]: %s:%d in %s: ",
676 levels[level], (long)GETTID(), file, line, function);
678 s = buf + strlen(buf);
679 vsnprintf(s, sizeof(buf) - strlen(buf), fmt, args);
680 term_strip(s, s, strlen(s) + 1);
681 syslog(syslog_level_map[level], "%s", buf);
685 * \brief send log messages to syslog and/or the console
687 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
689 struct logchannel *chan;
690 struct ast_dynamic_str *buf;
691 time_t t;
692 struct tm tm;
693 char date[256];
695 va_list ap;
697 if (!(buf = ast_dynamic_str_thread_get(&log_buf, LOG_BUF_INIT_SIZE)))
698 return;
700 if (AST_LIST_EMPTY(&logchannels))
703 * we don't have the logger chain configured yet,
704 * so just log to stdout
706 if (level != __LOG_VERBOSE) {
707 int res;
708 va_start(ap, fmt);
709 res = ast_dynamic_str_thread_set_va(&buf, BUFSIZ, &log_buf, fmt, ap);
710 va_end(ap);
711 if (res != AST_DYNSTR_BUILD_FAILED) {
712 term_filter_escapes(buf->str);
713 fputs(buf->str, stdout);
716 return;
719 /* don't display LOG_DEBUG messages unless option_verbose _or_ option_debug
720 are non-zero; LOG_DEBUG messages can still be displayed if option_debug
721 is zero, if option_verbose is non-zero (this allows for 'level zero'
722 LOG_DEBUG messages to be displayed, if the logmask on any channel
723 allows it)
725 if (!option_verbose && !option_debug && (level == __LOG_DEBUG))
726 return;
728 /* Ignore anything that never gets logged anywhere */
729 if (!(global_logmask & (1 << level)))
730 return;
732 /* Ignore anything other than the currently debugged file if there is one */
733 if ((level == __LOG_DEBUG) && !ast_strlen_zero(debug_filename) && strcasecmp(debug_filename, file))
734 return;
736 time(&t);
737 ast_localtime(&t, &tm, NULL);
738 strftime(date, sizeof(date), dateformat, &tm);
740 AST_LIST_LOCK(&logchannels);
742 if (logfiles.event_log && level == __LOG_EVENT) {
743 va_start(ap, fmt);
745 fprintf(eventlog, "%s asterisk[%ld]: ", date, (long)getpid());
746 vfprintf(eventlog, fmt, ap);
747 fflush(eventlog);
749 va_end(ap);
750 AST_LIST_UNLOCK(&logchannels);
751 return;
754 AST_LIST_TRAVERSE(&logchannels, chan, list) {
755 if (chan->disabled)
756 break;
757 /* Check syslog channels */
758 if (chan->type == LOGTYPE_SYSLOG && (chan->logmask & (1 << level))) {
759 va_start(ap, fmt);
760 ast_log_vsyslog(level, file, line, function, fmt, ap);
761 va_end(ap);
762 /* Console channels */
763 } else if ((chan->logmask & (1 << level)) && (chan->type == LOGTYPE_CONSOLE)) {
764 char linestr[128];
765 char tmp1[80], tmp2[80], tmp3[80], tmp4[80];
767 if (level != __LOG_VERBOSE) {
768 int res;
769 sprintf(linestr, "%d", line);
770 ast_dynamic_str_thread_set(&buf, BUFSIZ, &log_buf,
771 "[%s] %s[%ld]: %s:%s %s: ",
772 date,
773 term_color(tmp1, levels[level], colors[level], 0, sizeof(tmp1)),
774 (long)GETTID(),
775 term_color(tmp2, file, COLOR_BRWHITE, 0, sizeof(tmp2)),
776 term_color(tmp3, linestr, COLOR_BRWHITE, 0, sizeof(tmp3)),
777 term_color(tmp4, function, COLOR_BRWHITE, 0, sizeof(tmp4)));
778 /*filter to the console!*/
779 term_filter_escapes(buf->str);
780 ast_console_puts_mutable(buf->str);
782 va_start(ap, fmt);
783 res = ast_dynamic_str_thread_set_va(&buf, BUFSIZ, &log_buf, fmt, ap);
784 va_end(ap);
785 if (res != AST_DYNSTR_BUILD_FAILED)
786 ast_console_puts_mutable(buf->str);
788 /* File channels */
789 } else if ((chan->logmask & (1 << level)) && (chan->fileptr)) {
790 int res;
791 ast_dynamic_str_thread_set(&buf, BUFSIZ, &log_buf,
792 "[%s] %s[%ld] %s: ",
793 date, levels[level], (long)GETTID(), file);
794 res = fprintf(chan->fileptr, "%s", buf->str);
795 if (res <= 0 && !ast_strlen_zero(buf->str)) { /* Error, no characters printed */
796 fprintf(stderr,"**** Asterisk Logging Error: ***********\n");
797 if (errno == ENOMEM || errno == ENOSPC) {
798 fprintf(stderr, "Asterisk logging error: Out of disk space, can't log to log file %s\n", chan->filename);
799 } else
800 fprintf(stderr, "Logger Warning: Unable to write to log file '%s': %s (disabled)\n", chan->filename, strerror(errno));
801 manager_event(EVENT_FLAG_SYSTEM, "LogChannel", "Channel: %s\r\nEnabled: No\r\nReason: %d - %s\r\n", chan->filename, errno, strerror(errno));
802 chan->disabled = 1;
803 } else {
804 int res;
805 /* No error message, continue printing */
806 va_start(ap, fmt);
807 res = ast_dynamic_str_thread_set_va(&buf, BUFSIZ, &log_buf, fmt, ap);
808 va_end(ap);
809 if (res != AST_DYNSTR_BUILD_FAILED) {
810 term_strip(buf->str, buf->str, buf->len);
811 fputs(buf->str, chan->fileptr);
812 fflush(chan->fileptr);
818 AST_LIST_UNLOCK(&logchannels);
820 if (filesize_reload_needed) {
821 reload_logger(1);
822 ast_log(LOG_EVENT,"Rotated Logs Per SIGXFSZ (Exceeded file size limit)\n");
823 if (option_verbose)
824 ast_verbose("Rotated Logs Per SIGXFSZ (Exceeded file size limit)\n");
828 void ast_backtrace(void)
830 #ifdef linux
831 #ifdef AST_DEVMODE
832 int count=0, i=0;
833 void **addresses;
834 char **strings;
836 if ((addresses = ast_calloc(MAX_BACKTRACE_FRAMES, sizeof(*addresses)))) {
837 count = backtrace(addresses, MAX_BACKTRACE_FRAMES);
838 if ((strings = backtrace_symbols(addresses, count))) {
839 ast_log(LOG_DEBUG, "Got %d backtrace record%c\n", count, count != 1 ? 's' : ' ');
840 for (i=0; i < count ; i++) {
841 #if __WORDSIZE == 32
842 ast_log(LOG_DEBUG, "#%d: [%08X] %s\n", i, (unsigned int)addresses[i], strings[i]);
843 #elif __WORDSIZE == 64
844 ast_log(LOG_DEBUG, "#%d: [%016lX] %s\n", i, (unsigned long)addresses[i], strings[i]);
845 #endif
847 free(strings);
848 } else {
849 ast_log(LOG_DEBUG, "Could not allocate memory for backtrace\n");
851 free(addresses);
853 #else
854 ast_log(LOG_WARNING, "Must run configure with '--enable-dev-mode' for stack backtraces.\n");
855 #endif
856 #else /* ndef linux */
857 ast_log(LOG_WARNING, "Inline stack backtraces are only available on the Linux platform.\n");
858 #endif
861 void ast_verbose(const char *fmt, ...)
863 struct verb *v;
864 struct ast_dynamic_str *buf;
865 int res;
866 va_list ap;
868 if (ast_opt_timestamp) {
869 time_t t;
870 struct tm tm;
871 char date[40];
872 char *datefmt;
874 time(&t);
875 ast_localtime(&t, &tm, NULL);
876 strftime(date, sizeof(date), dateformat, &tm);
877 datefmt = alloca(strlen(date) + 3 + strlen(fmt) + 1);
878 sprintf(datefmt, "%c[%s] %s", 127, date, fmt);
879 fmt = datefmt;
880 } else {
881 char *tmp = alloca(strlen(fmt) + 2);
882 sprintf(tmp, "%c%s", 127, fmt);
883 fmt = tmp;
886 if (!(buf = ast_dynamic_str_thread_get(&verbose_buf, VERBOSE_BUF_INIT_SIZE)))
887 return;
889 va_start(ap, fmt);
890 res = ast_dynamic_str_thread_set_va(&buf, 0, &verbose_buf, fmt, ap);
891 va_end(ap);
893 if (res == AST_DYNSTR_BUILD_FAILED)
894 return;
896 /* filter out possibly hazardous escape sequences */
897 term_filter_escapes(buf->str);
899 AST_LIST_LOCK(&verbosers);
900 AST_LIST_TRAVERSE(&verbosers, v, list)
901 v->verboser(buf->str);
902 AST_LIST_UNLOCK(&verbosers);
904 ast_log(LOG_VERBOSE, "%s", buf->str + 1);
907 int ast_register_verbose(void (*v)(const char *string))
909 struct verb *verb;
911 if (!(verb = ast_malloc(sizeof(*verb))))
912 return -1;
914 verb->verboser = v;
916 AST_LIST_LOCK(&verbosers);
917 AST_LIST_INSERT_HEAD(&verbosers, verb, list);
918 AST_LIST_UNLOCK(&verbosers);
920 return 0;
923 int ast_unregister_verbose(void (*v)(const char *string))
925 struct verb *cur;
927 AST_LIST_LOCK(&verbosers);
928 AST_LIST_TRAVERSE_SAFE_BEGIN(&verbosers, cur, list) {
929 if (cur->verboser == v) {
930 AST_LIST_REMOVE_CURRENT(&verbosers, list);
931 free(cur);
932 break;
935 AST_LIST_TRAVERSE_SAFE_END
936 AST_LIST_UNLOCK(&verbosers);
938 return cur ? 0 : -1;