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>
29 <support_level>core</support_level>
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36 /* When we include logger.h again it will trample on some stuff in syslog.h, but
37 * nothing we care about in here. */
40 #include "asterisk/_private.h"
41 #include "asterisk/paths.h" /* use ast_config_AST_LOG_DIR */
42 #include "asterisk/logger.h"
43 #include "asterisk/lock.h"
44 #include "asterisk/channel.h"
45 #include "asterisk/config.h"
46 #include "asterisk/term.h"
47 #include "asterisk/cli.h"
48 #include "asterisk/utils.h"
49 #include "asterisk/manager.h"
50 #include "asterisk/astobj2.h"
51 #include "asterisk/threadstorage.h"
52 #include "asterisk/strings.h"
53 #include "asterisk/pbx.h"
54 #include "asterisk/app.h"
55 #include "asterisk/syslog.h"
56 #include "asterisk/buildinfo.h"
57 #include "asterisk/ast_version.h"
65 #define MAX_BACKTRACE_FRAMES 20
66 # if defined(HAVE_DLADDR) && defined(HAVE_BFD) && defined(BETTER_BACKTRACES)
75 static char dateformat[256] = "%b %e %T"; /* Original Asterisk Format */
77 static char queue_log_name[256] = QUEUELOG;
78 static char exec_after_rotate[256] = "";
80 static int filesize_reload_needed;
81 static unsigned int global_logmask = 0xFFFF;
82 static int queuelog_init;
83 static int logger_initialized;
84 static volatile int next_unique_callid; /* Used to assign unique call_ids to calls */
85 static int display_callids;
86 static void unique_callid_cleanup(void *data);
89 int call_identifier; /* Numerical value of the call displayed in the logs */
92 AST_THREADSTORAGE_CUSTOM(unique_callid, NULL, unique_callid_cleanup);
94 static enum rotatestrategy {
95 SEQUENTIAL = 1 << 0, /* Original method - create a new file, in order */
96 ROTATE = 1 << 1, /* Rotate all files, such that the oldest file has the highest suffix */
97 TIMESTAMP = 1 << 2, /* Append the epoch timestamp onto the end of the archived file */
98 } rotatestrategy = SEQUENTIAL;
101 unsigned int queue_log:1;
102 unsigned int queue_log_to_file:1;
103 unsigned int queue_adaptive_realtime:1;
106 static char hostname[MAXHOSTNAMELEN];
115 /*! What to log to this channel */
116 unsigned int logmask;
117 /*! If this channel is disabled or not */
119 /*! syslog facility */
121 /*! Verbosity level */
123 /*! Type of log channel */
125 /*! logfile logging file pointer */
128 char filename[PATH_MAX];
129 /*! field for linking to list */
130 AST_LIST_ENTRY(logchannel) list;
131 /*! Line number from configuration file */
133 /*! Components (levels) from last config load */
137 static AST_RWLIST_HEAD_STATIC(logchannels, logchannel);
145 enum logmsgtypes type;
149 struct ast_callid *callid;
150 AST_DECLARE_STRING_FIELDS(
151 AST_STRING_FIELD(date);
152 AST_STRING_FIELD(file);
153 AST_STRING_FIELD(function);
154 AST_STRING_FIELD(message);
155 AST_STRING_FIELD(level_name);
157 AST_LIST_ENTRY(logmsg) list;
160 static void logmsg_free(struct logmsg *msg)
163 ast_callid_unref(msg->callid);
168 static AST_LIST_HEAD_STATIC(logmsgs, logmsg);
169 static pthread_t logthread = AST_PTHREADT_NULL;
170 static ast_cond_t logcond;
171 static int close_logger_thread = 0;
175 /*! \brief Logging channels used in the Asterisk logging system
177 * The first 16 levels are reserved for system usage, and the remaining
178 * levels are reserved for usage by dynamic levels registered via
179 * ast_logger_register_level.
182 /* Modifications to this array are protected by the rwlock in the
186 static char *levels[NUMLOGLEVELS] = {
188 "---EVENT---", /* no longer used */
196 /*! \brief Colors used in the console for logging */
197 static const int colors[NUMLOGLEVELS] = {
199 COLOR_BRBLUE, /* no longer used */
232 AST_THREADSTORAGE(verbose_buf);
233 #define VERBOSE_BUF_INIT_SIZE 256
235 AST_THREADSTORAGE(log_buf);
236 #define LOG_BUF_INIT_SIZE 256
238 static void logger_queue_init(void);
240 static unsigned int make_components(const char *s, int lineno, int *verbosity)
243 unsigned int res = 0;
244 char *stringp = ast_strdupa(s);
249 while ((w = strsep(&stringp, ","))) {
250 w = ast_skip_blanks(w);
252 if (!strcmp(w, "*")) {
255 } else if (!strncasecmp(w, "verbose(", 8) && sscanf(w + 8, "%d)", verbosity) == 1) {
256 res |= (1 << __LOG_VERBOSE);
258 } else for (x = 0; x < ARRAY_LEN(levels); x++) {
259 if (levels[x] && !strcasecmp(w, levels[x])) {
269 static struct logchannel *make_logchannel(const char *channel, const char *components, int lineno)
271 struct logchannel *chan;
274 struct timeval now = ast_tvnow();
275 char datestring[256];
277 if (ast_strlen_zero(channel) || !(chan = ast_calloc(1, sizeof(*chan) + strlen(components) + 1)))
280 strcpy(chan->components, components);
281 chan->lineno = lineno;
283 if (!strcasecmp(channel, "console")) {
284 chan->type = LOGTYPE_CONSOLE;
285 } else if (!strncasecmp(channel, "syslog", 6)) {
288 * syslog.facility => level,level,level
290 facility = strchr(channel, '.');
291 if (!facility++ || !facility) {
295 chan->facility = ast_syslog_facility(facility);
297 if (chan->facility < 0) {
298 fprintf(stderr, "Logger Warning: bad syslog facility in logger.conf\n");
303 chan->type = LOGTYPE_SYSLOG;
304 ast_copy_string(chan->filename, channel, sizeof(chan->filename));
305 openlog("asterisk", LOG_PID, chan->facility);
307 if (!ast_strlen_zero(hostname)) {
308 snprintf(chan->filename, sizeof(chan->filename), "%s/%s.%s",
309 channel[0] != '/' ? ast_config_AST_LOG_DIR : "", channel, hostname);
311 snprintf(chan->filename, sizeof(chan->filename), "%s/%s",
312 channel[0] != '/' ? ast_config_AST_LOG_DIR : "", channel);
314 if (!(chan->fileptr = fopen(chan->filename, "a"))) {
315 /* Can't do real logging here since we're called with a lock
316 * so log to any attached consoles */
317 ast_console_puts_mutable("ERROR: Unable to open log file '", __LOG_ERROR);
318 ast_console_puts_mutable(chan->filename, __LOG_ERROR);
319 ast_console_puts_mutable("': ", __LOG_ERROR);
320 ast_console_puts_mutable(strerror(errno), __LOG_ERROR);
321 ast_console_puts_mutable("'\n", __LOG_ERROR);
325 /* Create our date/time */
326 ast_localtime(&now, &tm, NULL);
327 ast_strftime(datestring, sizeof(datestring), dateformat, &tm);
329 fprintf(chan->fileptr, "[%s] Asterisk %s built by %s @ %s on a %s running %s on %s\n",
330 datestring, ast_get_version(), ast_build_user, ast_build_hostname,
331 ast_build_machine, ast_build_os, ast_build_date);
332 fflush(chan->fileptr);
334 chan->type = LOGTYPE_FILE;
336 chan->logmask = make_components(chan->components, lineno, &chan->verbosity);
341 static void init_logger_chain(int locked, const char *altconf)
343 struct logchannel *chan;
344 struct ast_config *cfg;
345 struct ast_variable *var;
347 struct ast_flags config_flags = { 0 };
351 if (!(cfg = ast_config_load2(S_OR(altconf, "logger.conf"), "logger", config_flags)) || cfg == CONFIG_STATUS_FILEINVALID) {
355 /* delete our list of log channels */
357 AST_RWLIST_WRLOCK(&logchannels);
359 while ((chan = AST_RWLIST_REMOVE_HEAD(&logchannels, list))) {
364 AST_RWLIST_UNLOCK(&logchannels);
371 /* If no config file, we're fine, set default options. */
374 fprintf(stderr, "Unable to open logger.conf: %s; default settings will be used.\n", strerror(errno));
376 fprintf(stderr, "Errors detected in logger.conf: see above; default settings will be used.\n");
378 if (!(chan = ast_calloc(1, sizeof(*chan)))) {
381 chan->type = LOGTYPE_CONSOLE;
382 chan->logmask = __LOG_WARNING | __LOG_NOTICE | __LOG_ERROR;
384 AST_RWLIST_WRLOCK(&logchannels);
386 AST_RWLIST_INSERT_HEAD(&logchannels, chan, list);
387 global_logmask |= chan->logmask;
389 AST_RWLIST_UNLOCK(&logchannels);
394 if ((s = ast_variable_retrieve(cfg, "general", "appendhostname"))) {
396 if (gethostname(hostname, sizeof(hostname) - 1)) {
397 ast_copy_string(hostname, "unknown", sizeof(hostname));
398 fprintf(stderr, "What box has no hostname???\n");
404 if ((s = ast_variable_retrieve(cfg, "general", "display_callids"))) {
405 display_callids = ast_true(s);
407 if ((s = ast_variable_retrieve(cfg, "general", "dateformat")))
408 ast_copy_string(dateformat, s, sizeof(dateformat));
410 ast_copy_string(dateformat, "%b %e %T", sizeof(dateformat));
411 if ((s = ast_variable_retrieve(cfg, "general", "queue_log"))) {
412 logfiles.queue_log = ast_true(s);
414 if ((s = ast_variable_retrieve(cfg, "general", "queue_log_to_file"))) {
415 logfiles.queue_log_to_file = ast_true(s);
417 if ((s = ast_variable_retrieve(cfg, "general", "queue_log_name"))) {
418 ast_copy_string(queue_log_name, s, sizeof(queue_log_name));
420 if ((s = ast_variable_retrieve(cfg, "general", "exec_after_rotate"))) {
421 ast_copy_string(exec_after_rotate, s, sizeof(exec_after_rotate));
423 if ((s = ast_variable_retrieve(cfg, "general", "rotatestrategy"))) {
424 if (strcasecmp(s, "timestamp") == 0) {
425 rotatestrategy = TIMESTAMP;
426 } else if (strcasecmp(s, "rotate") == 0) {
427 rotatestrategy = ROTATE;
428 } else if (strcasecmp(s, "sequential") == 0) {
429 rotatestrategy = SEQUENTIAL;
431 fprintf(stderr, "Unknown rotatestrategy: %s\n", s);
434 if ((s = ast_variable_retrieve(cfg, "general", "rotatetimestamp"))) {
435 rotatestrategy = ast_true(s) ? TIMESTAMP : SEQUENTIAL;
436 fprintf(stderr, "rotatetimestamp option has been deprecated. Please use rotatestrategy instead.\n");
441 AST_RWLIST_WRLOCK(&logchannels);
443 var = ast_variable_browse(cfg, "logfiles");
444 for (; var; var = var->next) {
445 if (!(chan = make_logchannel(var->name, var->value, var->lineno))) {
446 /* Print error message directly to the consoles since the lock is held
447 * and we don't want to unlock with the list partially built */
448 ast_console_puts_mutable("ERROR: Unable to create log channel '", __LOG_ERROR);
449 ast_console_puts_mutable(var->name, __LOG_ERROR);
450 ast_console_puts_mutable("'\n", __LOG_ERROR);
453 AST_RWLIST_INSERT_HEAD(&logchannels, chan, list);
454 global_logmask |= chan->logmask;
463 AST_RWLIST_UNLOCK(&logchannels);
466 ast_config_destroy(cfg);
469 void ast_child_verbose(int level, const char *fmt, ...)
471 char *msg = NULL, *emsg = NULL, *sptr, *eptr;
477 if ((size = vsnprintf(msg, 0, fmt, ap)) < 0) {
484 if (!(msg = ast_malloc(size + 1))) {
489 vsnprintf(msg, size + 1, fmt, aq);
492 if (!(emsg = ast_malloc(size * 2 + 1))) {
497 for (sptr = msg, eptr = emsg; ; sptr++) {
508 fprintf(stdout, "verbose \"%s\" %d\n", emsg, level);
513 void ast_queue_log(const char *queuename, const char *callid, const char *agent, const char *event, const char *fmt, ...)
522 if (!logger_initialized) {
523 /* You are too early. We are not open yet! */
526 if (!queuelog_init) {
527 AST_RWLIST_WRLOCK(&logchannels);
528 if (!queuelog_init) {
530 * We have delayed initializing the queue logging system so
531 * preloaded realtime modules can get up. We must initialize
532 * now since someone is trying to log something.
536 AST_RWLIST_UNLOCK(&logchannels);
537 ast_queue_log("NONE", "NONE", "NONE", "QUEUESTART", "%s", "");
539 AST_RWLIST_UNLOCK(&logchannels);
543 if (ast_check_realtime("queue_log")) {
545 ast_localtime(&tv, &tm, NULL);
546 ast_strftime(time_str, sizeof(time_str), "%F %T.%6q", &tm);
548 vsnprintf(qlog_msg, sizeof(qlog_msg), fmt, ap);
550 if (logfiles.queue_adaptive_realtime) {
551 AST_DECLARE_APP_ARGS(args,
552 AST_APP_ARG(data)[5];
554 AST_NONSTANDARD_APP_ARGS(args, qlog_msg, '|');
555 /* Ensure fields are large enough to receive data */
556 ast_realtime_require_field("queue_log",
557 "data1", RQ_CHAR, strlen(S_OR(args.data[0], "")),
558 "data2", RQ_CHAR, strlen(S_OR(args.data[1], "")),
559 "data3", RQ_CHAR, strlen(S_OR(args.data[2], "")),
560 "data4", RQ_CHAR, strlen(S_OR(args.data[3], "")),
561 "data5", RQ_CHAR, strlen(S_OR(args.data[4], "")),
565 ast_store_realtime("queue_log", "time", time_str,
567 "queuename", queuename,
570 "data1", S_OR(args.data[0], ""),
571 "data2", S_OR(args.data[1], ""),
572 "data3", S_OR(args.data[2], ""),
573 "data4", S_OR(args.data[3], ""),
574 "data5", S_OR(args.data[4], ""),
577 ast_store_realtime("queue_log", "time", time_str,
579 "queuename", queuename,
586 if (!logfiles.queue_log_to_file) {
593 qlog_len = snprintf(qlog_msg, sizeof(qlog_msg), "%ld|%s|%s|%s|%s|", (long)time(NULL), callid, queuename, agent, event);
594 vsnprintf(qlog_msg + qlog_len, sizeof(qlog_msg) - qlog_len, fmt, ap);
596 AST_RWLIST_RDLOCK(&logchannels);
598 fprintf(qlog, "%s\n", qlog_msg);
601 AST_RWLIST_UNLOCK(&logchannels);
605 static int rotate_file(const char *filename)
609 int x, y, which, found, res = 0, fd;
610 char *suffixes[4] = { "", ".gz", ".bz2", ".Z" };
612 switch (rotatestrategy) {
615 snprintf(new, sizeof(new), "%s.%d", filename, x);
616 fd = open(new, O_RDONLY);
622 if (rename(filename, new)) {
623 fprintf(stderr, "Unable to rename file '%s' to '%s'\n", filename, new);
630 snprintf(new, sizeof(new), "%s.%ld", filename, (long)time(NULL));
631 if (rename(filename, new)) {
632 fprintf(stderr, "Unable to rename file '%s' to '%s'\n", filename, new);
639 /* Find the next empty slot, including a possible suffix */
642 for (which = 0; which < ARRAY_LEN(suffixes); which++) {
643 snprintf(new, sizeof(new), "%s.%d%s", filename, x, suffixes[which]);
644 fd = open(new, O_RDONLY);
656 /* Found an empty slot */
657 for (y = x; y > 0; y--) {
658 for (which = 0; which < ARRAY_LEN(suffixes); which++) {
659 snprintf(old, sizeof(old), "%s.%d%s", filename, y - 1, suffixes[which]);
660 fd = open(old, O_RDONLY);
662 /* Found the right suffix */
664 snprintf(new, sizeof(new), "%s.%d%s", filename, y, suffixes[which]);
665 if (rename(old, new)) {
666 fprintf(stderr, "Unable to rename file '%s' to '%s'\n", old, new);
674 /* Finally, rename the current file */
675 snprintf(new, sizeof(new), "%s.0", filename);
676 if (rename(filename, new)) {
677 fprintf(stderr, "Unable to rename file '%s' to '%s'\n", filename, new);
684 if (!ast_strlen_zero(exec_after_rotate)) {
685 struct ast_channel *c = ast_dummy_channel_alloc();
688 pbx_builtin_setvar_helper(c, "filename", filename);
689 pbx_substitute_variables_helper(c, exec_after_rotate, buf, sizeof(buf));
691 c = ast_channel_unref(c);
693 if (ast_safe_system(buf) == -1) {
694 ast_log(LOG_WARNING, "error executing '%s'\n", buf);
702 * \brief Start the realtime queue logging if configured.
704 * \retval TRUE if not to open queue log file.
706 static int logger_queue_rt_start(void)
708 if (ast_check_realtime("queue_log")) {
709 if (!ast_realtime_require_field("queue_log",
710 "time", RQ_DATETIME, 26,
711 "data1", RQ_CHAR, 20,
712 "data2", RQ_CHAR, 20,
713 "data3", RQ_CHAR, 20,
714 "data4", RQ_CHAR, 20,
715 "data5", RQ_CHAR, 20,
717 logfiles.queue_adaptive_realtime = 1;
719 logfiles.queue_adaptive_realtime = 0;
722 if (!logfiles.queue_log_to_file) {
723 /* Don't open the log file. */
732 * \brief Rotate the queue log file and restart.
734 * \param queue_rotate Log queue rotation mode.
736 * \note Assumes logchannels is write locked on entry.
738 * \retval 0 on success.
739 * \retval -1 on error.
741 static int logger_queue_restart(int queue_rotate)
744 char qfname[PATH_MAX];
746 if (logger_queue_rt_start()) {
750 snprintf(qfname, sizeof(qfname), "%s/%s", ast_config_AST_LOG_DIR, queue_log_name);
752 /* Just in case it was still open. */
760 /* Open the log file. */
761 qlog = fopen(qfname, "a");
763 ast_log(LOG_ERROR, "Unable to create queue log: %s\n", strerror(errno));
769 static int reload_logger(int rotate, const char *altconf)
771 int queue_rotate = rotate;
772 struct logchannel *f;
775 AST_RWLIST_WRLOCK(&logchannels);
779 /* Check filesize - this one typically doesn't need an auto-rotate */
780 if (ftello(qlog) > 0x40000000) { /* Arbitrarily, 1 GB */
794 ast_mkdir(ast_config_AST_LOG_DIR, 0777);
796 AST_RWLIST_TRAVERSE(&logchannels, f, list) {
798 f->disabled = 0; /* Re-enable logging at reload */
800 <managerEventInstance>
801 <synopsis>Raised when a logging channel is re-enabled after a reload operation.</synopsis>
803 <parameter name="Channel">
804 <para>The name of the logging channel.</para>
807 </managerEventInstance>
809 manager_event(EVENT_FLAG_SYSTEM, "LogChannel", "Channel: %s\r\nEnabled: Yes\r\n", f->filename);
811 if (f->fileptr && (f->fileptr != stdout) && (f->fileptr != stderr)) {
813 if (ftello(f->fileptr) > 0x40000000) { /* Arbitrarily, 1 GB */
814 /* Be more proactive about rotating massive log files */
817 fclose(f->fileptr); /* Close file */
819 if (rotate || rotate_this) {
820 rotate_file(f->filename);
825 filesize_reload_needed = 0;
827 init_logger_chain(1 /* locked */, altconf);
829 ast_unload_realtime("queue_log");
830 if (logfiles.queue_log) {
831 res = logger_queue_restart(queue_rotate);
832 AST_RWLIST_UNLOCK(&logchannels);
833 ast_queue_log("NONE", "NONE", "NONE", "CONFIGRELOAD", "%s", "");
834 ast_verb(1, "Asterisk Queue Logger restarted\n");
836 AST_RWLIST_UNLOCK(&logchannels);
842 /*! \brief Reload the logger module without rotating log files (also used from loader.c during
843 a full Asterisk reload) */
844 int logger_reload(void)
846 if (reload_logger(0, NULL)) {
847 return RESULT_FAILURE;
849 return RESULT_SUCCESS;
852 static char *handle_logger_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
856 e->command = "logger reload";
858 "Usage: logger reload [<alt-conf>]\n"
859 " Reloads the logger subsystem state. Use after restarting syslogd(8) if you are using syslog logging.\n";
864 if (reload_logger(0, a->argc == 3 ? a->argv[2] : NULL)) {
865 ast_cli(a->fd, "Failed to reload the logger\n");
871 static char *handle_logger_rotate(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
875 e->command = "logger rotate";
877 "Usage: logger rotate\n"
878 " Rotates and Reopens the log files.\n";
883 if (reload_logger(1, NULL)) {
884 ast_cli(a->fd, "Failed to reload the logger and rotate log files\n");
890 static char *handle_logger_set_level(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
898 e->command = "logger set level {DEBUG|NOTICE|WARNING|ERROR|VERBOSE|DTMF} {on|off}";
900 "Usage: logger set level {DEBUG|NOTICE|WARNING|ERROR|VERBOSE|DTMF} {on|off}\n"
901 " Set a specific log level to enabled/disabled for this console.\n";
908 return CLI_SHOWUSAGE;
910 AST_RWLIST_WRLOCK(&logchannels);
912 for (x = 0; x < ARRAY_LEN(levels); x++) {
913 if (levels[x] && !strcasecmp(a->argv[3], levels[x])) {
919 AST_RWLIST_UNLOCK(&logchannels);
921 state = ast_true(a->argv[4]) ? 1 : 0;
924 ast_console_toggle_loglevel(a->fd, level, state);
925 ast_cli(a->fd, "Logger status for '%s' has been set to '%s'.\n", levels[level], state ? "on" : "off");
927 return CLI_SHOWUSAGE;
932 /*! \brief CLI command to show logging system configuration */
933 static char *handle_logger_show_channels(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
935 #define FORMATL "%-35.35s %-8.8s %-9.9s "
936 struct logchannel *chan;
939 e->command = "logger show channels";
941 "Usage: logger show channels\n"
942 " List configured logger channels.\n";
947 ast_cli(a->fd, FORMATL, "Channel", "Type", "Status");
948 ast_cli(a->fd, "Configuration\n");
949 ast_cli(a->fd, FORMATL, "-------", "----", "------");
950 ast_cli(a->fd, "-------------\n");
951 AST_RWLIST_RDLOCK(&logchannels);
952 AST_RWLIST_TRAVERSE(&logchannels, chan, list) {
955 ast_cli(a->fd, FORMATL, chan->filename, chan->type == LOGTYPE_CONSOLE ? "Console" : (chan->type == LOGTYPE_SYSLOG ? "Syslog" : "File"),
956 chan->disabled ? "Disabled" : "Enabled");
957 ast_cli(a->fd, " - ");
958 for (level = 0; level < ARRAY_LEN(levels); level++) {
959 if ((chan->logmask & (1 << level)) && levels[level]) {
960 ast_cli(a->fd, "%s ", levels[level]);
963 ast_cli(a->fd, "\n");
965 AST_RWLIST_UNLOCK(&logchannels);
966 ast_cli(a->fd, "\n");
972 void (*verboser)(const char *string);
973 AST_LIST_ENTRY(verb) list;
976 static AST_RWLIST_HEAD_STATIC(verbosers, verb);
978 static struct ast_cli_entry cli_logger[] = {
979 AST_CLI_DEFINE(handle_logger_show_channels, "List configured log channels"),
980 AST_CLI_DEFINE(handle_logger_reload, "Reopens the log files"),
981 AST_CLI_DEFINE(handle_logger_rotate, "Rotates and reopens the log files"),
982 AST_CLI_DEFINE(handle_logger_set_level, "Enables/Disables a specific logging level for this console")
985 static void _handle_SIGXFSZ(int sig)
987 /* Indicate need to reload */
988 filesize_reload_needed = 1;
991 static struct sigaction handle_SIGXFSZ = {
992 .sa_handler = _handle_SIGXFSZ,
993 .sa_flags = SA_RESTART,
996 static void ast_log_vsyslog(struct logmsg *msg)
999 int syslog_level = ast_syslog_priority_from_loglevel(msg->level);
1001 if (syslog_level < 0) {
1002 /* we are locked here, so cannot ast_log() */
1003 fprintf(stderr, "ast_log_vsyslog called with bogus level: %d\n", msg->level);
1007 snprintf(buf, sizeof(buf), "%s[%d]: %s:%d in %s: %s",
1008 levels[msg->level], msg->lwp, msg->file, msg->line, msg->function, msg->message);
1010 term_strip(buf, buf, strlen(buf) + 1);
1011 syslog(syslog_level, "%s", buf);
1014 /* These gymnastics are due to platforms which designate char as unsigned by
1015 * default. Level is the negative character -- offset by 1, because \0 is the
1017 #define VERBOSE_MAGIC2LEVEL(x) (((char) -*(signed char *) (x)) - 1)
1018 #define VERBOSE_HASMAGIC(x) (*(signed char *) (x) < 0)
1020 /*! \brief Print a normal log message to the channels */
1021 static void logger_print_normal(struct logmsg *logmsg)
1023 struct logchannel *chan = NULL;
1025 struct verb *v = NULL;
1028 if (logmsg->level == __LOG_VERBOSE) {
1029 char *tmpmsg = ast_strdupa(logmsg->message + 1);
1030 level = VERBOSE_MAGIC2LEVEL(logmsg->message);
1031 /* Iterate through the list of verbosers and pass them the log message string */
1032 AST_RWLIST_RDLOCK(&verbosers);
1033 AST_RWLIST_TRAVERSE(&verbosers, v, list)
1034 v->verboser(logmsg->message);
1035 AST_RWLIST_UNLOCK(&verbosers);
1036 ast_string_field_set(logmsg, message, tmpmsg);
1039 AST_RWLIST_RDLOCK(&logchannels);
1041 if (!AST_RWLIST_EMPTY(&logchannels)) {
1042 AST_RWLIST_TRAVERSE(&logchannels, chan, list) {
1043 char call_identifier_str[13];
1045 if (logmsg->callid) {
1046 snprintf(call_identifier_str, sizeof(call_identifier_str), "[C-%08x]", logmsg->callid->call_identifier);
1048 call_identifier_str[0] = '\0';
1052 /* If the channel is disabled, then move on to the next one */
1053 if (chan->disabled) {
1056 if (logmsg->level == __LOG_VERBOSE && level > chan->verbosity) {
1060 /* Check syslog channels */
1061 if (chan->type == LOGTYPE_SYSLOG && (chan->logmask & (1 << logmsg->level))) {
1062 ast_log_vsyslog(logmsg);
1063 /* Console channels */
1064 } else if (chan->type == LOGTYPE_CONSOLE && (chan->logmask & (1 << logmsg->level))) {
1066 char tmp1[80], tmp2[80], tmp3[80], tmp4[80];
1068 /* If the level is verbose, then skip it */
1069 if (logmsg->level == __LOG_VERBOSE)
1072 /* Turn the numerical line number into a string */
1073 snprintf(linestr, sizeof(linestr), "%d", logmsg->line);
1074 /* Build string to print out */
1075 snprintf(buf, sizeof(buf), "[%s] %s[%d]%s: %s:%s %s: %s",
1077 term_color(tmp1, logmsg->level_name, colors[logmsg->level], 0, sizeof(tmp1)),
1079 call_identifier_str,
1080 term_color(tmp2, logmsg->file, COLOR_BRWHITE, 0, sizeof(tmp2)),
1081 term_color(tmp3, linestr, COLOR_BRWHITE, 0, sizeof(tmp3)),
1082 term_color(tmp4, logmsg->function, COLOR_BRWHITE, 0, sizeof(tmp4)),
1085 ast_console_puts_mutable(buf, logmsg->level);
1087 } else if (chan->type == LOGTYPE_FILE && (chan->logmask & (1 << logmsg->level))) {
1090 /* If no file pointer exists, skip it */
1091 if (!chan->fileptr) {
1095 /* Print out to the file */
1096 res = fprintf(chan->fileptr, "[%s] %s[%d]%s %s: %s",
1097 logmsg->date, logmsg->level_name, logmsg->lwp, call_identifier_str,
1098 logmsg->file, term_strip(buf, logmsg->message, BUFSIZ));
1099 if (res <= 0 && !ast_strlen_zero(logmsg->message)) {
1100 fprintf(stderr, "**** Asterisk Logging Error: ***********\n");
1101 if (errno == ENOMEM || errno == ENOSPC)
1102 fprintf(stderr, "Asterisk logging error: Out of disk space, can't log to log file %s\n", chan->filename);
1104 fprintf(stderr, "Logger Warning: Unable to write to log file '%s': %s (disabled)\n", chan->filename, strerror(errno));
1106 <managerEventInstance>
1107 <synopsis>Raised when a logging channel is disabled.</synopsis>
1109 <parameter name="Channel">
1110 <para>The name of the logging channel.</para>
1113 </managerEventInstance>
1115 manager_event(EVENT_FLAG_SYSTEM, "LogChannel", "Channel: %s\r\nEnabled: No\r\nReason: %d - %s\r\n", chan->filename, errno, strerror(errno));
1117 } else if (res > 0) {
1118 fflush(chan->fileptr);
1122 } else if (logmsg->level != __LOG_VERBOSE) {
1123 fputs(logmsg->message, stdout);
1126 AST_RWLIST_UNLOCK(&logchannels);
1128 /* If we need to reload because of the file size, then do so */
1129 if (filesize_reload_needed) {
1130 reload_logger(-1, NULL);
1131 ast_verb(1, "Rotated Logs Per SIGXFSZ (Exceeded file size limit)\n");
1137 /*! \brief Actual logging thread */
1138 static void *logger_thread(void *data)
1140 struct logmsg *next = NULL, *msg = NULL;
1143 /* We lock the message list, and see if any message exists... if not we wait on the condition to be signalled */
1144 AST_LIST_LOCK(&logmsgs);
1145 if (AST_LIST_EMPTY(&logmsgs)) {
1146 if (close_logger_thread) {
1147 AST_LIST_UNLOCK(&logmsgs);
1150 ast_cond_wait(&logcond, &logmsgs.lock);
1153 next = AST_LIST_FIRST(&logmsgs);
1154 AST_LIST_HEAD_INIT_NOLOCK(&logmsgs);
1155 AST_LIST_UNLOCK(&logmsgs);
1157 /* Otherwise go through and process each message in the order added */
1158 while ((msg = next)) {
1159 /* Get the next entry now so that we can free our current structure later */
1160 next = AST_LIST_NEXT(msg, list);
1162 /* Depending on the type, send it to the proper function */
1163 logger_print_normal(msg);
1165 /* Free the data since we are done */
1169 /* If we should stop, then stop */
1170 if (close_logger_thread)
1179 * \brief Initialize the logger queue.
1181 * \note Assumes logchannels is write locked on entry.
1185 static void logger_queue_init(void)
1187 ast_unload_realtime("queue_log");
1188 if (logfiles.queue_log) {
1189 char qfname[PATH_MAX];
1191 if (logger_queue_rt_start()) {
1195 /* Open the log file. */
1196 snprintf(qfname, sizeof(qfname), "%s/%s", ast_config_AST_LOG_DIR,
1199 /* Just in case it was already open. */
1202 qlog = fopen(qfname, "a");
1204 ast_log(LOG_ERROR, "Unable to create queue log: %s\n", strerror(errno));
1209 int init_logger(void)
1211 /* auto rotate if sig SIGXFSZ comes a-knockin */
1212 sigaction(SIGXFSZ, &handle_SIGXFSZ, NULL);
1214 /* start logger thread */
1215 ast_cond_init(&logcond, NULL);
1216 if (ast_pthread_create(&logthread, NULL, logger_thread, NULL) < 0) {
1217 ast_cond_destroy(&logcond);
1221 /* register the logger cli commands */
1222 ast_cli_register_multiple(cli_logger, ARRAY_LEN(cli_logger));
1224 ast_mkdir(ast_config_AST_LOG_DIR, 0777);
1226 /* create log channels */
1227 init_logger_chain(0 /* locked */, NULL);
1228 logger_initialized = 1;
1233 void close_logger(void)
1235 struct logchannel *f = NULL;
1237 logger_initialized = 0;
1239 /* Stop logger thread */
1240 AST_LIST_LOCK(&logmsgs);
1241 close_logger_thread = 1;
1242 ast_cond_signal(&logcond);
1243 AST_LIST_UNLOCK(&logmsgs);
1245 if (logthread != AST_PTHREADT_NULL)
1246 pthread_join(logthread, NULL);
1248 AST_RWLIST_WRLOCK(&logchannels);
1255 AST_RWLIST_TRAVERSE(&logchannels, f, list) {
1256 if (f->fileptr && (f->fileptr != stdout) && (f->fileptr != stderr)) {
1262 closelog(); /* syslog */
1264 AST_RWLIST_UNLOCK(&logchannels);
1267 void ast_callid_strnprint(char *buffer, size_t buffer_size, struct ast_callid *callid)
1269 snprintf(buffer, buffer_size, "[C-%08x]", callid->call_identifier);
1272 struct ast_callid *ast_create_callid(void)
1274 struct ast_callid *call;
1277 if (!(call = ao2_alloc(sizeof(struct ast_callid), NULL))) {
1278 ast_log(LOG_ERROR, "Could not allocate callid struct.\n");
1282 using = ast_atomic_fetchadd_int(&next_unique_callid, +1);
1284 call->call_identifier = using;
1285 ast_debug(3, "CALL_ID [C-%08x] created by thread.\n", call->call_identifier);
1289 struct ast_callid *ast_read_threadstorage_callid(void)
1291 struct ast_callid **callid;
1292 callid = ast_threadstorage_get(&unique_callid, sizeof(struct ast_callid **));
1293 if (callid && *callid) {
1294 ast_callid_ref(*callid);
1302 int ast_callid_threadassoc_add(struct ast_callid *callid)
1304 struct ast_callid **pointing;
1305 pointing = ast_threadstorage_get(&unique_callid, sizeof(struct ast_callid **));
1307 ast_log(LOG_ERROR, "Failed to allocate thread storage.\n");
1312 /* callid will be unreffed at thread destruction */
1313 ast_callid_ref(callid);
1315 ast_debug(3, "CALL_ID [C-%08x] bound to thread.\n", callid->call_identifier);
1317 ast_log(LOG_WARNING, "Attempted to ast_callid_threadassoc_add on thread already associated with a callid.\n");
1324 int ast_callid_threadassoc_remove()
1326 struct ast_callid **pointing;
1327 pointing = ast_threadstorage_get(&unique_callid, sizeof(struct ast_callid **));
1329 ast_log(LOG_ERROR, "Failed to allocate thread storage.\n");
1334 ast_log(LOG_ERROR, "Tried to clean callid thread storage with no callid in thread storage.\n");
1337 ast_debug(3, "Call_ID [C-%08x] being removed from thread.\n", (*pointing)->call_identifier);
1338 *pointing = ast_callid_unref(*pointing);
1343 int ast_callid_threadstorage_auto(struct ast_callid **callid)
1345 struct ast_callid *tmp;
1347 /* Start by trying to see if a callid is available from thread storage */
1348 tmp = ast_read_threadstorage_callid();
1354 /* If that failed, try to create a new one and bind it. */
1355 tmp = ast_create_callid();
1357 ast_callid_threadassoc_add(tmp);
1362 /* If neither worked, then something must have gone wrong. */
1366 void ast_callid_threadstorage_auto_clean(struct ast_callid *callid, int callid_created)
1369 /* If the callid was created rather than simply grabbed from the thread storage, we need to unbind here. */
1370 if (callid_created == 1) {
1371 ast_callid_threadassoc_remove();
1373 callid = ast_callid_unref(callid);
1379 * \brief thread storage cleanup function for unique_callid
1381 static void unique_callid_cleanup(void *data)
1383 struct ast_callid **callid = data;
1386 ast_callid_unref(*callid);
1393 * \brief send log messages to syslog and/or the console
1395 static void __attribute__((format(printf, 6, 0))) ast_log_full(int level, const char *file, int line, const char *function, struct ast_callid *callid, const char *fmt, va_list ap)
1397 struct logmsg *logmsg = NULL;
1398 struct ast_str *buf = NULL;
1400 struct timeval now = ast_tvnow();
1402 char datestring[256];
1404 if (!(buf = ast_str_thread_get(&log_buf, LOG_BUF_INIT_SIZE)))
1407 if (level != __LOG_VERBOSE && AST_RWLIST_EMPTY(&logchannels)) {
1409 * we don't have the logger chain configured yet,
1410 * so just log to stdout
1413 result = ast_str_set_va(&buf, BUFSIZ, fmt, ap); /* XXX BUFSIZ ? */
1414 if (result != AST_DYNSTR_BUILD_FAILED) {
1415 term_filter_escapes(ast_str_buffer(buf));
1416 fputs(ast_str_buffer(buf), stdout);
1421 /* Ignore anything that never gets logged anywhere */
1422 if (level != __LOG_VERBOSE && !(global_logmask & (1 << level)))
1426 res = ast_str_set_va(&buf, BUFSIZ, fmt, ap);
1428 /* If the build failed, then abort and free this structure */
1429 if (res == AST_DYNSTR_BUILD_FAILED)
1432 /* Create a new logging message */
1433 if (!(logmsg = ast_calloc_with_stringfields(1, struct logmsg, res + 128)))
1436 /* Copy string over */
1437 ast_string_field_set(logmsg, message, ast_str_buffer(buf));
1440 if (level == __LOG_VERBOSE) {
1441 logmsg->type = LOGMSG_VERBOSE;
1443 logmsg->type = LOGMSG_NORMAL;
1446 if (display_callids && callid) {
1447 logmsg->callid = ast_callid_ref(callid);
1448 /* callid will be unreffed at logmsg destruction */
1451 /* Create our date/time */
1452 ast_localtime(&now, &tm, NULL);
1453 ast_strftime(datestring, sizeof(datestring), dateformat, &tm);
1454 ast_string_field_set(logmsg, date, datestring);
1456 /* Copy over data */
1457 logmsg->level = level;
1458 logmsg->line = line;
1459 ast_string_field_set(logmsg, level_name, levels[level]);
1460 ast_string_field_set(logmsg, file, file);
1461 ast_string_field_set(logmsg, function, function);
1462 logmsg->lwp = ast_get_tid();
1464 /* If the logger thread is active, append it to the tail end of the list - otherwise skip that step */
1465 if (logthread != AST_PTHREADT_NULL) {
1466 AST_LIST_LOCK(&logmsgs);
1467 if (close_logger_thread) {
1468 /* Logger is either closing or closed. We cannot log this message. */
1471 AST_LIST_INSERT_TAIL(&logmsgs, logmsg, list);
1472 ast_cond_signal(&logcond);
1474 AST_LIST_UNLOCK(&logmsgs);
1476 logger_print_normal(logmsg);
1477 logmsg_free(logmsg);
1481 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
1483 struct ast_callid *callid;
1486 callid = ast_read_threadstorage_callid();
1489 ast_log_full(level, file, line, function, callid, fmt, ap);
1493 ast_callid_unref(callid);
1497 void ast_log_callid(int level, const char *file, int line, const char *function, struct ast_callid *callid, const char *fmt, ...)
1501 ast_log_full(level, file, line, function, callid, fmt, ap);
1507 struct ast_bt *ast_bt_create(void)
1509 struct ast_bt *bt = ast_calloc(1, sizeof(*bt));
1511 ast_log(LOG_ERROR, "Unable to allocate memory for backtrace structure!\n");
1517 ast_bt_get_addresses(bt);
1522 int ast_bt_get_addresses(struct ast_bt *bt)
1524 bt->num_frames = backtrace(bt->addresses, AST_MAX_BT_FRAMES);
1529 void *ast_bt_destroy(struct ast_bt *bt)
1538 char **ast_bt_get_symbols(void **addresses, size_t num_frames)
1540 char **strings = NULL;
1541 #if defined(BETTER_BACKTRACES)
1543 bfd *bfdobj; /* bfd.h */
1544 Dl_info dli; /* dlfcn.h */
1546 asymbol **syms = NULL; /* bfd.h */
1547 bfd_vma offset; /* bfd.h */
1548 const char *lastslash;
1550 const char *file, *func;
1552 char address_str[128];
1554 size_t strings_size;
1558 #if defined(BETTER_BACKTRACES)
1559 strings_size = num_frames * sizeof(*strings);
1560 eachlen = ast_calloc(num_frames, sizeof(*eachlen));
1562 if (!(strings = ast_calloc(num_frames, sizeof(*strings)))) {
1566 for (stackfr = 0; stackfr < num_frames; stackfr++) {
1567 int found = 0, symbolcount;
1571 if (!dladdr(addresses[stackfr], &dli)) {
1575 if (strcmp(dli.dli_fname, "asterisk") == 0) {
1576 char asteriskpath[256];
1577 if (!(dli.dli_fname = ast_utils_which("asterisk", asteriskpath, sizeof(asteriskpath)))) {
1578 /* This will fail to find symbols */
1579 ast_debug(1, "Failed to find asterisk binary for debug symbols.\n");
1580 dli.dli_fname = "asterisk";
1584 lastslash = strrchr(dli.dli_fname, '/');
1585 if ( (bfdobj = bfd_openr(dli.dli_fname, NULL)) &&
1586 bfd_check_format(bfdobj, bfd_object) &&
1587 (allocsize = bfd_get_symtab_upper_bound(bfdobj)) > 0 &&
1588 (syms = ast_malloc(allocsize)) &&
1589 (symbolcount = bfd_canonicalize_symtab(bfdobj, syms))) {
1591 if (bfdobj->flags & DYNAMIC) {
1592 offset = addresses[stackfr] - dli.dli_fbase;
1594 offset = addresses[stackfr] - (void *) 0;
1597 for (section = bfdobj->sections; section; section = section->next) {
1598 if ( !bfd_get_section_flags(bfdobj, section) & SEC_ALLOC ||
1599 section->vma > offset ||
1600 section->size + section->vma < offset) {
1604 if (!bfd_find_nearest_line(bfdobj, section, syms, offset - section->vma, &file, &func, &line)) {
1608 /* file can possibly be null even with a success result from bfd_find_nearest_line */
1609 file = file ? file : "";
1611 /* Stack trace output */
1613 if ((lastslash = strrchr(file, '/'))) {
1614 const char *prevslash;
1615 for (prevslash = lastslash - 1; *prevslash != '/' && prevslash >= file; prevslash--);
1616 if (prevslash >= file) {
1617 lastslash = prevslash;
1620 if (dli.dli_saddr == NULL) {
1621 address_str[0] = '\0';
1623 snprintf(address_str, sizeof(address_str), " (%p+%lX)",
1625 (unsigned long) (addresses[stackfr] - dli.dli_saddr));
1627 snprintf(msg, sizeof(msg), "%s:%u %s()%s",
1628 lastslash ? lastslash + 1 : file, line,
1632 break; /* out of section iteration */
1642 /* Default output, if we cannot find the information within BFD */
1644 if (dli.dli_saddr == NULL) {
1645 address_str[0] = '\0';
1647 snprintf(address_str, sizeof(address_str), " (%p+%lX)",
1649 (unsigned long) (addresses[stackfr] - dli.dli_saddr));
1651 snprintf(msg, sizeof(msg), "%s %s()%s",
1652 lastslash ? lastslash + 1 : dli.dli_fname,
1653 S_OR(dli.dli_sname, "<unknown>"),
1657 if (!ast_strlen_zero(msg)) {
1659 eachlen[stackfr] = strlen(msg);
1660 if (!(tmp = ast_realloc(strings, strings_size + eachlen[stackfr] + 1))) {
1663 break; /* out of stack frame iteration */
1666 strings[stackfr] = (char *) strings + strings_size;
1667 ast_copy_string(strings[stackfr], msg, eachlen[stackfr] + 1);
1668 strings_size += eachlen[stackfr] + 1;
1673 /* Recalculate the offset pointers */
1674 strings[0] = (char *) strings + num_frames * sizeof(*strings);
1675 for (stackfr = 1; stackfr < num_frames; stackfr++) {
1676 strings[stackfr] = strings[stackfr - 1] + eachlen[stackfr - 1] + 1;
1679 #else /* !defined(BETTER_BACKTRACES) */
1680 strings = backtrace_symbols(addresses, num_frames);
1681 #endif /* defined(BETTER_BACKTRACES) */
1685 #endif /* HAVE_BKTR */
1687 void ast_backtrace(void)
1694 if (!(bt = ast_bt_create())) {
1695 ast_log(LOG_WARNING, "Unable to allocate space for backtrace structure\n");
1699 if ((strings = ast_bt_get_symbols(bt->addresses, bt->num_frames))) {
1700 ast_debug(1, "Got %d backtrace record%c\n", bt->num_frames, bt->num_frames != 1 ? 's' : ' ');
1701 for (i = 3; i < bt->num_frames - 2; i++) {
1702 ast_debug(1, "#%d: [%p] %s\n", i - 3, bt->addresses[i], strings[i]);
1705 /* MALLOC_DEBUG will erroneously report an error here, unless we undef the macro. */
1709 ast_debug(1, "Could not allocate memory for backtrace\n");
1713 ast_log(LOG_WARNING, "Must run configure with '--with-execinfo' for stack backtraces.\n");
1714 #endif /* defined(HAVE_BKTR) */
1717 void __ast_verbose_ap(const char *file, int line, const char *func, int level, struct ast_callid *callid, const char *fmt, va_list ap)
1719 struct ast_str *buf = NULL;
1721 const char *prefix = level >= 4 ? VERBOSE_PREFIX_4 : level == 3 ? VERBOSE_PREFIX_3 : level == 2 ? VERBOSE_PREFIX_2 : level == 1 ? VERBOSE_PREFIX_1 : "";
1722 signed char magic = level > 127 ? -128 : -level - 1; /* 0 => -1, 1 => -2, etc. Can't pass NUL, as it is EOS-delimiter */
1724 /* For compatibility with modules still calling ast_verbose() directly instead of using ast_verb() */
1726 if (!strncmp(fmt, VERBOSE_PREFIX_4, strlen(VERBOSE_PREFIX_4))) {
1728 } else if (!strncmp(fmt, VERBOSE_PREFIX_3, strlen(VERBOSE_PREFIX_3))) {
1730 } else if (!strncmp(fmt, VERBOSE_PREFIX_2, strlen(VERBOSE_PREFIX_2))) {
1732 } else if (!strncmp(fmt, VERBOSE_PREFIX_1, strlen(VERBOSE_PREFIX_1))) {
1739 if (!(buf = ast_str_thread_get(&verbose_buf, VERBOSE_BUF_INIT_SIZE))) {
1743 if (ast_opt_timestamp) {
1750 ast_localtime(&now, &tm, NULL);
1751 ast_strftime(date, sizeof(date), dateformat, &tm);
1752 datefmt = ast_alloca(strlen(date) + 3 + strlen(prefix) + strlen(fmt) + 1);
1753 sprintf(datefmt, "%c[%s] %s%s", (char) magic, date, prefix, fmt);
1756 char *tmp = ast_alloca(strlen(prefix) + strlen(fmt) + 2);
1757 sprintf(tmp, "%c%s%s", (char) magic, prefix, fmt);
1762 res = ast_str_set_va(&buf, 0, fmt, ap);
1764 /* If the build failed then we can drop this allocated message */
1765 if (res == AST_DYNSTR_BUILD_FAILED) {
1769 ast_log_callid(__LOG_VERBOSE, file, line, func, callid, "%s", ast_str_buffer(buf));
1772 void __ast_verbose(const char *file, int line, const char *func, int level, const char *fmt, ...)
1774 struct ast_callid *callid;
1777 callid = ast_read_threadstorage_callid();
1780 __ast_verbose_ap(file, line, func, level, callid, fmt, ap);
1784 ast_callid_unref(callid);
1788 void __ast_verbose_callid(const char *file, int line, const char *func, int level, struct ast_callid *callid, const char *fmt, ...)
1792 __ast_verbose_ap(file, line, func, level, callid, fmt, ap);
1796 /* No new code should use this directly, but we have the ABI for backwards compat */
1798 void __attribute__((format(printf, 1,2))) ast_verbose(const char *fmt, ...);
1799 void ast_verbose(const char *fmt, ...)
1801 struct ast_callid *callid;
1804 callid = ast_read_threadstorage_callid();
1807 __ast_verbose_ap("", 0, "", 0, callid, fmt, ap);
1811 ast_callid_unref(callid);
1815 int ast_register_verbose(void (*v)(const char *string))
1819 if (!(verb = ast_malloc(sizeof(*verb))))
1824 AST_RWLIST_WRLOCK(&verbosers);
1825 AST_RWLIST_INSERT_HEAD(&verbosers, verb, list);
1826 AST_RWLIST_UNLOCK(&verbosers);
1831 int ast_unregister_verbose(void (*v)(const char *string))
1835 AST_RWLIST_WRLOCK(&verbosers);
1836 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&verbosers, cur, list) {
1837 if (cur->verboser == v) {
1838 AST_RWLIST_REMOVE_CURRENT(list);
1843 AST_RWLIST_TRAVERSE_SAFE_END;
1844 AST_RWLIST_UNLOCK(&verbosers);
1846 return cur ? 0 : -1;
1849 static void update_logchannels(void)
1851 struct logchannel *cur;
1853 AST_RWLIST_WRLOCK(&logchannels);
1857 AST_RWLIST_TRAVERSE(&logchannels, cur, list) {
1858 cur->logmask = make_components(cur->components, cur->lineno, &cur->verbosity);
1859 global_logmask |= cur->logmask;
1862 AST_RWLIST_UNLOCK(&logchannels);
1865 int ast_logger_register_level(const char *name)
1868 unsigned int available = 0;
1870 AST_RWLIST_WRLOCK(&logchannels);
1872 for (level = 0; level < ARRAY_LEN(levels); level++) {
1873 if ((level >= 16) && !available && !levels[level]) {
1878 if (levels[level] && !strcasecmp(levels[level], name)) {
1879 ast_log(LOG_WARNING,
1880 "Unable to register dynamic logger level '%s': a standard logger level uses that name.\n",
1882 AST_RWLIST_UNLOCK(&logchannels);
1889 ast_log(LOG_WARNING,
1890 "Unable to register dynamic logger level '%s'; maximum number of levels registered.\n",
1892 AST_RWLIST_UNLOCK(&logchannels);
1897 levels[available] = ast_strdup(name);
1899 AST_RWLIST_UNLOCK(&logchannels);
1901 ast_debug(1, "Registered dynamic logger level '%s' with index %d.\n", name, available);
1903 update_logchannels();
1908 void ast_logger_unregister_level(const char *name)
1910 unsigned int found = 0;
1913 AST_RWLIST_WRLOCK(&logchannels);
1915 for (x = 16; x < ARRAY_LEN(levels); x++) {
1920 if (strcasecmp(levels[x], name)) {
1929 /* take this level out of the global_logmask, to ensure that no new log messages
1930 * will be queued for it
1933 global_logmask &= ~(1 << x);
1935 ast_free(levels[x]);
1937 AST_RWLIST_UNLOCK(&logchannels);
1939 ast_debug(1, "Unregistered dynamic logger level '%s' with index %d.\n", name, x);
1941 update_logchannels();
1943 AST_RWLIST_UNLOCK(&logchannels);