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"
72 #include "asterisk/strings.h"
74 #if defined(__linux__) && !defined(__NR_gettid)
75 #include <asm/unistd.h>
78 #if defined(__linux__) && defined(__NR_gettid)
79 #define GETTID() syscall(__NR_gettid)
81 #define GETTID() getpid()
85 static char dateformat[256] = "%b %e %T"; /* Original Asterisk Format */
87 static char queue_log_name[256] = QUEUELOG;
89 static int filesize_reload_needed;
90 static int global_logmask = -1;
91 static int rotatetimestamp;
94 unsigned int queue_log:1;
95 unsigned int event_log:1;
96 } logfiles = { 1, 1 };
98 static char hostname[MAXHOSTNAMELEN];
107 int logmask; /* What to log to this channel */
108 int disabled; /* If this channel is disabled or not */
109 int facility; /* syslog facility */
110 enum logtypes type; /* Type of log channel */
111 FILE *fileptr; /* logfile logging file pointer */
112 char filename[256]; /* Filename */
113 AST_LIST_ENTRY(logchannel) list;
116 static AST_LIST_HEAD_STATIC(logchannels, logchannel);
124 enum logmsgtypes type;
129 const char *function;
130 AST_LIST_ENTRY(logmsg) list;
134 static AST_LIST_HEAD_STATIC(logmsgs, logmsg);
135 static pthread_t logthread = AST_PTHREADT_NULL;
136 static ast_cond_t logcond;
137 static int close_logger_thread;
139 static FILE *eventlog;
142 static char *levels[] = {
152 static int colors[] = {
162 AST_THREADSTORAGE(verbose_buf);
163 #define VERBOSE_BUF_INIT_SIZE 256
165 AST_THREADSTORAGE(log_buf);
166 #define LOG_BUF_INIT_SIZE 256
168 static int make_components(char *s, int lineno)
174 while ((w = strsep(&stringp, ","))) {
175 w = ast_skip_blanks(w);
176 if (!strcasecmp(w, "error"))
177 res |= (1 << __LOG_ERROR);
178 else if (!strcasecmp(w, "warning"))
179 res |= (1 << __LOG_WARNING);
180 else if (!strcasecmp(w, "notice"))
181 res |= (1 << __LOG_NOTICE);
182 else if (!strcasecmp(w, "event"))
183 res |= (1 << __LOG_EVENT);
184 else if (!strcasecmp(w, "debug"))
185 res |= (1 << __LOG_DEBUG);
186 else if (!strcasecmp(w, "verbose"))
187 res |= (1 << __LOG_VERBOSE);
188 else if (!strcasecmp(w, "dtmf"))
189 res |= (1 << __LOG_DTMF);
191 fprintf(stderr, "Logfile Warning: Unknown keyword '%s' at line %d of logger.conf\n", w, lineno);
198 static struct logchannel *make_logchannel(char *channel, char *components, int lineno)
200 struct logchannel *chan;
206 if (ast_strlen_zero(channel) || !(chan = ast_calloc(1, sizeof(*chan))))
209 if (!strcasecmp(channel, "console")) {
210 chan->type = LOGTYPE_CONSOLE;
211 } else if (!strncasecmp(channel, "syslog", 6)) {
214 * syslog.facility => level,level,level
216 facility = strchr(channel, '.');
217 if (!facility++ || !facility) {
223 * Walk through the list of facilitynames (defined in sys/syslog.h)
224 * to see if we can find the one we have been given
227 cptr = facilitynames;
228 while (cptr->c_name) {
229 if (!strcasecmp(facility, cptr->c_name)) {
230 chan->facility = cptr->c_val;
237 if (!strcasecmp(facility, "kern"))
238 chan->facility = LOG_KERN;
239 else if (!strcasecmp(facility, "USER"))
240 chan->facility = LOG_USER;
241 else if (!strcasecmp(facility, "MAIL"))
242 chan->facility = LOG_MAIL;
243 else if (!strcasecmp(facility, "DAEMON"))
244 chan->facility = LOG_DAEMON;
245 else if (!strcasecmp(facility, "AUTH"))
246 chan->facility = LOG_AUTH;
247 else if (!strcasecmp(facility, "SYSLOG"))
248 chan->facility = LOG_SYSLOG;
249 else if (!strcasecmp(facility, "LPR"))
250 chan->facility = LOG_LPR;
251 else if (!strcasecmp(facility, "NEWS"))
252 chan->facility = LOG_NEWS;
253 else if (!strcasecmp(facility, "UUCP"))
254 chan->facility = LOG_UUCP;
255 else if (!strcasecmp(facility, "CRON"))
256 chan->facility = LOG_CRON;
257 else if (!strcasecmp(facility, "LOCAL0"))
258 chan->facility = LOG_LOCAL0;
259 else if (!strcasecmp(facility, "LOCAL1"))
260 chan->facility = LOG_LOCAL1;
261 else if (!strcasecmp(facility, "LOCAL2"))
262 chan->facility = LOG_LOCAL2;
263 else if (!strcasecmp(facility, "LOCAL3"))
264 chan->facility = LOG_LOCAL3;
265 else if (!strcasecmp(facility, "LOCAL4"))
266 chan->facility = LOG_LOCAL4;
267 else if (!strcasecmp(facility, "LOCAL5"))
268 chan->facility = LOG_LOCAL5;
269 else if (!strcasecmp(facility, "LOCAL6"))
270 chan->facility = LOG_LOCAL6;
271 else if (!strcasecmp(facility, "LOCAL7"))
272 chan->facility = LOG_LOCAL7;
275 if (0 > chan->facility) {
276 fprintf(stderr, "Logger Warning: bad syslog facility in logger.conf\n");
281 chan->type = LOGTYPE_SYSLOG;
282 snprintf(chan->filename, sizeof(chan->filename), "%s", channel);
283 openlog("asterisk", LOG_PID, chan->facility);
285 if (channel[0] == '/') {
286 if (!ast_strlen_zero(hostname)) {
287 snprintf(chan->filename, sizeof(chan->filename) - 1,"%s.%s", channel, hostname);
289 ast_copy_string(chan->filename, channel, sizeof(chan->filename));
293 if (!ast_strlen_zero(hostname)) {
294 snprintf(chan->filename, sizeof(chan->filename), "%s/%s.%s", ast_config_AST_LOG_DIR, channel, hostname);
296 snprintf(chan->filename, sizeof(chan->filename), "%s/%s", ast_config_AST_LOG_DIR, channel);
298 chan->fileptr = fopen(chan->filename, "a");
299 if (!chan->fileptr) {
300 /* Can't log here, since we're called with a lock */
301 fprintf(stderr, "Logger Warning: Unable to open log file '%s': %s\n", chan->filename, strerror(errno));
303 chan->type = LOGTYPE_FILE;
305 chan->logmask = make_components(components, lineno);
309 static void init_logger_chain(void)
311 struct logchannel *chan;
312 struct ast_config *cfg;
313 struct ast_variable *var;
316 /* delete our list of log channels */
317 AST_LIST_LOCK(&logchannels);
318 while ((chan = AST_LIST_REMOVE_HEAD(&logchannels, list)))
320 AST_LIST_UNLOCK(&logchannels);
327 cfg = ast_config_load("logger.conf");
329 /* If no config file, we're fine, set default options. */
332 fprintf(stderr, "Unable to open logger.conf: %s; default settings will be used.\n", strerror(errno));
334 fprintf(stderr, "Errors detected in logger.conf: see above; default settings will be used.\n");
335 if (!(chan = ast_calloc(1, sizeof(*chan))))
337 chan->type = LOGTYPE_CONSOLE;
338 chan->logmask = 28; /*warning,notice,error */
339 AST_LIST_LOCK(&logchannels);
340 AST_LIST_INSERT_HEAD(&logchannels, chan, list);
341 AST_LIST_UNLOCK(&logchannels);
342 global_logmask |= chan->logmask;
346 if ((s = ast_variable_retrieve(cfg, "general", "appendhostname"))) {
348 if (gethostname(hostname, sizeof(hostname) - 1)) {
349 ast_copy_string(hostname, "unknown", sizeof(hostname));
350 ast_log(LOG_WARNING, "What box has no hostname???\n");
356 if ((s = ast_variable_retrieve(cfg, "general", "dateformat")))
357 ast_copy_string(dateformat, s, sizeof(dateformat));
359 ast_copy_string(dateformat, "%b %e %T", sizeof(dateformat));
360 if ((s = ast_variable_retrieve(cfg, "general", "queue_log")))
361 logfiles.queue_log = ast_true(s);
362 if ((s = ast_variable_retrieve(cfg, "general", "event_log")))
363 logfiles.event_log = ast_true(s);
364 if ((s = ast_variable_retrieve(cfg, "general", "queue_log_name")))
365 ast_copy_string(queue_log_name, s, sizeof(queue_log_name));
366 if ((s = ast_variable_retrieve(cfg, "general", "rotatetimestamp")))
367 rotatetimestamp = ast_true(s);
369 AST_LIST_LOCK(&logchannels);
370 var = ast_variable_browse(cfg, "logfiles");
371 for (; var; var = var->next) {
372 if (!(chan = make_logchannel(var->name, var->value, var->lineno)))
374 AST_LIST_INSERT_HEAD(&logchannels, chan, list);
375 global_logmask |= chan->logmask;
377 AST_LIST_UNLOCK(&logchannels);
379 ast_config_destroy(cfg);
382 void ast_queue_log(const char *queuename, const char *callid, const char *agent, const char *event, const char *fmt, ...)
385 AST_LIST_LOCK(&logchannels);
388 fprintf(qlog, "%ld|%s|%s|%s|%s|", (long)time(NULL), callid, queuename, agent, event);
389 vfprintf(qlog, fmt, ap);
394 AST_LIST_UNLOCK(&logchannels);
397 int reload_logger(int rotate)
399 char old[PATH_MAX] = "";
401 int event_rotate = rotate, queue_rotate = rotate;
402 struct logchannel *f;
406 AST_LIST_LOCK(&logchannels);
420 ast_mkdir(ast_config_AST_LOG_DIR, 0777);
422 AST_LIST_TRAVERSE(&logchannels, f, list) {
424 f->disabled = 0; /* Re-enable logging at reload */
425 manager_event(EVENT_FLAG_SYSTEM, "LogChannel", "Channel: %s\r\nEnabled: Yes\r\n", f->filename);
427 if (f->fileptr && (f->fileptr != stdout) && (f->fileptr != stderr)) {
428 fclose(f->fileptr); /* Close file */
431 ast_copy_string(old, f->filename, sizeof(old));
433 if (!rotatetimestamp) {
435 snprintf(new, sizeof(new), "%s.%d", f->filename, x);
436 myf = fopen(new, "r");
443 snprintf(new, sizeof(new), "%s.%ld", f->filename, (long)time(NULL));
447 fprintf(stderr, "Unable to rename file '%s' to '%s'\n", old, new);
452 filesize_reload_needed = 0;
456 if (logfiles.event_log) {
457 snprintf(old, sizeof(old), "%s/%s", ast_config_AST_LOG_DIR, EVENTLOG);
459 if (!rotatetimestamp) {
461 snprintf(new, sizeof(new), "%s/%s.%d", ast_config_AST_LOG_DIR, EVENTLOG,x);
462 myf = fopen(new, "r");
463 if (myf) /* File exists */
469 snprintf(new, sizeof(new), "%s/%s.%ld", ast_config_AST_LOG_DIR, EVENTLOG,(long)time(NULL));
473 ast_log(LOG_ERROR, "Unable to rename file '%s' to '%s'\n", old, new);
476 eventlog = fopen(old, "a");
478 ast_log(LOG_EVENT, "Restarted Asterisk Event Logger\n");
480 ast_verbose("Asterisk Event Logger restarted\n");
482 ast_log(LOG_ERROR, "Unable to create event log: %s\n", strerror(errno));
487 if (logfiles.queue_log) {
488 snprintf(old, sizeof(old), "%s/%s", ast_config_AST_LOG_DIR, queue_log_name);
490 if (!rotatetimestamp) {
492 snprintf(new, sizeof(new), "%s/%s.%d", ast_config_AST_LOG_DIR, queue_log_name, x);
493 myf = fopen(new, "r");
494 if (myf) /* File exists */
501 snprintf(new, sizeof(new), "%s/%s.%ld", ast_config_AST_LOG_DIR, queue_log_name,(long)time(NULL));
503 if (rename(old, new))
504 ast_log(LOG_ERROR, "Unable to rename file '%s' to '%s'\n", old, new);
507 qlog = fopen(old, "a");
509 ast_queue_log("NONE", "NONE", "NONE", "CONFIGRELOAD", "%s", "");
510 ast_log(LOG_EVENT, "Restarted Asterisk Queue Logger\n");
512 ast_verbose("Asterisk Queue Logger restarted\n");
514 ast_log(LOG_ERROR, "Unable to create queue log: %s\n", strerror(errno));
519 AST_LIST_UNLOCK(&logchannels);
524 static int handle_logger_reload(int fd, int argc, char *argv[])
526 if (reload_logger(0)) {
527 ast_cli(fd, "Failed to reload the logger\n");
528 return RESULT_FAILURE;
530 return RESULT_SUCCESS;
533 static int handle_logger_rotate(int fd, int argc, char *argv[])
535 if (reload_logger(1)) {
536 ast_cli(fd, "Failed to reload the logger and rotate log files\n");
537 return RESULT_FAILURE;
539 return RESULT_SUCCESS;
542 /*! \brief CLI command to show logging system configuration */
543 static int handle_logger_show_channels(int fd, int argc, char *argv[])
545 #define FORMATL "%-35.35s %-8.8s %-9.9s "
546 struct logchannel *chan;
548 ast_cli(fd,FORMATL, "Channel", "Type", "Status");
549 ast_cli(fd, "Configuration\n");
550 ast_cli(fd,FORMATL, "-------", "----", "------");
551 ast_cli(fd, "-------------\n");
552 AST_LIST_LOCK(&logchannels);
553 AST_LIST_TRAVERSE(&logchannels, chan, list) {
554 ast_cli(fd, FORMATL, chan->filename, chan->type==LOGTYPE_CONSOLE ? "Console" : (chan->type==LOGTYPE_SYSLOG ? "Syslog" : "File"),
555 chan->disabled ? "Disabled" : "Enabled");
557 if (chan->logmask & (1 << __LOG_DEBUG))
558 ast_cli(fd, "Debug ");
559 if (chan->logmask & (1 << __LOG_DTMF))
560 ast_cli(fd, "DTMF ");
561 if (chan->logmask & (1 << __LOG_VERBOSE))
562 ast_cli(fd, "Verbose ");
563 if (chan->logmask & (1 << __LOG_WARNING))
564 ast_cli(fd, "Warning ");
565 if (chan->logmask & (1 << __LOG_NOTICE))
566 ast_cli(fd, "Notice ");
567 if (chan->logmask & (1 << __LOG_ERROR))
568 ast_cli(fd, "Error ");
569 if (chan->logmask & (1 << __LOG_EVENT))
570 ast_cli(fd, "Event ");
573 AST_LIST_UNLOCK(&logchannels);
576 return RESULT_SUCCESS;
580 void (*verboser)(const char *string);
581 AST_LIST_ENTRY(verb) list;
584 static AST_LIST_HEAD_STATIC(verbosers, verb);
586 static char logger_reload_help[] =
587 "Usage: logger reload\n"
588 " Reloads the logger subsystem state. Use after restarting syslogd(8) if you are using syslog logging.\n";
590 static char logger_rotate_help[] =
591 "Usage: logger rotate\n"
592 " Rotates and Reopens the log files.\n";
594 static char logger_show_channels_help[] =
595 "Usage: logger show channels\n"
596 " List configured logger channels.\n";
598 static struct ast_cli_entry cli_logger[] = {
599 { { "logger", "show", "channels", NULL },
600 handle_logger_show_channels, "List configured log channels",
601 logger_show_channels_help },
603 { { "logger", "reload", NULL },
604 handle_logger_reload, "Reopens the log files",
605 logger_reload_help },
607 { { "logger", "rotate", NULL },
608 handle_logger_rotate, "Rotates and reopens the log files",
609 logger_rotate_help },
612 static int handle_SIGXFSZ(int sig)
614 /* Indicate need to reload */
615 filesize_reload_needed = 1;
619 static void ast_log_vsyslog(int level, const char *file, int line, const char *function, char *str)
623 if (level >= SYSLOG_NLEVELS) {
624 /* we are locked here, so cannot ast_log() */
625 fprintf(stderr, "ast_log_vsyslog called with bogus level: %d\n", level);
629 if (level == __LOG_VERBOSE) {
630 snprintf(buf, sizeof(buf), "VERBOSE[%ld]: %s", (long)GETTID(), str);
632 } else if (level == __LOG_DTMF) {
633 snprintf(buf, sizeof(buf), "DTMF[%ld]: %s", (long)GETTID(), str);
636 snprintf(buf, sizeof(buf), "%s[%ld]: %s:%d in %s: %s",
637 levels[level], (long)GETTID(), file, line, function, str);
640 term_strip(buf, buf, strlen(buf) + 1);
641 syslog(syslog_level_map[level], "%s", buf);
644 /* Print a normal log message to the channels */
645 static void logger_print_normal(struct logmsg *logmsg)
647 struct logchannel *chan = NULL;
650 AST_LIST_LOCK(&logchannels);
652 if (logfiles.event_log && logmsg->level == __LOG_EVENT) {
653 fprintf(eventlog, "%s asterisk[%ld]: %s", logmsg->date, (long)getpid(), logmsg->str);
655 AST_LIST_UNLOCK(&logchannels);
659 if (!AST_LIST_EMPTY(&logchannels)) {
660 AST_LIST_TRAVERSE(&logchannels, chan, list) {
661 /* If the channel is disabled, then move on to the next one */
664 /* Check syslog channels */
665 if (chan->type == LOGTYPE_SYSLOG && (chan->logmask & (1 << logmsg->level))) {
666 ast_log_vsyslog(logmsg->level, logmsg->file, logmsg->line, logmsg->function, logmsg->str);
667 /* Console channels */
668 } else if (chan->type == LOGTYPE_CONSOLE && (chan->logmask & (1 << logmsg->level))) {
670 char tmp1[80], tmp2[80], tmp3[80], tmp4[80];
672 /* If the level is verbose, then skip it */
673 if (logmsg->level == __LOG_VERBOSE)
676 /* Turn the numerical line number into a string */
677 snprintf(linestr, sizeof(linestr), "%d", logmsg->line);
678 /* Build string to print out */
679 snprintf(buf, sizeof(buf), "[%s] %s[%ld]: %s:%s %s: %s",
681 term_color(tmp1, levels[logmsg->level], colors[logmsg->level], 0, sizeof(tmp1)),
683 term_color(tmp2, logmsg->file, COLOR_BRWHITE, 0, sizeof(tmp2)),
684 term_color(tmp3, linestr, COLOR_BRWHITE, 0, sizeof(tmp3)),
685 term_color(tmp4, logmsg->function, COLOR_BRWHITE, 0, sizeof(tmp4)),
688 ast_console_puts_mutable(buf);
690 } else if (chan->type == LOGTYPE_FILE && (chan->logmask & (1 << logmsg->level))) {
693 /* If no file pointer exists, skip it */
697 /* Print out to the file */
698 res = fprintf(chan->fileptr, "[%s] %s[%ld] %s: %s",
699 logmsg->date, levels[logmsg->level], (long)GETTID(), logmsg->file, logmsg->str);
700 if (res <= 0 && !ast_strlen_zero(logmsg->str)) {
701 fprintf(stderr, "**** Asterisk Logging Error: ***********\n");
702 if (errno == ENOMEM || errno == ENOSPC)
703 fprintf(stderr, "Asterisk logging error: Out of disk space, can't log to log file %s\n", chan->filename);
705 fprintf(stderr, "Logger Warning: Unable to write to log file '%s': %s (disabled)\n", chan->filename, strerror(errno));
706 manager_event(EVENT_FLAG_SYSTEM, "LogChannel", "Channel: %s\r\nEnabled: No\r\nReason: %d - %s\r\n", chan->filename, errno, strerror(errno));
708 } else if (res > 0) {
709 fflush(chan->fileptr);
713 } else if (logmsg->level != __LOG_VERBOSE) {
714 fputs(logmsg->str, stdout);
717 AST_LIST_UNLOCK(&logchannels);
719 /* If we need to reload because of the file size, then do so */
720 if (filesize_reload_needed) {
722 ast_log(LOG_EVENT, "Rotated Logs Per SIGXFSZ (Exceeded file size limit)\n");
724 ast_verbose("Rotated Logs Per SIGXFSZ (Exceeded file size limit)\n");
730 /* Print a verbose message to the verbosers */
731 static void logger_print_verbose(struct logmsg *logmsg)
733 struct verb *v = NULL;
735 /* Iterate through the list of verbosers and pass them the log message string */
736 AST_LIST_LOCK(&verbosers);
737 AST_LIST_TRAVERSE(&verbosers, v, list)
738 v->verboser(logmsg->str);
739 AST_LIST_UNLOCK(&verbosers);
744 /* Actual logging thread */
745 static void *logger_thread(void *data)
747 struct logmsg *next = NULL, *msg = NULL;
750 /* We lock the message list, and see if any message exists... if not we wait on the condition to be signalled */
751 AST_LIST_LOCK(&logmsgs);
752 if (AST_LIST_EMPTY(&logmsgs))
753 ast_cond_wait(&logcond, &logmsgs.lock);
754 next = AST_LIST_FIRST(&logmsgs);
755 AST_LIST_HEAD_INIT_NOLOCK(&logmsgs);
756 AST_LIST_UNLOCK(&logmsgs);
758 /* If we should stop, then stop */
759 if (close_logger_thread)
762 /* Otherwise go through and process each message in the order added */
763 while ((msg = next)) {
764 /* Get the next entry now so that we can free our current structure later */
765 next = AST_LIST_NEXT(msg, list);
767 /* Depending on the type, send it to the proper function */
768 if (msg->type == LOGMSG_NORMAL)
769 logger_print_normal(msg);
770 else if (msg->type == LOGMSG_VERBOSE)
771 logger_print_verbose(msg);
773 /* Free the data since we are done */
781 int init_logger(void)
786 /* auto rotate if sig SIGXFSZ comes a-knockin */
787 (void) signal(SIGXFSZ,(void *) handle_SIGXFSZ);
789 /* start logger thread */
790 ast_cond_init(&logcond, NULL);
791 if (ast_pthread_create(&logthread, NULL, logger_thread, NULL) < 0) {
792 ast_cond_destroy(&logcond);
796 /* register the logger cli commands */
797 ast_cli_register_multiple(cli_logger, sizeof(cli_logger) / sizeof(struct ast_cli_entry));
799 ast_mkdir(ast_config_AST_LOG_DIR, 0777);
801 /* create log channels */
804 /* create the eventlog */
805 if (logfiles.event_log) {
806 snprintf(tmp, sizeof(tmp), "%s/%s", ast_config_AST_LOG_DIR, EVENTLOG);
807 eventlog = fopen(tmp, "a");
809 ast_log(LOG_EVENT, "Started Asterisk Event Logger\n");
811 ast_verbose("Asterisk Event Logger Started %s\n", tmp);
813 ast_log(LOG_ERROR, "Unable to create event log: %s\n", strerror(errno));
818 if (logfiles.queue_log) {
819 snprintf(tmp, sizeof(tmp), "%s/%s", ast_config_AST_LOG_DIR, queue_log_name);
820 qlog = fopen(tmp, "a");
821 ast_queue_log("NONE", "NONE", "NONE", "QUEUESTART", "%s", "");
826 void close_logger(void)
828 struct logchannel *f = NULL;
830 /* Stop logger thread */
831 AST_LIST_LOCK(&logmsgs);
832 close_logger_thread = 1;
833 ast_cond_signal(&logcond);
834 AST_LIST_UNLOCK(&logmsgs);
836 AST_LIST_LOCK(&logchannels);
848 AST_LIST_TRAVERSE(&logchannels, f, list) {
849 if (f->fileptr && (f->fileptr != stdout) && (f->fileptr != stderr)) {
855 closelog(); /* syslog */
857 AST_LIST_UNLOCK(&logchannels);
863 * \brief send log messages to syslog and/or the console
865 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
867 struct logmsg *logmsg = NULL;
868 struct ast_str *buf = NULL;
874 if (!(buf = ast_str_thread_get(&log_buf, LOG_BUF_INIT_SIZE)))
877 if (AST_LIST_EMPTY(&logchannels)) {
879 * we don't have the logger chain configured yet,
880 * so just log to stdout
882 if (level != __LOG_VERBOSE) {
885 res = ast_str_set_va(&buf, BUFSIZ, fmt, ap); /* XXX BUFSIZ ? */
887 if (res != AST_DYNSTR_BUILD_FAILED) {
888 term_filter_escapes(buf->str);
889 fputs(buf->str, stdout);
895 /* don't display LOG_DEBUG messages unless option_verbose _or_ option_debug
896 are non-zero; LOG_DEBUG messages can still be displayed if option_debug
897 is zero, if option_verbose is non-zero (this allows for 'level zero'
898 LOG_DEBUG messages to be displayed, if the logmask on any channel
901 if (!option_verbose && !option_debug && (level == __LOG_DEBUG))
904 /* Ignore anything that never gets logged anywhere */
905 if (!(global_logmask & (1 << level)))
908 /* Ignore anything other than the currently debugged file if there is one */
909 if ((level == __LOG_DEBUG) && !ast_strlen_zero(debug_filename) && strcasecmp(debug_filename, file))
914 res = ast_str_set_va(&buf, BUFSIZ, fmt, ap);
917 /* If the build failed, then abort and free this structure */
918 if (res == AST_DYNSTR_BUILD_FAILED)
921 /* Create a new logging message */
922 if (!(logmsg = ast_calloc(1, sizeof(*logmsg) + res + 1)))
925 /* Copy string over */
926 strcpy(logmsg->str, buf->str);
928 /* Set type to be normal */
929 logmsg->type = LOGMSG_NORMAL;
931 /* Create our date/time */
933 ast_localtime(&t, &tm, NULL);
934 strftime(logmsg->date, sizeof(logmsg->date), dateformat, &tm);
937 logmsg->level = level;
940 logmsg->function = function;
942 /* If the logger thread is active, append it to the tail end of the list - otherwise skip that step */
943 if (logthread != AST_PTHREADT_NULL) {
944 AST_LIST_LOCK(&logmsgs);
945 AST_LIST_INSERT_TAIL(&logmsgs, logmsg, list);
946 ast_cond_signal(&logcond);
947 AST_LIST_UNLOCK(&logmsgs);
949 logger_print_normal(logmsg);
956 void ast_backtrace(void)
964 if ((addresses = ast_calloc(MAX_BACKTRACE_FRAMES, sizeof(*addresses)))) {
965 count = backtrace(addresses, MAX_BACKTRACE_FRAMES);
966 if ((strings = backtrace_symbols(addresses, count))) {
967 ast_debug(1, "Got %d backtrace record%c\n", count, count != 1 ? 's' : ' ');
968 for (i=0; i < count ; i++) {
969 ast_debug(1, "#%d: [%08X] %s\n", i, (unsigned int)addresses[i], strings[i]);
973 ast_debug(1, "Could not allocate memory for backtrace\n");
978 ast_log(LOG_WARNING, "Must run configure with '--enable-dev-mode' for stack backtraces.\n");
980 #else /* ndef Linux */
981 ast_log(LOG_WARNING, "Inline stack backtraces are only available on the Linux platform.\n");
985 void ast_verbose(const char *fmt, ...)
987 struct logmsg *logmsg = NULL;
988 struct ast_str *buf = NULL;
992 if (!(buf = ast_str_thread_get(&verbose_buf, VERBOSE_BUF_INIT_SIZE)))
997 res = ast_str_set_va(&buf, 0, fmt, ap);
1000 /* If the build failed then we can drop this allocated message */
1001 if (res == AST_DYNSTR_BUILD_FAILED)
1004 if (!(logmsg = ast_calloc(1, sizeof(*logmsg) + res + 1)))
1007 strcpy(logmsg->str, buf->str);
1009 ast_log(LOG_VERBOSE, logmsg->str);
1012 logmsg->type = LOGMSG_VERBOSE;
1014 if (ast_opt_timestamp) {
1021 ast_localtime(&t, &tm, NULL);
1022 strftime(date, sizeof(date), dateformat, &tm);
1023 datefmt = alloca(strlen(date) + 3 + strlen(fmt) + 1);
1024 sprintf(datefmt, "[%s] %s", date, fmt);
1028 /* Add to the list and poke the thread if possible */
1029 if (logthread != AST_PTHREADT_NULL) {
1030 AST_LIST_LOCK(&logmsgs);
1031 AST_LIST_INSERT_TAIL(&logmsgs, logmsg, list);
1032 ast_cond_signal(&logcond);
1033 AST_LIST_UNLOCK(&logmsgs);
1035 logger_print_verbose(logmsg);
1040 int ast_register_verbose(void (*v)(const char *string))
1044 if (!(verb = ast_malloc(sizeof(*verb))))
1049 AST_LIST_LOCK(&verbosers);
1050 AST_LIST_INSERT_HEAD(&verbosers, verb, list);
1051 AST_LIST_UNLOCK(&verbosers);
1056 int ast_unregister_verbose(void (*v)(const char *string))
1060 AST_LIST_LOCK(&verbosers);
1061 AST_LIST_TRAVERSE_SAFE_BEGIN(&verbosers, cur, list) {
1062 if (cur->verboser == v) {
1063 AST_LIST_REMOVE_CURRENT(&verbosers, list);
1068 AST_LIST_TRAVERSE_SAFE_END
1069 AST_LIST_UNLOCK(&verbosers);
1071 return cur ? 0 : -1;