Mon Jun 18 2012 03:55:36

Asterisk developer's documentation


logger.c
Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2006, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief Asterisk Logger
00022  *
00023  * Logging routines
00024  *
00025  * \author Mark Spencer <markster@digium.com>
00026  */
00027 
00028 #include "asterisk.h"
00029 
00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 360933 $")
00031 
00032 /* When we include logger.h again it will trample on some stuff in syslog.h, but
00033  * nothing we care about in here. */
00034 #include <syslog.h>
00035 
00036 #include "asterisk/_private.h"
00037 #include "asterisk/paths.h"   /* use ast_config_AST_LOG_DIR */
00038 #include "asterisk/logger.h"
00039 #include "asterisk/lock.h"
00040 #include "asterisk/channel.h"
00041 #include "asterisk/config.h"
00042 #include "asterisk/term.h"
00043 #include "asterisk/cli.h"
00044 #include "asterisk/utils.h"
00045 #include "asterisk/manager.h"
00046 #include "asterisk/threadstorage.h"
00047 #include "asterisk/strings.h"
00048 #include "asterisk/pbx.h"
00049 #include "asterisk/app.h"
00050 #include "asterisk/syslog.h"
00051 
00052 #include <signal.h>
00053 #include <time.h>
00054 #include <sys/stat.h>
00055 #include <fcntl.h>
00056 #ifdef HAVE_BKTR
00057 #include <execinfo.h>
00058 #define MAX_BACKTRACE_FRAMES 20
00059 #  if defined(HAVE_DLADDR) && defined(HAVE_BFD) && defined(BETTER_BACKTRACES)
00060 #    include <dlfcn.h>
00061 #    include <bfd.h>
00062 #  endif
00063 #endif
00064 
00065 #if defined(__linux__) && !defined(__NR_gettid)
00066 #include <asm/unistd.h>
00067 #endif
00068 
00069 #if defined(__linux__) && defined(__NR_gettid)
00070 #define GETTID() syscall(__NR_gettid)
00071 #else
00072 #define GETTID() getpid()
00073 #endif
00074 static char dateformat[256] = "%b %e %T";    /* Original Asterisk Format */
00075 
00076 static char queue_log_name[256] = QUEUELOG;
00077 static char exec_after_rotate[256] = "";
00078 
00079 static int filesize_reload_needed;
00080 static unsigned int global_logmask = 0xFFFF;
00081 static int queuelog_init;
00082 static int logger_initialized;
00083 
00084 static enum rotatestrategy {
00085    SEQUENTIAL = 1 << 0,     /* Original method - create a new file, in order */
00086    ROTATE = 1 << 1,         /* Rotate all files, such that the oldest file has the highest suffix */
00087    TIMESTAMP = 1 << 2,      /* Append the epoch timestamp onto the end of the archived file */
00088 } rotatestrategy = SEQUENTIAL;
00089 
00090 static struct {
00091    unsigned int queue_log:1;
00092    unsigned int queue_log_to_file:1;
00093    unsigned int queue_adaptive_realtime:1;
00094 } logfiles = { 1 };
00095 
00096 static char hostname[MAXHOSTNAMELEN];
00097 
00098 enum logtypes {
00099    LOGTYPE_SYSLOG,
00100    LOGTYPE_FILE,
00101    LOGTYPE_CONSOLE,
00102 };
00103 
00104 struct logchannel {
00105    /*! What to log to this channel */
00106    unsigned int logmask;
00107    /*! If this channel is disabled or not */
00108    int disabled;
00109    /*! syslog facility */
00110    int facility;
00111    /*! Type of log channel */
00112    enum logtypes type;
00113    /*! logfile logging file pointer */
00114    FILE *fileptr;
00115    /*! Filename */
00116    char filename[PATH_MAX];
00117    /*! field for linking to list */
00118    AST_LIST_ENTRY(logchannel) list;
00119    /*! Line number from configuration file */
00120    int lineno;
00121    /*! Components (levels) from last config load */
00122    char components[0];
00123 };
00124 
00125 static AST_RWLIST_HEAD_STATIC(logchannels, logchannel);
00126 
00127 enum logmsgtypes {
00128    LOGMSG_NORMAL = 0,
00129    LOGMSG_VERBOSE,
00130 };
00131 
00132 struct logmsg {
00133    enum logmsgtypes type;
00134    int level;
00135    int line;
00136    long process_id;
00137    AST_DECLARE_STRING_FIELDS(
00138       AST_STRING_FIELD(date);
00139       AST_STRING_FIELD(file);
00140       AST_STRING_FIELD(function);
00141       AST_STRING_FIELD(message);
00142       AST_STRING_FIELD(level_name);
00143    );
00144    AST_LIST_ENTRY(logmsg) list;
00145 };
00146 
00147 static AST_LIST_HEAD_STATIC(logmsgs, logmsg);
00148 static pthread_t logthread = AST_PTHREADT_NULL;
00149 static ast_cond_t logcond;
00150 static int close_logger_thread = 0;
00151 
00152 static FILE *qlog;
00153 
00154 /*! \brief Logging channels used in the Asterisk logging system
00155  *
00156  * The first 16 levels are reserved for system usage, and the remaining
00157  * levels are reserved for usage by dynamic levels registered via
00158  * ast_logger_register_level.
00159  */
00160 
00161 /* Modifications to this array are protected by the rwlock in the
00162  * logchannels list.
00163  */
00164 
00165 static char *levels[NUMLOGLEVELS] = {
00166    "DEBUG",
00167    "---EVENT---",    /* no longer used */
00168    "NOTICE",
00169    "WARNING",
00170    "ERROR",
00171    "VERBOSE",
00172    "DTMF",
00173 };
00174 
00175 /*! \brief Colors used in the console for logging */
00176 static const int colors[NUMLOGLEVELS] = {
00177    COLOR_BRGREEN,
00178    COLOR_BRBLUE,     /* no longer used */
00179    COLOR_YELLOW,
00180    COLOR_BRRED,
00181    COLOR_RED,
00182    COLOR_GREEN,
00183    COLOR_BRGREEN,
00184    0,
00185    0,
00186    0,
00187    0,
00188    0,
00189    0,
00190    0,
00191    0,
00192    0,
00193    COLOR_BRBLUE,
00194    COLOR_BRBLUE,
00195    COLOR_BRBLUE,
00196    COLOR_BRBLUE,
00197    COLOR_BRBLUE,
00198    COLOR_BRBLUE,
00199    COLOR_BRBLUE,
00200    COLOR_BRBLUE,
00201    COLOR_BRBLUE,
00202    COLOR_BRBLUE,
00203    COLOR_BRBLUE,
00204    COLOR_BRBLUE,
00205    COLOR_BRBLUE,
00206    COLOR_BRBLUE,
00207    COLOR_BRBLUE,
00208    COLOR_BRBLUE,
00209 };
00210 
00211 AST_THREADSTORAGE(verbose_buf);
00212 #define VERBOSE_BUF_INIT_SIZE   256
00213 
00214 AST_THREADSTORAGE(log_buf);
00215 #define LOG_BUF_INIT_SIZE       256
00216 
00217 static void logger_queue_init(void);
00218 
00219 static unsigned int make_components(const char *s, int lineno)
00220 {
00221    char *w;
00222    unsigned int res = 0;
00223    char *stringp = ast_strdupa(s);
00224    unsigned int x;
00225 
00226    while ((w = strsep(&stringp, ","))) {
00227       w = ast_skip_blanks(w);
00228 
00229       if (!strcmp(w, "*")) {
00230          res = 0xFFFFFFFF;
00231          break;
00232       } else for (x = 0; x < ARRAY_LEN(levels); x++) {
00233          if (levels[x] && !strcasecmp(w, levels[x])) {
00234             res |= (1 << x);
00235             break;
00236          }
00237       }
00238    }
00239 
00240    return res;
00241 }
00242 
00243 static struct logchannel *make_logchannel(const char *channel, const char *components, int lineno)
00244 {
00245    struct logchannel *chan;
00246    char *facility;
00247 
00248    if (ast_strlen_zero(channel) || !(chan = ast_calloc(1, sizeof(*chan) + strlen(components) + 1)))
00249       return NULL;
00250 
00251    strcpy(chan->components, components);
00252    chan->lineno = lineno;
00253 
00254    if (!strcasecmp(channel, "console")) {
00255       chan->type = LOGTYPE_CONSOLE;
00256    } else if (!strncasecmp(channel, "syslog", 6)) {
00257       /*
00258       * syntax is:
00259       *  syslog.facility => level,level,level
00260       */
00261       facility = strchr(channel, '.');
00262       if (!facility++ || !facility) {
00263          facility = "local0";
00264       }
00265 
00266       chan->facility = ast_syslog_facility(facility);
00267 
00268       if (chan->facility < 0) {
00269          fprintf(stderr, "Logger Warning: bad syslog facility in logger.conf\n");
00270          ast_free(chan);
00271          return NULL;
00272       }
00273 
00274       chan->type = LOGTYPE_SYSLOG;
00275       ast_copy_string(chan->filename, channel, sizeof(chan->filename));
00276       openlog("asterisk", LOG_PID, chan->facility);
00277    } else {
00278       if (!ast_strlen_zero(hostname)) {
00279          snprintf(chan->filename, sizeof(chan->filename), "%s/%s.%s",
00280              channel[0] != '/' ? ast_config_AST_LOG_DIR : "", channel, hostname);
00281       } else {
00282          snprintf(chan->filename, sizeof(chan->filename), "%s/%s",
00283              channel[0] != '/' ? ast_config_AST_LOG_DIR : "", channel);
00284       }
00285       if (!(chan->fileptr = fopen(chan->filename, "a"))) {
00286          /* Can't do real logging here since we're called with a lock
00287           * so log to any attached consoles */
00288          ast_console_puts_mutable("ERROR: Unable to open log file '", __LOG_ERROR);
00289          ast_console_puts_mutable(chan->filename, __LOG_ERROR);
00290          ast_console_puts_mutable("': ", __LOG_ERROR);
00291          ast_console_puts_mutable(strerror(errno), __LOG_ERROR);
00292          ast_console_puts_mutable("'\n", __LOG_ERROR);
00293          ast_free(chan);
00294          return NULL;
00295       }
00296       chan->type = LOGTYPE_FILE;
00297    }
00298    chan->logmask = make_components(chan->components, lineno);
00299 
00300    return chan;
00301 }
00302 
00303 static void init_logger_chain(int locked)
00304 {
00305    struct logchannel *chan;
00306    struct ast_config *cfg;
00307    struct ast_variable *var;
00308    const char *s;
00309    struct ast_flags config_flags = { 0 };
00310 
00311    if (!(cfg = ast_config_load2("logger.conf", "logger", config_flags)) || cfg == CONFIG_STATUS_FILEINVALID) {
00312       return;
00313    }
00314 
00315    /* delete our list of log channels */
00316    if (!locked) {
00317       AST_RWLIST_WRLOCK(&logchannels);
00318    }
00319    while ((chan = AST_RWLIST_REMOVE_HEAD(&logchannels, list))) {
00320       ast_free(chan);
00321    }
00322    global_logmask = 0;
00323    if (!locked) {
00324       AST_RWLIST_UNLOCK(&logchannels);
00325    }
00326 
00327    errno = 0;
00328    /* close syslog */
00329    closelog();
00330 
00331    /* If no config file, we're fine, set default options. */
00332    if (!cfg) {
00333       if (errno) {
00334          fprintf(stderr, "Unable to open logger.conf: %s; default settings will be used.\n", strerror(errno));
00335       } else {
00336          fprintf(stderr, "Errors detected in logger.conf: see above; default settings will be used.\n");
00337       }
00338       if (!(chan = ast_calloc(1, sizeof(*chan)))) {
00339          return;
00340       }
00341       chan->type = LOGTYPE_CONSOLE;
00342       chan->logmask = __LOG_WARNING | __LOG_NOTICE | __LOG_ERROR;
00343       if (!locked) {
00344          AST_RWLIST_WRLOCK(&logchannels);
00345       }
00346       AST_RWLIST_INSERT_HEAD(&logchannels, chan, list);
00347       global_logmask |= chan->logmask;
00348       if (!locked) {
00349          AST_RWLIST_UNLOCK(&logchannels);
00350       }
00351       return;
00352    }
00353 
00354    if ((s = ast_variable_retrieve(cfg, "general", "appendhostname"))) {
00355       if (ast_true(s)) {
00356          if (gethostname(hostname, sizeof(hostname) - 1)) {
00357             ast_copy_string(hostname, "unknown", sizeof(hostname));
00358             fprintf(stderr, "What box has no hostname???\n");
00359          }
00360       } else
00361          hostname[0] = '\0';
00362    } else
00363       hostname[0] = '\0';
00364    if ((s = ast_variable_retrieve(cfg, "general", "dateformat")))
00365       ast_copy_string(dateformat, s, sizeof(dateformat));
00366    else
00367       ast_copy_string(dateformat, "%b %e %T", sizeof(dateformat));
00368    if ((s = ast_variable_retrieve(cfg, "general", "queue_log"))) {
00369       logfiles.queue_log = ast_true(s);
00370    }
00371    if ((s = ast_variable_retrieve(cfg, "general", "queue_log_to_file"))) {
00372       logfiles.queue_log_to_file = ast_true(s);
00373    }
00374    if ((s = ast_variable_retrieve(cfg, "general", "queue_log_name"))) {
00375       ast_copy_string(queue_log_name, s, sizeof(queue_log_name));
00376    }
00377    if ((s = ast_variable_retrieve(cfg, "general", "exec_after_rotate"))) {
00378       ast_copy_string(exec_after_rotate, s, sizeof(exec_after_rotate));
00379    }
00380    if ((s = ast_variable_retrieve(cfg, "general", "rotatestrategy"))) {
00381       if (strcasecmp(s, "timestamp") == 0) {
00382          rotatestrategy = TIMESTAMP;
00383       } else if (strcasecmp(s, "rotate") == 0) {
00384          rotatestrategy = ROTATE;
00385       } else if (strcasecmp(s, "sequential") == 0) {
00386          rotatestrategy = SEQUENTIAL;
00387       } else {
00388          fprintf(stderr, "Unknown rotatestrategy: %s\n", s);
00389       }
00390    } else {
00391       if ((s = ast_variable_retrieve(cfg, "general", "rotatetimestamp"))) {
00392          rotatestrategy = ast_true(s) ? TIMESTAMP : SEQUENTIAL;
00393          fprintf(stderr, "rotatetimestamp option has been deprecated.  Please use rotatestrategy instead.\n");
00394       }
00395    }
00396 
00397    if (!locked) {
00398       AST_RWLIST_WRLOCK(&logchannels);
00399    }
00400    var = ast_variable_browse(cfg, "logfiles");
00401    for (; var; var = var->next) {
00402       if (!(chan = make_logchannel(var->name, var->value, var->lineno))) {
00403          /* Print error message directly to the consoles since the lock is held
00404           * and we don't want to unlock with the list partially built */
00405          ast_console_puts_mutable("ERROR: Unable to create log channel '", __LOG_ERROR);
00406          ast_console_puts_mutable(var->name, __LOG_ERROR);
00407          ast_console_puts_mutable("'\n", __LOG_ERROR);
00408          continue;
00409       }
00410       AST_RWLIST_INSERT_HEAD(&logchannels, chan, list);
00411       global_logmask |= chan->logmask;
00412    }
00413 
00414    if (qlog) {
00415       fclose(qlog);
00416       qlog = NULL;
00417    }
00418 
00419    if (!locked) {
00420       AST_RWLIST_UNLOCK(&logchannels);
00421    }
00422 
00423    ast_config_destroy(cfg);
00424 }
00425 
00426 void ast_child_verbose(int level, const char *fmt, ...)
00427 {
00428    char *msg = NULL, *emsg = NULL, *sptr, *eptr;
00429    va_list ap, aq;
00430    int size;
00431 
00432    /* Don't bother, if the level isn't that high */
00433    if (option_verbose < level) {
00434       return;
00435    }
00436 
00437    va_start(ap, fmt);
00438    va_copy(aq, ap);
00439    if ((size = vsnprintf(msg, 0, fmt, ap)) < 0) {
00440       va_end(ap);
00441       va_end(aq);
00442       return;
00443    }
00444    va_end(ap);
00445 
00446    if (!(msg = ast_malloc(size + 1))) {
00447       va_end(aq);
00448       return;
00449    }
00450 
00451    vsnprintf(msg, size + 1, fmt, aq);
00452    va_end(aq);
00453 
00454    if (!(emsg = ast_malloc(size * 2 + 1))) {
00455       ast_free(msg);
00456       return;
00457    }
00458 
00459    for (sptr = msg, eptr = emsg; ; sptr++) {
00460       if (*sptr == '"') {
00461          *eptr++ = '\\';
00462       }
00463       *eptr++ = *sptr;
00464       if (*sptr == '\0') {
00465          break;
00466       }
00467    }
00468    ast_free(msg);
00469 
00470    fprintf(stdout, "verbose \"%s\" %d\n", emsg, level);
00471    fflush(stdout);
00472    ast_free(emsg);
00473 }
00474 
00475 void ast_queue_log(const char *queuename, const char *callid, const char *agent, const char *event, const char *fmt, ...)
00476 {
00477    va_list ap;
00478    struct timeval tv;
00479    struct ast_tm tm;
00480    char qlog_msg[8192];
00481    int qlog_len;
00482    char time_str[30];
00483 
00484    if (!logger_initialized) {
00485       /* You are too early.  We are not open yet! */
00486       return;
00487    }
00488    if (!queuelog_init) {
00489       AST_RWLIST_WRLOCK(&logchannels);
00490       if (!queuelog_init) {
00491          /*
00492           * We have delayed initializing the queue logging system so
00493           * preloaded realtime modules can get up.  We must initialize
00494           * now since someone is trying to log something.
00495           */
00496          logger_queue_init();
00497          queuelog_init = 1;
00498          AST_RWLIST_UNLOCK(&logchannels);
00499          ast_queue_log("NONE", "NONE", "NONE", "QUEUESTART", "%s", "");
00500       } else {
00501          AST_RWLIST_UNLOCK(&logchannels);
00502       }
00503    }
00504 
00505    if (ast_check_realtime("queue_log")) {
00506       tv = ast_tvnow();
00507       ast_localtime(&tv, &tm, NULL);
00508       ast_strftime(time_str, sizeof(time_str), "%F %T.%6q", &tm);
00509       va_start(ap, fmt);
00510       vsnprintf(qlog_msg, sizeof(qlog_msg), fmt, ap);
00511       va_end(ap);
00512       if (logfiles.queue_adaptive_realtime) {
00513          AST_DECLARE_APP_ARGS(args,
00514             AST_APP_ARG(data)[5];
00515          );
00516          AST_NONSTANDARD_APP_ARGS(args, qlog_msg, '|');
00517          /* Ensure fields are large enough to receive data */
00518          ast_realtime_require_field("queue_log",
00519             "data1", RQ_CHAR, strlen(S_OR(args.data[0], "")),
00520             "data2", RQ_CHAR, strlen(S_OR(args.data[1], "")),
00521             "data3", RQ_CHAR, strlen(S_OR(args.data[2], "")),
00522             "data4", RQ_CHAR, strlen(S_OR(args.data[3], "")),
00523             "data5", RQ_CHAR, strlen(S_OR(args.data[4], "")),
00524             SENTINEL);
00525 
00526          /* Store the log */
00527          ast_store_realtime("queue_log", "time", time_str,
00528             "callid", callid,
00529             "queuename", queuename,
00530             "agent", agent,
00531             "event", event,
00532             "data1", S_OR(args.data[0], ""),
00533             "data2", S_OR(args.data[1], ""),
00534             "data3", S_OR(args.data[2], ""),
00535             "data4", S_OR(args.data[3], ""),
00536             "data5", S_OR(args.data[4], ""),
00537             SENTINEL);
00538       } else {
00539          ast_store_realtime("queue_log", "time", time_str,
00540             "callid", callid,
00541             "queuename", queuename,
00542             "agent", agent,
00543             "event", event,
00544             "data", qlog_msg,
00545             SENTINEL);
00546       }
00547 
00548       if (!logfiles.queue_log_to_file) {
00549          return;
00550       }
00551    }
00552 
00553    if (qlog) {
00554       va_start(ap, fmt);
00555       qlog_len = snprintf(qlog_msg, sizeof(qlog_msg), "%ld|%s|%s|%s|%s|", (long)time(NULL), callid, queuename, agent, event);
00556       vsnprintf(qlog_msg + qlog_len, sizeof(qlog_msg) - qlog_len, fmt, ap);
00557       va_end(ap);
00558       AST_RWLIST_RDLOCK(&logchannels);
00559       if (qlog) {
00560          fprintf(qlog, "%s\n", qlog_msg);
00561          fflush(qlog);
00562       }
00563       AST_RWLIST_UNLOCK(&logchannels);
00564    }
00565 }
00566 
00567 static int rotate_file(const char *filename)
00568 {
00569    char old[PATH_MAX];
00570    char new[PATH_MAX];
00571    int x, y, which, found, res = 0, fd;
00572    char *suffixes[4] = { "", ".gz", ".bz2", ".Z" };
00573 
00574    switch (rotatestrategy) {
00575    case SEQUENTIAL:
00576       for (x = 0; ; x++) {
00577          snprintf(new, sizeof(new), "%s.%d", filename, x);
00578          fd = open(new, O_RDONLY);
00579          if (fd > -1)
00580             close(fd);
00581          else
00582             break;
00583       }
00584       if (rename(filename, new)) {
00585          fprintf(stderr, "Unable to rename file '%s' to '%s'\n", filename, new);
00586          res = -1;
00587       } else {
00588          filename = new;
00589       }
00590       break;
00591    case TIMESTAMP:
00592       snprintf(new, sizeof(new), "%s.%ld", filename, (long)time(NULL));
00593       if (rename(filename, new)) {
00594          fprintf(stderr, "Unable to rename file '%s' to '%s'\n", filename, new);
00595          res = -1;
00596       } else {
00597          filename = new;
00598       }
00599       break;
00600    case ROTATE:
00601       /* Find the next empty slot, including a possible suffix */
00602       for (x = 0; ; x++) {
00603          found = 0;
00604          for (which = 0; which < ARRAY_LEN(suffixes); which++) {
00605             snprintf(new, sizeof(new), "%s.%d%s", filename, x, suffixes[which]);
00606             fd = open(new, O_RDONLY);
00607             if (fd > -1) {
00608                close(fd);
00609                found = 1;
00610                break;
00611             }
00612          }
00613          if (!found) {
00614             break;
00615          }
00616       }
00617 
00618       /* Found an empty slot */
00619       for (y = x; y > 0; y--) {
00620          for (which = 0; which < ARRAY_LEN(suffixes); which++) {
00621             snprintf(old, sizeof(old), "%s.%d%s", filename, y - 1, suffixes[which]);
00622             fd = open(old, O_RDONLY);
00623             if (fd > -1) {
00624                /* Found the right suffix */
00625                close(fd);
00626                snprintf(new, sizeof(new), "%s.%d%s", filename, y, suffixes[which]);
00627                if (rename(old, new)) {
00628                   fprintf(stderr, "Unable to rename file '%s' to '%s'\n", old, new);
00629                   res = -1;
00630                }
00631                break;
00632             }
00633          }
00634       }
00635 
00636       /* Finally, rename the current file */
00637       snprintf(new, sizeof(new), "%s.0", filename);
00638       if (rename(filename, new)) {
00639          fprintf(stderr, "Unable to rename file '%s' to '%s'\n", filename, new);
00640          res = -1;
00641       } else {
00642          filename = new;
00643       }
00644    }
00645 
00646    if (!ast_strlen_zero(exec_after_rotate)) {
00647       struct ast_channel *c = ast_dummy_channel_alloc();
00648       char buf[512];
00649 
00650       pbx_builtin_setvar_helper(c, "filename", filename);
00651       pbx_substitute_variables_helper(c, exec_after_rotate, buf, sizeof(buf));
00652       if (c) {
00653          c = ast_channel_unref(c);
00654       }
00655       if (ast_safe_system(buf) == -1) {
00656          ast_log(LOG_WARNING, "error executing '%s'\n", buf);
00657       }
00658    }
00659    return res;
00660 }
00661 
00662 /*!
00663  * \internal
00664  * \brief Start the realtime queue logging if configured.
00665  *
00666  * \retval TRUE if not to open queue log file.
00667  */
00668 static int logger_queue_rt_start(void)
00669 {
00670    if (ast_check_realtime("queue_log")) {
00671       if (!ast_realtime_require_field("queue_log",
00672          "time", RQ_DATETIME, 26,
00673          "data1", RQ_CHAR, 20,
00674          "data2", RQ_CHAR, 20,
00675          "data3", RQ_CHAR, 20,
00676          "data4", RQ_CHAR, 20,
00677          "data5", RQ_CHAR, 20,
00678          SENTINEL)) {
00679          logfiles.queue_adaptive_realtime = 1;
00680       } else {
00681          logfiles.queue_adaptive_realtime = 0;
00682       }
00683 
00684       if (!logfiles.queue_log_to_file) {
00685          /* Don't open the log file. */
00686          return 1;
00687       }
00688    }
00689    return 0;
00690 }
00691 
00692 /*!
00693  * \internal
00694  * \brief Rotate the queue log file and restart.
00695  *
00696  * \param queue_rotate Log queue rotation mode.
00697  *
00698  * \note Assumes logchannels is write locked on entry.
00699  *
00700  * \retval 0 on success.
00701  * \retval -1 on error.
00702  */
00703 static int logger_queue_restart(int queue_rotate)
00704 {
00705    int res = 0;
00706    char qfname[PATH_MAX];
00707 
00708    if (logger_queue_rt_start()) {
00709       return res;
00710    }
00711 
00712    snprintf(qfname, sizeof(qfname), "%s/%s", ast_config_AST_LOG_DIR, queue_log_name);
00713    if (qlog) {
00714       /* Just in case it was still open. */
00715       fclose(qlog);
00716       qlog = NULL;
00717    }
00718    if (queue_rotate) {
00719       rotate_file(qfname);
00720    }
00721 
00722    /* Open the log file. */
00723    qlog = fopen(qfname, "a");
00724    if (!qlog) {
00725       ast_log(LOG_ERROR, "Unable to create queue log: %s\n", strerror(errno));
00726       res = -1;
00727    }
00728    return res;
00729 }
00730 
00731 static int reload_logger(int rotate)
00732 {
00733    int queue_rotate = rotate;
00734    struct logchannel *f;
00735    int res = 0;
00736 
00737    AST_RWLIST_WRLOCK(&logchannels);
00738 
00739    if (qlog) {
00740       if (rotate < 0) {
00741          /* Check filesize - this one typically doesn't need an auto-rotate */
00742          if (ftello(qlog) > 0x40000000) { /* Arbitrarily, 1 GB */
00743             fclose(qlog);
00744             qlog = NULL;
00745          } else {
00746             queue_rotate = 0;
00747          }
00748       } else {
00749          fclose(qlog);
00750          qlog = NULL;
00751       }
00752    } else {
00753       queue_rotate = 0;
00754    }
00755 
00756    ast_mkdir(ast_config_AST_LOG_DIR, 0777);
00757 
00758    AST_RWLIST_TRAVERSE(&logchannels, f, list) {
00759       if (f->disabled) {
00760          f->disabled = 0;  /* Re-enable logging at reload */
00761          manager_event(EVENT_FLAG_SYSTEM, "LogChannel", "Channel: %s\r\nEnabled: Yes\r\n", f->filename);
00762       }
00763       if (f->fileptr && (f->fileptr != stdout) && (f->fileptr != stderr)) {
00764          int rotate_this = 0;
00765          if (ftello(f->fileptr) > 0x40000000) { /* Arbitrarily, 1 GB */
00766             /* Be more proactive about rotating massive log files */
00767             rotate_this = 1;
00768          }
00769          fclose(f->fileptr);  /* Close file */
00770          f->fileptr = NULL;
00771          if (rotate || rotate_this) {
00772             rotate_file(f->filename);
00773          }
00774       }
00775    }
00776 
00777    filesize_reload_needed = 0;
00778 
00779    init_logger_chain(1 /* locked */);
00780 
00781    ast_unload_realtime("queue_log");
00782    if (logfiles.queue_log) {
00783       res = logger_queue_restart(queue_rotate);
00784       AST_RWLIST_UNLOCK(&logchannels);
00785       ast_queue_log("NONE", "NONE", "NONE", "CONFIGRELOAD", "%s", "");
00786       ast_verb(1, "Asterisk Queue Logger restarted\n");
00787    } else {
00788       AST_RWLIST_UNLOCK(&logchannels);
00789    }
00790 
00791    return res;
00792 }
00793 
00794 /*! \brief Reload the logger module without rotating log files (also used from loader.c during
00795    a full Asterisk reload) */
00796 int logger_reload(void)
00797 {
00798    if (reload_logger(0)) {
00799       return RESULT_FAILURE;
00800    }
00801    return RESULT_SUCCESS;
00802 }
00803 
00804 static char *handle_logger_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00805 {
00806    switch (cmd) {
00807    case CLI_INIT:
00808       e->command = "logger reload";
00809       e->usage = 
00810          "Usage: logger reload\n"
00811          "       Reloads the logger subsystem state.  Use after restarting syslogd(8) if you are using syslog logging.\n";
00812       return NULL;
00813    case CLI_GENERATE:
00814       return NULL;
00815    }
00816    if (reload_logger(0)) {
00817       ast_cli(a->fd, "Failed to reload the logger\n");
00818       return CLI_FAILURE;
00819    }
00820    return CLI_SUCCESS;
00821 }
00822 
00823 static char *handle_logger_rotate(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00824 {
00825    switch (cmd) {
00826    case CLI_INIT:
00827       e->command = "logger rotate";
00828       e->usage = 
00829          "Usage: logger rotate\n"
00830          "       Rotates and Reopens the log files.\n";
00831       return NULL;
00832    case CLI_GENERATE:
00833       return NULL;   
00834    }
00835    if (reload_logger(1)) {
00836       ast_cli(a->fd, "Failed to reload the logger and rotate log files\n");
00837       return CLI_FAILURE;
00838    } 
00839    return CLI_SUCCESS;
00840 }
00841 
00842 static char *handle_logger_set_level(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00843 {
00844    int x;
00845    int state;
00846    int level = -1;
00847 
00848    switch (cmd) {
00849    case CLI_INIT:
00850       e->command = "logger set level {DEBUG|NOTICE|WARNING|ERROR|VERBOSE|DTMF} {on|off}";
00851       e->usage = 
00852          "Usage: logger set level {DEBUG|NOTICE|WARNING|ERROR|VERBOSE|DTMF} {on|off}\n"
00853          "       Set a specific log level to enabled/disabled for this console.\n";
00854       return NULL;
00855    case CLI_GENERATE:
00856       return NULL;
00857    }
00858 
00859    if (a->argc < 5)
00860       return CLI_SHOWUSAGE;
00861 
00862    AST_RWLIST_WRLOCK(&logchannels);
00863 
00864    for (x = 0; x < ARRAY_LEN(levels); x++) {
00865       if (levels[x] && !strcasecmp(a->argv[3], levels[x])) {
00866          level = x;
00867          break;
00868       }
00869    }
00870 
00871    AST_RWLIST_UNLOCK(&logchannels);
00872 
00873    state = ast_true(a->argv[4]) ? 1 : 0;
00874 
00875    if (level != -1) {
00876       ast_console_toggle_loglevel(a->fd, level, state);
00877       ast_cli(a->fd, "Logger status for '%s' has been set to '%s'.\n", levels[level], state ? "on" : "off");
00878    } else
00879       return CLI_SHOWUSAGE;
00880 
00881    return CLI_SUCCESS;
00882 }
00883 
00884 /*! \brief CLI command to show logging system configuration */
00885 static char *handle_logger_show_channels(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00886 {
00887 #define FORMATL   "%-35.35s %-8.8s %-9.9s "
00888    struct logchannel *chan;
00889    switch (cmd) {
00890    case CLI_INIT:
00891       e->command = "logger show channels";
00892       e->usage = 
00893          "Usage: logger show channels\n"
00894          "       List configured logger channels.\n";
00895       return NULL;
00896    case CLI_GENERATE:
00897       return NULL;   
00898    }
00899    ast_cli(a->fd, FORMATL, "Channel", "Type", "Status");
00900    ast_cli(a->fd, "Configuration\n");
00901    ast_cli(a->fd, FORMATL, "-------", "----", "------");
00902    ast_cli(a->fd, "-------------\n");
00903    AST_RWLIST_RDLOCK(&logchannels);
00904    AST_RWLIST_TRAVERSE(&logchannels, chan, list) {
00905       unsigned int level;
00906 
00907       ast_cli(a->fd, FORMATL, chan->filename, chan->type == LOGTYPE_CONSOLE ? "Console" : (chan->type == LOGTYPE_SYSLOG ? "Syslog" : "File"),
00908          chan->disabled ? "Disabled" : "Enabled");
00909       ast_cli(a->fd, " - ");
00910       for (level = 0; level < ARRAY_LEN(levels); level++) {
00911          if ((chan->logmask & (1 << level)) && levels[level]) {
00912             ast_cli(a->fd, "%s ", levels[level]);
00913          }
00914       }
00915       ast_cli(a->fd, "\n");
00916    }
00917    AST_RWLIST_UNLOCK(&logchannels);
00918    ast_cli(a->fd, "\n");
00919 
00920    return CLI_SUCCESS;
00921 }
00922 
00923 struct verb {
00924    void (*verboser)(const char *string);
00925    AST_LIST_ENTRY(verb) list;
00926 };
00927 
00928 static AST_RWLIST_HEAD_STATIC(verbosers, verb);
00929 
00930 static struct ast_cli_entry cli_logger[] = {
00931    AST_CLI_DEFINE(handle_logger_show_channels, "List configured log channels"),
00932    AST_CLI_DEFINE(handle_logger_reload, "Reopens the log files"),
00933    AST_CLI_DEFINE(handle_logger_rotate, "Rotates and reopens the log files"),
00934    AST_CLI_DEFINE(handle_logger_set_level, "Enables/Disables a specific logging level for this console")
00935 };
00936 
00937 static void _handle_SIGXFSZ(int sig)
00938 {
00939    /* Indicate need to reload */
00940    filesize_reload_needed = 1;
00941 }
00942 
00943 static struct sigaction handle_SIGXFSZ = {
00944    .sa_handler = _handle_SIGXFSZ,
00945    .sa_flags = SA_RESTART,
00946 };
00947 
00948 static void ast_log_vsyslog(struct logmsg *msg)
00949 {
00950    char buf[BUFSIZ];
00951    int syslog_level = ast_syslog_priority_from_loglevel(msg->level);
00952 
00953    if (syslog_level < 0) {
00954       /* we are locked here, so cannot ast_log() */
00955       fprintf(stderr, "ast_log_vsyslog called with bogus level: %d\n", msg->level);
00956       return;
00957    }
00958 
00959    snprintf(buf, sizeof(buf), "%s[%ld]: %s:%d in %s: %s",
00960        levels[msg->level], msg->process_id, msg->file, msg->line, msg->function, msg->message);
00961 
00962    term_strip(buf, buf, strlen(buf) + 1);
00963    syslog(syslog_level, "%s", buf);
00964 }
00965 
00966 /*! \brief Print a normal log message to the channels */
00967 static void logger_print_normal(struct logmsg *logmsg)
00968 {
00969    struct logchannel *chan = NULL;
00970    char buf[BUFSIZ];
00971    struct verb *v = NULL;
00972 
00973    if (logmsg->level == __LOG_VERBOSE) {
00974       char *tmpmsg = ast_strdupa(logmsg->message + 1);
00975       /* Iterate through the list of verbosers and pass them the log message string */
00976       AST_RWLIST_RDLOCK(&verbosers);
00977       AST_RWLIST_TRAVERSE(&verbosers, v, list)
00978          v->verboser(logmsg->message);
00979       AST_RWLIST_UNLOCK(&verbosers);
00980       ast_string_field_set(logmsg, message, tmpmsg);
00981    }
00982 
00983    AST_RWLIST_RDLOCK(&logchannels);
00984 
00985    if (!AST_RWLIST_EMPTY(&logchannels)) {
00986       AST_RWLIST_TRAVERSE(&logchannels, chan, list) {
00987          /* If the channel is disabled, then move on to the next one */
00988          if (chan->disabled)
00989             continue;
00990          /* Check syslog channels */
00991          if (chan->type == LOGTYPE_SYSLOG && (chan->logmask & (1 << logmsg->level))) {
00992             ast_log_vsyslog(logmsg);
00993          /* Console channels */
00994          } else if (chan->type == LOGTYPE_CONSOLE && (chan->logmask & (1 << logmsg->level))) {
00995             char linestr[128];
00996             char tmp1[80], tmp2[80], tmp3[80], tmp4[80];
00997 
00998             /* If the level is verbose, then skip it */
00999             if (logmsg->level == __LOG_VERBOSE)
01000                continue;
01001 
01002             /* Turn the numerical line number into a string */
01003             snprintf(linestr, sizeof(linestr), "%d", logmsg->line);
01004             /* Build string to print out */
01005             snprintf(buf, sizeof(buf), "[%s] %s[%ld]: %s:%s %s: %s",
01006                 logmsg->date,
01007                 term_color(tmp1, logmsg->level_name, colors[logmsg->level], 0, sizeof(tmp1)),
01008                 logmsg->process_id,
01009                 term_color(tmp2, logmsg->file, COLOR_BRWHITE, 0, sizeof(tmp2)),
01010                 term_color(tmp3, linestr, COLOR_BRWHITE, 0, sizeof(tmp3)),
01011                 term_color(tmp4, logmsg->function, COLOR_BRWHITE, 0, sizeof(tmp4)),
01012                 logmsg->message);
01013             /* Print out */
01014             ast_console_puts_mutable(buf, logmsg->level);
01015          /* File channels */
01016          } else if (chan->type == LOGTYPE_FILE && (chan->logmask & (1 << logmsg->level))) {
01017             int res = 0;
01018 
01019             /* If no file pointer exists, skip it */
01020             if (!chan->fileptr) {
01021                continue;
01022             }
01023 
01024             /* Print out to the file */
01025             res = fprintf(chan->fileptr, "[%s] %s[%ld] %s: %s",
01026                      logmsg->date, logmsg->level_name, logmsg->process_id, logmsg->file, term_strip(buf, logmsg->message, BUFSIZ));
01027             if (res <= 0 && !ast_strlen_zero(logmsg->message)) {
01028                fprintf(stderr, "**** Asterisk Logging Error: ***********\n");
01029                if (errno == ENOMEM || errno == ENOSPC)
01030                   fprintf(stderr, "Asterisk logging error: Out of disk space, can't log to log file %s\n", chan->filename);
01031                else
01032                   fprintf(stderr, "Logger Warning: Unable to write to log file '%s': %s (disabled)\n", chan->filename, strerror(errno));
01033                manager_event(EVENT_FLAG_SYSTEM, "LogChannel", "Channel: %s\r\nEnabled: No\r\nReason: %d - %s\r\n", chan->filename, errno, strerror(errno));
01034                chan->disabled = 1;
01035             } else if (res > 0) {
01036                fflush(chan->fileptr);
01037             }
01038          }
01039       }
01040    } else if (logmsg->level != __LOG_VERBOSE) {
01041       fputs(logmsg->message, stdout);
01042    }
01043 
01044    AST_RWLIST_UNLOCK(&logchannels);
01045 
01046    /* If we need to reload because of the file size, then do so */
01047    if (filesize_reload_needed) {
01048       reload_logger(-1);
01049       ast_verb(1, "Rotated Logs Per SIGXFSZ (Exceeded file size limit)\n");
01050    }
01051 
01052    return;
01053 }
01054 
01055 /*! \brief Actual logging thread */
01056 static void *logger_thread(void *data)
01057 {
01058    struct logmsg *next = NULL, *msg = NULL;
01059 
01060    for (;;) {
01061       /* We lock the message list, and see if any message exists... if not we wait on the condition to be signalled */
01062       AST_LIST_LOCK(&logmsgs);
01063       if (AST_LIST_EMPTY(&logmsgs)) {
01064          if (close_logger_thread) {
01065             AST_LIST_UNLOCK(&logmsgs);
01066             break;
01067          } else {
01068             ast_cond_wait(&logcond, &logmsgs.lock);
01069          }
01070       }
01071       next = AST_LIST_FIRST(&logmsgs);
01072       AST_LIST_HEAD_INIT_NOLOCK(&logmsgs);
01073       AST_LIST_UNLOCK(&logmsgs);
01074 
01075       /* Otherwise go through and process each message in the order added */
01076       while ((msg = next)) {
01077          /* Get the next entry now so that we can free our current structure later */
01078          next = AST_LIST_NEXT(msg, list);
01079 
01080          /* Depending on the type, send it to the proper function */
01081          logger_print_normal(msg);
01082 
01083          /* Free the data since we are done */
01084          ast_free(msg);
01085       }
01086 
01087       /* If we should stop, then stop */
01088       if (close_logger_thread)
01089          break;
01090    }
01091 
01092    return NULL;
01093 }
01094 
01095 /*!
01096  * \internal
01097  * \brief Initialize the logger queue.
01098  *
01099  * \note Assumes logchannels is write locked on entry.
01100  *
01101  * \return Nothing
01102  */
01103 static void logger_queue_init(void)
01104 {
01105    ast_unload_realtime("queue_log");
01106    if (logfiles.queue_log) {
01107       char qfname[PATH_MAX];
01108 
01109       if (logger_queue_rt_start()) {
01110          return;
01111       }
01112 
01113       /* Open the log file. */
01114       snprintf(qfname, sizeof(qfname), "%s/%s", ast_config_AST_LOG_DIR,
01115          queue_log_name);
01116       if (qlog) {
01117          /* Just in case it was already open. */
01118          fclose(qlog);
01119       }
01120       qlog = fopen(qfname, "a");
01121       if (!qlog) {
01122          ast_log(LOG_ERROR, "Unable to create queue log: %s\n", strerror(errno));
01123       }
01124    }
01125 }
01126 
01127 int init_logger(void)
01128 {
01129    /* auto rotate if sig SIGXFSZ comes a-knockin */
01130    sigaction(SIGXFSZ, &handle_SIGXFSZ, NULL);
01131 
01132    /* start logger thread */
01133    ast_cond_init(&logcond, NULL);
01134    if (ast_pthread_create(&logthread, NULL, logger_thread, NULL) < 0) {
01135       ast_cond_destroy(&logcond);
01136       return -1;
01137    }
01138 
01139    /* register the logger cli commands */
01140    ast_cli_register_multiple(cli_logger, ARRAY_LEN(cli_logger));
01141 
01142    ast_mkdir(ast_config_AST_LOG_DIR, 0777);
01143 
01144    /* create log channels */
01145    init_logger_chain(0 /* locked */);
01146    logger_initialized = 1;
01147 
01148    return 0;
01149 }
01150 
01151 void close_logger(void)
01152 {
01153    struct logchannel *f = NULL;
01154 
01155    logger_initialized = 0;
01156 
01157    /* Stop logger thread */
01158    AST_LIST_LOCK(&logmsgs);
01159    close_logger_thread = 1;
01160    ast_cond_signal(&logcond);
01161    AST_LIST_UNLOCK(&logmsgs);
01162 
01163    if (logthread != AST_PTHREADT_NULL)
01164       pthread_join(logthread, NULL);
01165 
01166    AST_RWLIST_WRLOCK(&logchannels);
01167 
01168    if (qlog) {
01169       fclose(qlog);
01170       qlog = NULL;
01171    }
01172 
01173    AST_RWLIST_TRAVERSE(&logchannels, f, list) {
01174       if (f->fileptr && (f->fileptr != stdout) && (f->fileptr != stderr)) {
01175          fclose(f->fileptr);
01176          f->fileptr = NULL;
01177       }
01178    }
01179 
01180    closelog(); /* syslog */
01181 
01182    AST_RWLIST_UNLOCK(&logchannels);
01183 }
01184 
01185 /*!
01186  * \brief send log messages to syslog and/or the console
01187  */
01188 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
01189 {
01190    struct logmsg *logmsg = NULL;
01191    struct ast_str *buf = NULL;
01192    struct ast_tm tm;
01193    struct timeval now = ast_tvnow();
01194    int res = 0;
01195    va_list ap;
01196    char datestring[256];
01197 
01198    if (!(buf = ast_str_thread_get(&log_buf, LOG_BUF_INIT_SIZE)))
01199       return;
01200 
01201    if (level != __LOG_VERBOSE && AST_RWLIST_EMPTY(&logchannels)) {
01202       /*
01203        * we don't have the logger chain configured yet,
01204        * so just log to stdout
01205        */
01206       int result;
01207       va_start(ap, fmt);
01208       result = ast_str_set_va(&buf, BUFSIZ, fmt, ap); /* XXX BUFSIZ ? */
01209       va_end(ap);
01210       if (result != AST_DYNSTR_BUILD_FAILED) {
01211          term_filter_escapes(ast_str_buffer(buf));
01212          fputs(ast_str_buffer(buf), stdout);
01213       }
01214       return;
01215    }
01216    
01217    /* don't display LOG_DEBUG messages unless option_verbose _or_ option_debug
01218       are non-zero; LOG_DEBUG messages can still be displayed if option_debug
01219       is zero, if option_verbose is non-zero (this allows for 'level zero'
01220       LOG_DEBUG messages to be displayed, if the logmask on any channel
01221       allows it)
01222    */
01223    if (!option_verbose && !option_debug && (level == __LOG_DEBUG))
01224       return;
01225 
01226    /* Ignore anything that never gets logged anywhere */
01227    if (level != __LOG_VERBOSE && !(global_logmask & (1 << level)))
01228       return;
01229    
01230    /* Build string */
01231    va_start(ap, fmt);
01232    res = ast_str_set_va(&buf, BUFSIZ, fmt, ap);
01233    va_end(ap);
01234 
01235    /* If the build failed, then abort and free this structure */
01236    if (res == AST_DYNSTR_BUILD_FAILED)
01237       return;
01238 
01239    /* Create a new logging message */
01240    if (!(logmsg = ast_calloc_with_stringfields(1, struct logmsg, res + 128)))
01241       return;
01242 
01243    /* Copy string over */
01244    ast_string_field_set(logmsg, message, ast_str_buffer(buf));
01245 
01246    /* Set type */
01247    if (level == __LOG_VERBOSE) {
01248       logmsg->type = LOGMSG_VERBOSE;
01249    } else {
01250       logmsg->type = LOGMSG_NORMAL;
01251    }
01252 
01253    /* Create our date/time */
01254    ast_localtime(&now, &tm, NULL);
01255    ast_strftime(datestring, sizeof(datestring), dateformat, &tm);
01256    ast_string_field_set(logmsg, date, datestring);
01257 
01258    /* Copy over data */
01259    logmsg->level = level;
01260    logmsg->line = line;
01261    ast_string_field_set(logmsg, level_name, levels[level]);
01262    ast_string_field_set(logmsg, file, file);
01263    ast_string_field_set(logmsg, function, function);
01264    logmsg->process_id = (long) GETTID();
01265 
01266    /* If the logger thread is active, append it to the tail end of the list - otherwise skip that step */
01267    if (logthread != AST_PTHREADT_NULL) {
01268       AST_LIST_LOCK(&logmsgs);
01269       if (close_logger_thread) {
01270          /* Logger is either closing or closed.  We cannot log this message. */
01271          ast_free(logmsg);
01272       } else {
01273          AST_LIST_INSERT_TAIL(&logmsgs, logmsg, list);
01274          ast_cond_signal(&logcond);
01275       }
01276       AST_LIST_UNLOCK(&logmsgs);
01277    } else {
01278       logger_print_normal(logmsg);
01279       ast_free(logmsg);
01280    }
01281 }
01282 
01283 #ifdef HAVE_BKTR
01284 
01285 struct ast_bt *ast_bt_create(void) 
01286 {
01287    struct ast_bt *bt = ast_calloc(1, sizeof(*bt));
01288    if (!bt) {
01289       ast_log(LOG_ERROR, "Unable to allocate memory for backtrace structure!\n");
01290       return NULL;
01291    }
01292 
01293    bt->alloced = 1;
01294 
01295    ast_bt_get_addresses(bt);
01296 
01297    return bt;
01298 }
01299 
01300 int ast_bt_get_addresses(struct ast_bt *bt)
01301 {
01302    bt->num_frames = backtrace(bt->addresses, AST_MAX_BT_FRAMES);
01303 
01304    return 0;
01305 }
01306 
01307 void *ast_bt_destroy(struct ast_bt *bt)
01308 {
01309    if (bt->alloced) {
01310       ast_free(bt);
01311    }
01312 
01313    return NULL;
01314 }
01315 
01316 char **ast_bt_get_symbols(void **addresses, size_t num_frames)
01317 {
01318    char **strings = NULL;
01319 #if defined(BETTER_BACKTRACES)
01320    int stackfr;
01321    bfd *bfdobj;           /* bfd.h */
01322    Dl_info dli;           /* dlfcn.h */
01323    long allocsize;
01324    asymbol **syms = NULL; /* bfd.h */
01325    bfd_vma offset;        /* bfd.h */
01326    const char *lastslash;
01327    asection *section;
01328    const char *file, *func;
01329    unsigned int line;
01330    char address_str[128];
01331    char msg[1024];
01332    size_t strings_size;
01333    size_t *eachlen;
01334 #endif
01335 
01336 #if defined(BETTER_BACKTRACES)
01337    strings_size = num_frames * sizeof(*strings);
01338    eachlen = ast_calloc(num_frames, sizeof(*eachlen));
01339 
01340    if (!(strings = ast_calloc(num_frames, sizeof(*strings)))) {
01341       return NULL;
01342    }
01343 
01344    for (stackfr = 0; stackfr < num_frames; stackfr++) {
01345       int found = 0, symbolcount;
01346 
01347       msg[0] = '\0';
01348 
01349       if (!dladdr(addresses[stackfr], &dli)) {
01350          continue;
01351       }
01352 
01353       if (strcmp(dli.dli_fname, "asterisk") == 0) {
01354          char asteriskpath[256];
01355          if (!(dli.dli_fname = ast_utils_which("asterisk", asteriskpath, sizeof(asteriskpath)))) {
01356             /* This will fail to find symbols */
01357             ast_log(LOG_DEBUG, "Failed to find asterisk binary for debug symbols.\n");
01358             dli.dli_fname = "asterisk";
01359          }
01360       }
01361 
01362       lastslash = strrchr(dli.dli_fname, '/');
01363       if (  (bfdobj = bfd_openr(dli.dli_fname, NULL)) &&
01364             bfd_check_format(bfdobj, bfd_object) &&
01365             (allocsize = bfd_get_symtab_upper_bound(bfdobj)) > 0 &&
01366             (syms = ast_malloc(allocsize)) &&
01367             (symbolcount = bfd_canonicalize_symtab(bfdobj, syms))) {
01368 
01369          if (bfdobj->flags & DYNAMIC) {
01370             offset = addresses[stackfr] - dli.dli_fbase;
01371          } else {
01372             offset = addresses[stackfr] - (void *) 0;
01373          }
01374 
01375          for (section = bfdobj->sections; section; section = section->next) {
01376             if (  !bfd_get_section_flags(bfdobj, section) & SEC_ALLOC ||
01377                   section->vma > offset ||
01378                   section->size + section->vma < offset) {
01379                continue;
01380             }
01381 
01382             if (!bfd_find_nearest_line(bfdobj, section, syms, offset - section->vma, &file, &func, &line)) {
01383                continue;
01384             }
01385 
01386             /* Stack trace output */
01387             found++;
01388             if ((lastslash = strrchr(file, '/'))) {
01389                const char *prevslash;
01390                for (prevslash = lastslash - 1; *prevslash != '/' && prevslash >= file; prevslash--);
01391                if (prevslash >= file) {
01392                   lastslash = prevslash;
01393                }
01394             }
01395             if (dli.dli_saddr == NULL) {
01396                address_str[0] = '\0';
01397             } else {
01398                snprintf(address_str, sizeof(address_str), " (%p+%lX)",
01399                   dli.dli_saddr,
01400                   (unsigned long) (addresses[stackfr] - dli.dli_saddr));
01401             }
01402             snprintf(msg, sizeof(msg), "%s:%u %s()%s",
01403                lastslash ? lastslash + 1 : file, line,
01404                S_OR(func, "???"),
01405                address_str);
01406 
01407             break; /* out of section iteration */
01408          }
01409       }
01410       if (bfdobj) {
01411          bfd_close(bfdobj);
01412          if (syms) {
01413             ast_free(syms);
01414          }
01415       }
01416 
01417       /* Default output, if we cannot find the information within BFD */
01418       if (!found) {
01419          if (dli.dli_saddr == NULL) {
01420             address_str[0] = '\0';
01421          } else {
01422             snprintf(address_str, sizeof(address_str), " (%p+%lX)",
01423                dli.dli_saddr,
01424                (unsigned long) (addresses[stackfr] - dli.dli_saddr));
01425          }
01426          snprintf(msg, sizeof(msg), "%s %s()%s",
01427             lastslash ? lastslash + 1 : dli.dli_fname,
01428             S_OR(dli.dli_sname, "<unknown>"),
01429             address_str);
01430       }
01431 
01432       if (!ast_strlen_zero(msg)) {
01433          char **tmp;
01434          eachlen[stackfr] = strlen(msg);
01435          if (!(tmp = ast_realloc(strings, strings_size + eachlen[stackfr] + 1))) {
01436             ast_free(strings);
01437             strings = NULL;
01438             break; /* out of stack frame iteration */
01439          }
01440          strings = tmp;
01441          strings[stackfr] = (char *) strings + strings_size;
01442          ast_copy_string(strings[stackfr], msg, eachlen[stackfr] + 1);
01443          strings_size += eachlen[stackfr] + 1;
01444       }
01445    }
01446 
01447    if (strings) {
01448       /* Recalculate the offset pointers */
01449       strings[0] = (char *) strings + num_frames * sizeof(*strings);
01450       for (stackfr = 1; stackfr < num_frames; stackfr++) {
01451          strings[stackfr] = strings[stackfr - 1] + eachlen[stackfr - 1] + 1;
01452       }
01453    }
01454 #else /* !defined(BETTER_BACKTRACES) */
01455    strings = backtrace_symbols(addresses, num_frames);
01456 #endif /* defined(BETTER_BACKTRACES) */
01457    return strings;
01458 }
01459 
01460 #endif /* HAVE_BKTR */
01461 
01462 void ast_backtrace(void)
01463 {
01464 #ifdef HAVE_BKTR
01465    struct ast_bt *bt;
01466    int i = 0;
01467    char **strings;
01468 
01469    if (!(bt = ast_bt_create())) {
01470       ast_log(LOG_WARNING, "Unable to allocate space for backtrace structure\n");
01471       return;
01472    }
01473 
01474    if ((strings = ast_bt_get_symbols(bt->addresses, bt->num_frames))) {
01475       ast_debug(1, "Got %d backtrace record%c\n", bt->num_frames, bt->num_frames != 1 ? 's' : ' ');
01476       for (i = 3; i < bt->num_frames - 2; i++) {
01477          ast_log(LOG_DEBUG, "#%d: [%p] %s\n", i - 3, bt->addresses[i], strings[i]);
01478       }
01479 
01480       /* MALLOC_DEBUG will erroneously report an error here, unless we undef the macro. */
01481 #undef free
01482       free(strings);
01483    } else {
01484       ast_debug(1, "Could not allocate memory for backtrace\n");
01485    }
01486    ast_bt_destroy(bt);
01487 #else
01488    ast_log(LOG_WARNING, "Must run configure with '--with-execinfo' for stack backtraces.\n");
01489 #endif /* defined(HAVE_BKTR) */
01490 }
01491 
01492 void __ast_verbose_ap(const char *file, int line, const char *func, const char *fmt, va_list ap)
01493 {
01494    struct ast_str *buf = NULL;
01495    int res = 0;
01496 
01497    if (!(buf = ast_str_thread_get(&verbose_buf, VERBOSE_BUF_INIT_SIZE)))
01498       return;
01499 
01500    if (ast_opt_timestamp) {
01501       struct timeval now;
01502       struct ast_tm tm;
01503       char date[40];
01504       char *datefmt;
01505 
01506       now = ast_tvnow();
01507       ast_localtime(&now, &tm, NULL);
01508       ast_strftime(date, sizeof(date), dateformat, &tm);
01509       datefmt = alloca(strlen(date) + 3 + strlen(fmt) + 1);
01510       sprintf(datefmt, "%c[%s] %s", 127, date, fmt);
01511       fmt = datefmt;
01512    } else {
01513       char *tmp = alloca(strlen(fmt) + 2);
01514       sprintf(tmp, "%c%s", 127, fmt);
01515       fmt = tmp;
01516    }
01517 
01518    /* Build string */
01519    res = ast_str_set_va(&buf, 0, fmt, ap);
01520 
01521    /* If the build failed then we can drop this allocated message */
01522    if (res == AST_DYNSTR_BUILD_FAILED)
01523       return;
01524 
01525    ast_log(__LOG_VERBOSE, file, line, func, "%s", ast_str_buffer(buf));
01526 }
01527 
01528 void __ast_verbose(const char *file, int line, const char *func, const char *fmt, ...)
01529 {
01530    va_list ap;
01531 
01532    va_start(ap, fmt);
01533    __ast_verbose_ap(file, line, func, fmt, ap);
01534    va_end(ap);
01535 }
01536 
01537 /* No new code should use this directly, but we have the ABI for backwards compat */
01538 #undef ast_verbose
01539 void __attribute__((format(printf, 1,2))) ast_verbose(const char *fmt, ...);
01540 void ast_verbose(const char *fmt, ...)
01541 {
01542    va_list ap;
01543 
01544    va_start(ap, fmt);
01545    __ast_verbose_ap("", 0, "", fmt, ap);
01546    va_end(ap);
01547 }
01548 
01549 int ast_register_verbose(void (*v)(const char *string)) 
01550 {
01551    struct verb *verb;
01552 
01553    if (!(verb = ast_malloc(sizeof(*verb))))
01554       return -1;
01555 
01556    verb->verboser = v;
01557 
01558    AST_RWLIST_WRLOCK(&verbosers);
01559    AST_RWLIST_INSERT_HEAD(&verbosers, verb, list);
01560    AST_RWLIST_UNLOCK(&verbosers);
01561    
01562    return 0;
01563 }
01564 
01565 int ast_unregister_verbose(void (*v)(const char *string))
01566 {
01567    struct verb *cur;
01568 
01569    AST_RWLIST_WRLOCK(&verbosers);
01570    AST_RWLIST_TRAVERSE_SAFE_BEGIN(&verbosers, cur, list) {
01571       if (cur->verboser == v) {
01572          AST_RWLIST_REMOVE_CURRENT(list);
01573          ast_free(cur);
01574          break;
01575       }
01576    }
01577    AST_RWLIST_TRAVERSE_SAFE_END;
01578    AST_RWLIST_UNLOCK(&verbosers);
01579    
01580    return cur ? 0 : -1;
01581 }
01582 
01583 static void update_logchannels(void)
01584 {
01585    struct logchannel *cur;
01586 
01587    AST_RWLIST_WRLOCK(&logchannels);
01588 
01589    global_logmask = 0;
01590 
01591    AST_RWLIST_TRAVERSE(&logchannels, cur, list) {
01592       cur->logmask = make_components(cur->components, cur->lineno);
01593       global_logmask |= cur->logmask;
01594    }
01595 
01596    AST_RWLIST_UNLOCK(&logchannels);
01597 }
01598 
01599 int ast_logger_register_level(const char *name)
01600 {
01601    unsigned int level;
01602    unsigned int available = 0;
01603 
01604    AST_RWLIST_WRLOCK(&logchannels);
01605 
01606    for (level = 0; level < ARRAY_LEN(levels); level++) {
01607       if ((level >= 16) && !available && !levels[level]) {
01608          available = level;
01609          continue;
01610       }
01611 
01612       if (levels[level] && !strcasecmp(levels[level], name)) {
01613          ast_log(LOG_WARNING,
01614             "Unable to register dynamic logger level '%s': a standard logger level uses that name.\n",
01615             name);
01616          AST_RWLIST_UNLOCK(&logchannels);
01617 
01618          return -1;
01619       }
01620    }
01621 
01622    if (!available) {
01623       ast_log(LOG_WARNING,
01624          "Unable to register dynamic logger level '%s'; maximum number of levels registered.\n",
01625          name);
01626       AST_RWLIST_UNLOCK(&logchannels);
01627 
01628       return -1;
01629    }
01630 
01631    levels[available] = ast_strdup(name);
01632 
01633    AST_RWLIST_UNLOCK(&logchannels);
01634 
01635    ast_debug(1, "Registered dynamic logger level '%s' with index %d.\n", name, available);
01636 
01637    update_logchannels();
01638 
01639    return available;
01640 }
01641 
01642 void ast_logger_unregister_level(const char *name)
01643 {
01644    unsigned int found = 0;
01645    unsigned int x;
01646 
01647    AST_RWLIST_WRLOCK(&logchannels);
01648 
01649    for (x = 16; x < ARRAY_LEN(levels); x++) {
01650       if (!levels[x]) {
01651          continue;
01652       }
01653 
01654       if (strcasecmp(levels[x], name)) {
01655          continue;
01656       }
01657 
01658       found = 1;
01659       break;
01660    }
01661 
01662    if (found) {
01663       /* take this level out of the global_logmask, to ensure that no new log messages
01664        * will be queued for it
01665        */
01666 
01667       global_logmask &= ~(1 << x);
01668 
01669       ast_free(levels[x]);
01670       levels[x] = NULL;
01671       AST_RWLIST_UNLOCK(&logchannels);
01672 
01673       ast_debug(1, "Unregistered dynamic logger level '%s' with index %d.\n", name, x);
01674 
01675       update_logchannels();
01676    } else {
01677       AST_RWLIST_UNLOCK(&logchannels);
01678    }
01679 }
01680