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.
21 * \brief Asterisk Logger
25 * \author Mark Spencer <markster@digium.com>
30 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
41 #if ((defined(AST_DEVMODE)) && (defined(linux)))
43 #define MAX_BACKTRACE_FRAMES 20
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 */
50 static int syslog_level_map
[] = {
52 LOG_INFO
, /* arbitrary equivalent of LOG_EVENT */
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>
77 #if defined(__linux__) && defined(__NR_gettid)
78 #define GETTID() syscall(__NR_gettid)
80 #define GETTID() getpid()
84 static char dateformat
[256] = "%b %e %T"; /* Original Asterisk Format */
86 static int filesize_reload_needed
;
87 static int global_logmask
= -1;
90 unsigned int queue_log
:1;
91 unsigned int event_log
:1;
92 } logfiles
= { 1, 1 };
94 static char hostname
[MAXHOSTNAMELEN
];
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
;
117 static char *levels
[] = {
127 static int colors
[] = {
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
)
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
);
166 fprintf(stderr
, "Logfile Warning: Unknown keyword '%s' at line %d of logger.conf\n", w
, lineno
);
173 static struct logchannel
*make_logchannel(char *channel
, char *components
, int lineno
)
175 struct logchannel
*chan
;
181 if (ast_strlen_zero(channel
) || !(chan
= ast_calloc(1, sizeof(*chan
))))
184 if (!strcasecmp(channel
, "console")) {
185 chan
->type
= LOGTYPE_CONSOLE
;
186 } else if (!strncasecmp(channel
, "syslog", 6)) {
189 * syslog.facility => level,level,level
191 facility
= strchr(channel
, '.');
192 if(!facility
++ || !facility
) {
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
202 cptr
= facilitynames
;
203 while (cptr
->c_name
) {
204 if (!strcasecmp(facility
, cptr
->c_name
)) {
205 chan
->facility
= cptr
->c_val
;
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
;
250 if (0 > chan
->facility
) {
251 fprintf(stderr
, "Logger Warning: bad syslog facility in logger.conf\n");
256 chan
->type
= LOGTYPE_SYSLOG
;
257 snprintf(chan
->filename
, sizeof(chan
->filename
), "%s", channel
);
258 openlog("asterisk", LOG_PID
, chan
->facility
);
260 if (channel
[0] == '/') {
261 if(!ast_strlen_zero(hostname
)) {
262 snprintf(chan
->filename
, sizeof(chan
->filename
) - 1,"%s.%s", channel
, hostname
);
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
);
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
);
284 static void init_logger_chain(void)
286 struct logchannel
*chan
;
287 struct ast_config
*cfg
;
288 struct ast_variable
*var
;
291 /* delete our list of log channels */
292 AST_LIST_LOCK(&logchannels
);
293 while ((chan
= AST_LIST_REMOVE_HEAD(&logchannels
, list
)))
295 AST_LIST_UNLOCK(&logchannels
);
302 cfg
= ast_config_load("logger.conf");
304 /* If no config file, we're fine, set default options. */
307 fprintf(stderr
, "Unable to open logger.conf: %s; default settings will be used.\n", strerror(errno
));
309 fprintf(stderr
, "Errors detected in logger.conf: see above; default settings will be used.\n");
310 if (!(chan
= ast_calloc(1, sizeof(*chan
))))
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
;
321 if ((s
= ast_variable_retrieve(cfg
, "general", "appendhostname"))) {
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");
331 if ((s
= ast_variable_retrieve(cfg
, "general", "dateformat")))
332 ast_copy_string(dateformat
, s
, sizeof(dateformat
));
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
)))
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
, ...)
356 AST_LIST_LOCK(&logchannels
);
359 fprintf(qlog
, "%ld|%s|%s|%s|%s|", (long)time(NULL
), callid
, queuename
, agent
, event
);
360 vfprintf(qlog
, fmt
, ap
);
365 AST_LIST_UNLOCK(&logchannels
);
368 int reload_logger(int rotate
)
370 char old
[PATH_MAX
] = "";
372 int event_rotate
= rotate
, queue_rotate
= rotate
;
373 struct logchannel
*f
;
377 AST_LIST_LOCK(&logchannels
);
391 mkdir((char *)ast_config_AST_LOG_DIR
, 0755);
393 AST_LIST_TRAVERSE(&logchannels
, f
, list
) {
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 */
402 ast_copy_string(old
, f
->filename
, sizeof(old
));
405 snprintf(new, sizeof(new), "%s.%d", f
->filename
, x
);
406 myf
= fopen((char *)new, "r");
415 fprintf(stderr
, "Unable to rename file '%s' to '%s'\n", old
, new);
420 filesize_reload_needed
= 0;
424 if (logfiles
.event_log
) {
425 snprintf(old
, sizeof(old
), "%s/%s", (char *)ast_config_AST_LOG_DIR
, EVENTLOG
);
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 */
438 ast_log(LOG_ERROR
, "Unable to rename file '%s' to '%s'\n", old
, new);
441 eventlog
= fopen(old
, "a");
443 ast_log(LOG_EVENT
, "Restarted Asterisk Event Logger\n");
445 ast_verbose("Asterisk Event Logger restarted\n");
447 ast_log(LOG_ERROR
, "Unable to create event log: %s\n", strerror(errno
));
452 if (logfiles
.queue_log
) {
453 snprintf(old
, sizeof(old
), "%s/%s", (char *)ast_config_AST_LOG_DIR
, QUEUELOG
);
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 */
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");
471 ast_queue_log("NONE", "NONE", "NONE", "CONFIGRELOAD", "%s", "");
472 ast_log(LOG_EVENT
, "Restarted Asterisk Queue Logger\n");
474 ast_verbose("Asterisk Queue Logger restarted\n");
476 ast_log(LOG_ERROR
, "Unable to create queue log: %s\n", strerror(errno
));
481 AST_LIST_UNLOCK(&logchannels
);
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)
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");
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");
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 ");
543 AST_LIST_UNLOCK(&logchannels
);
546 return RESULT_SUCCESS
;
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;
589 int init_logger(void)
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 */
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");
611 ast_log(LOG_EVENT
, "Started Asterisk Event Logger\n");
613 ast_verbose("Asterisk Event Logger Started %s\n",(char *)tmp
);
615 ast_log(LOG_ERROR
, "Unable to create event log: %s\n", strerror(errno
));
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", "");
628 void close_logger(void)
630 struct logchannel
*f
;
632 AST_LIST_LOCK(&logchannels
);
644 AST_LIST_TRAVERSE(&logchannels
, f
, list
) {
645 if (f
->fileptr
&& (f
->fileptr
!= stdout
) && (f
->fileptr
!= stderr
)) {
651 closelog(); /* syslog */
653 AST_LIST_UNLOCK(&logchannels
);
658 static void ast_log_vsyslog(int level
, const char *file
, int line
, const char *function
, const char *fmt
, va_list args
)
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
);
668 if (level
== __LOG_VERBOSE
) {
669 snprintf(buf
, sizeof(buf
), "VERBOSE[%ld]: ", (long)GETTID());
671 } else if (level
== __LOG_DTMF
) {
672 snprintf(buf
, sizeof(buf
), "DTMF[%ld]: ", (long)GETTID());
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
;
697 if (!(buf
= ast_dynamic_str_thread_get(&log_buf
, LOG_BUF_INIT_SIZE
)))
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
) {
709 res
= ast_dynamic_str_thread_set_va(&buf
, BUFSIZ
, &log_buf
, fmt
, ap
);
711 if (res
!= AST_DYNSTR_BUILD_FAILED
) {
712 term_filter_escapes(buf
->str
);
713 fputs(buf
->str
, stdout
);
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
725 if (!option_verbose
&& !option_debug
&& (level
== __LOG_DEBUG
))
728 /* Ignore anything that never gets logged anywhere */
729 if (!(global_logmask
& (1 << level
)))
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
))
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
) {
745 fprintf(eventlog
, "%s asterisk[%ld]: ", date
, (long)getpid());
746 vfprintf(eventlog
, fmt
, ap
);
750 AST_LIST_UNLOCK(&logchannels
);
754 AST_LIST_TRAVERSE(&logchannels
, chan
, list
) {
757 /* Check syslog channels */
758 if (chan
->type
== LOGTYPE_SYSLOG
&& (chan
->logmask
& (1 << level
))) {
760 ast_log_vsyslog(level
, file
, line
, function
, fmt
, ap
);
762 /* Console channels */
763 } else if ((chan
->logmask
& (1 << level
)) && (chan
->type
== LOGTYPE_CONSOLE
)) {
765 char tmp1
[80], tmp2
[80], tmp3
[80], tmp4
[80];
767 if (level
!= __LOG_VERBOSE
) {
769 sprintf(linestr
, "%d", line
);
770 ast_dynamic_str_thread_set(&buf
, BUFSIZ
, &log_buf
,
771 "[%s] %s[%ld]: %s:%s %s: ",
773 term_color(tmp1
, levels
[level
], colors
[level
], 0, sizeof(tmp1
)),
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
);
783 res
= ast_dynamic_str_thread_set_va(&buf
, BUFSIZ
, &log_buf
, fmt
, ap
);
785 if (res
!= AST_DYNSTR_BUILD_FAILED
)
786 ast_console_puts_mutable(buf
->str
);
789 } else if ((chan
->logmask
& (1 << level
)) && (chan
->fileptr
)) {
791 ast_dynamic_str_thread_set(&buf
, BUFSIZ
, &log_buf
,
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
);
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
));
805 /* No error message, continue printing */
807 res
= ast_dynamic_str_thread_set_va(&buf
, BUFSIZ
, &log_buf
, fmt
, 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
) {
822 ast_log(LOG_EVENT
,"Rotated Logs Per SIGXFSZ (Exceeded file size limit)\n");
824 ast_verbose("Rotated Logs Per SIGXFSZ (Exceeded file size limit)\n");
828 void ast_backtrace(void)
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
++) {
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
]);
849 ast_log(LOG_DEBUG
, "Could not allocate memory for backtrace\n");
854 ast_log(LOG_WARNING
, "Must run configure with '--enable-dev-mode' for stack backtraces.\n");
856 #else /* ndef linux */
857 ast_log(LOG_WARNING
, "Inline stack backtraces are only available on the Linux platform.\n");
861 void ast_verbose(const char *fmt
, ...)
864 struct ast_dynamic_str
*buf
;
868 if (ast_opt_timestamp
) {
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
, "[%s] %s", date
, fmt
);
882 if (!(buf
= ast_dynamic_str_thread_get(&verbose_buf
, VERBOSE_BUF_INIT_SIZE
)))
886 res
= ast_dynamic_str_thread_set_va(&buf
, 0, &verbose_buf
, fmt
, ap
);
889 if (res
== AST_DYNSTR_BUILD_FAILED
)
892 /* filter out possibly hazardous escape sequences */
893 term_filter_escapes(buf
->str
);
895 AST_LIST_LOCK(&verbosers
);
896 AST_LIST_TRAVERSE(&verbosers
, v
, list
)
897 v
->verboser(buf
->str
);
898 AST_LIST_UNLOCK(&verbosers
);
900 ast_log(LOG_VERBOSE
, "%s", buf
->str
);
903 int ast_register_verbose(void (*v
)(const char *string
))
907 if (!(verb
= ast_malloc(sizeof(*verb
))))
912 AST_LIST_LOCK(&verbosers
);
913 AST_LIST_INSERT_HEAD(&verbosers
, verb
, list
);
914 AST_LIST_UNLOCK(&verbosers
);
919 int ast_unregister_verbose(void (*v
)(const char *string
))
923 AST_LIST_LOCK(&verbosers
);
924 AST_LIST_TRAVERSE_SAFE_BEGIN(&verbosers
, cur
, list
) {
925 if (cur
->verboser
== v
) {
926 AST_LIST_REMOVE_CURRENT(&verbosers
, list
);
931 AST_LIST_TRAVERSE_SAFE_END
932 AST_LIST_UNLOCK(&verbosers
);