Sun Oct 16 2011 08:41:42

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: 331649 $")
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[32] = {
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[32] = {
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       pbx_builtin_setvar_helper(c, "filename", filename);
00650       pbx_substitute_variables_helper(c, exec_after_rotate, buf, sizeof(buf));
00651       if (ast_safe_system(buf) == -1) {
00652          ast_log(LOG_WARNING, "error executing '%s'\n", buf);
00653       }
00654       c = ast_channel_release(c);
00655    }
00656    return res;
00657 }
00658 
00659 /*!
00660  * \internal
00661  * \brief Start the realtime queue logging if configured.
00662  *
00663  * \retval TRUE if not to open queue log file.
00664  */
00665 static int logger_queue_rt_start(void)
00666 {
00667    if (ast_check_realtime("queue_log")) {
00668       if (!ast_realtime_require_field("queue_log",
00669          "time", RQ_DATETIME, 26,
00670          "data1", RQ_CHAR, 20,
00671          "data2", RQ_CHAR, 20,
00672          "data3", RQ_CHAR, 20,
00673          "data4", RQ_CHAR, 20,
00674          "data5", RQ_CHAR, 20,
00675          SENTINEL)) {
00676          logfiles.queue_adaptive_realtime = 1;
00677       } else {
00678          logfiles.queue_adaptive_realtime = 0;
00679       }
00680 
00681       if (!logfiles.queue_log_to_file) {
00682          /* Don't open the log file. */
00683          return 1;
00684       }
00685    }
00686    return 0;
00687 }
00688 
00689 /*!
00690  * \internal
00691  * \brief Rotate the queue log file and restart.
00692  *
00693  * \param queue_rotate Log queue rotation mode.
00694  *
00695  * \note Assumes logchannels is write locked on entry.
00696  *
00697  * \retval 0 on success.
00698  * \retval -1 on error.
00699  */
00700 static int logger_queue_restart(int queue_rotate)
00701 {
00702    int res = 0;
00703    char qfname[PATH_MAX];
00704 
00705    if (logger_queue_rt_start()) {
00706       return res;
00707    }
00708 
00709    snprintf(qfname, sizeof(qfname), "%s/%s", ast_config_AST_LOG_DIR, queue_log_name);
00710    if (qlog) {
00711       /* Just in case it was still open. */
00712       fclose(qlog);
00713       qlog = NULL;
00714    }
00715    if (queue_rotate) {
00716       rotate_file(qfname);
00717    }
00718 
00719    /* Open the log file. */
00720    qlog = fopen(qfname, "a");
00721    if (!qlog) {
00722       ast_log(LOG_ERROR, "Unable to create queue log: %s\n", strerror(errno));
00723       res = -1;
00724    }
00725    return res;
00726 }
00727 
00728 static int reload_logger(int rotate)
00729 {
00730    int queue_rotate = rotate;
00731    struct logchannel *f;
00732    int res = 0;
00733 
00734    AST_RWLIST_WRLOCK(&logchannels);
00735 
00736    if (qlog) {
00737       if (rotate < 0) {
00738          /* Check filesize - this one typically doesn't need an auto-rotate */
00739          if (ftello(qlog) > 0x40000000) { /* Arbitrarily, 1 GB */
00740             fclose(qlog);
00741             qlog = NULL;
00742          } else {
00743             queue_rotate = 0;
00744          }
00745       } else {
00746          fclose(qlog);
00747          qlog = NULL;
00748       }
00749    } else {
00750       queue_rotate = 0;
00751    }
00752 
00753    ast_mkdir(ast_config_AST_LOG_DIR, 0777);
00754 
00755    AST_RWLIST_TRAVERSE(&logchannels, f, list) {
00756       if (f->disabled) {
00757          f->disabled = 0;  /* Re-enable logging at reload */
00758          manager_event(EVENT_FLAG_SYSTEM, "LogChannel", "Channel: %s\r\nEnabled: Yes\r\n", f->filename);
00759       }
00760       if (f->fileptr && (f->fileptr != stdout) && (f->fileptr != stderr)) {
00761          int rotate_this = 0;
00762          if (ftello(f->fileptr) > 0x40000000) { /* Arbitrarily, 1 GB */
00763             /* Be more proactive about rotating massive log files */
00764             rotate_this = 1;
00765          }
00766          fclose(f->fileptr);  /* Close file */
00767          f->fileptr = NULL;
00768          if (rotate || rotate_this) {
00769             rotate_file(f->filename);
00770          }
00771       }
00772    }
00773 
00774    filesize_reload_needed = 0;
00775 
00776    init_logger_chain(1 /* locked */);
00777 
00778    ast_unload_realtime("queue_log");
00779    if (logfiles.queue_log) {
00780       res = logger_queue_restart(queue_rotate);
00781       AST_RWLIST_UNLOCK(&logchannels);
00782       ast_queue_log("NONE", "NONE", "NONE", "CONFIGRELOAD", "%s", "");
00783       ast_verb(1, "Asterisk Queue Logger restarted\n");
00784    } else {
00785       AST_RWLIST_UNLOCK(&logchannels);
00786    }
00787 
00788    return res;
00789 }
00790 
00791 /*! \brief Reload the logger module without rotating log files (also used from loader.c during
00792    a full Asterisk reload) */
00793 int logger_reload(void)
00794 {
00795    if (reload_logger(0)) {
00796       return RESULT_FAILURE;
00797    }
00798    return RESULT_SUCCESS;
00799 }
00800 
00801 static char *handle_logger_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00802 {
00803    switch (cmd) {
00804    case CLI_INIT:
00805       e->command = "logger reload";
00806       e->usage = 
00807          "Usage: logger reload\n"
00808          "       Reloads the logger subsystem state.  Use after restarting syslogd(8) if you are using syslog logging.\n";
00809       return NULL;
00810    case CLI_GENERATE:
00811       return NULL;
00812    }
00813    if (reload_logger(0)) {
00814       ast_cli(a->fd, "Failed to reload the logger\n");
00815       return CLI_FAILURE;
00816    }
00817    return CLI_SUCCESS;
00818 }
00819 
00820 static char *handle_logger_rotate(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00821 {
00822    switch (cmd) {
00823    case CLI_INIT:
00824       e->command = "logger rotate";
00825       e->usage = 
00826          "Usage: logger rotate\n"
00827          "       Rotates and Reopens the log files.\n";
00828       return NULL;
00829    case CLI_GENERATE:
00830       return NULL;   
00831    }
00832    if (reload_logger(1)) {
00833       ast_cli(a->fd, "Failed to reload the logger and rotate log files\n");
00834       return CLI_FAILURE;
00835    } 
00836    return CLI_SUCCESS;
00837 }
00838 
00839 static char *handle_logger_set_level(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00840 {
00841    int x;
00842    int state;
00843    int level = -1;
00844 
00845    switch (cmd) {
00846    case CLI_INIT:
00847       e->command = "logger set level {DEBUG|NOTICE|WARNING|ERROR|VERBOSE|DTMF} {on|off}";
00848       e->usage = 
00849          "Usage: logger set level {DEBUG|NOTICE|WARNING|ERROR|VERBOSE|DTMF} {on|off}\n"
00850          "       Set a specific log level to enabled/disabled for this console.\n";
00851       return NULL;
00852    case CLI_GENERATE:
00853       return NULL;
00854    }
00855 
00856    if (a->argc < 5)
00857       return CLI_SHOWUSAGE;
00858 
00859    AST_RWLIST_WRLOCK(&logchannels);
00860 
00861    for (x = 0; x < ARRAY_LEN(levels); x++) {
00862       if (levels[x] && !strcasecmp(a->argv[3], levels[x])) {
00863          level = x;
00864          break;
00865       }
00866    }
00867 
00868    AST_RWLIST_UNLOCK(&logchannels);
00869 
00870    state = ast_true(a->argv[4]) ? 1 : 0;
00871 
00872    if (level != -1) {
00873       ast_console_toggle_loglevel(a->fd, level, state);
00874       ast_cli(a->fd, "Logger status for '%s' has been set to '%s'.\n", levels[level], state ? "on" : "off");
00875    } else
00876       return CLI_SHOWUSAGE;
00877 
00878    return CLI_SUCCESS;
00879 }
00880 
00881 /*! \brief CLI command to show logging system configuration */
00882 static char *handle_logger_show_channels(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00883 {
00884 #define FORMATL   "%-35.35s %-8.8s %-9.9s "
00885    struct logchannel *chan;
00886    switch (cmd) {
00887    case CLI_INIT:
00888       e->command = "logger show channels";
00889       e->usage = 
00890          "Usage: logger show channels\n"
00891          "       List configured logger channels.\n";
00892       return NULL;
00893    case CLI_GENERATE:
00894       return NULL;   
00895    }
00896    ast_cli(a->fd, FORMATL, "Channel", "Type", "Status");
00897    ast_cli(a->fd, "Configuration\n");
00898    ast_cli(a->fd, FORMATL, "-------", "----", "------");
00899    ast_cli(a->fd, "-------------\n");
00900    AST_RWLIST_RDLOCK(&logchannels);
00901    AST_RWLIST_TRAVERSE(&logchannels, chan, list) {
00902       unsigned int level;
00903 
00904       ast_cli(a->fd, FORMATL, chan->filename, chan->type == LOGTYPE_CONSOLE ? "Console" : (chan->type == LOGTYPE_SYSLOG ? "Syslog" : "File"),
00905          chan->disabled ? "Disabled" : "Enabled");
00906       ast_cli(a->fd, " - ");
00907       for (level = 0; level < ARRAY_LEN(levels); level++) {
00908          if ((chan->logmask & (1 << level)) && levels[level]) {
00909             ast_cli(a->fd, "%s ", levels[level]);
00910          }
00911       }
00912       ast_cli(a->fd, "\n");
00913    }
00914    AST_RWLIST_UNLOCK(&logchannels);
00915    ast_cli(a->fd, "\n");
00916 
00917    return CLI_SUCCESS;
00918 }
00919 
00920 struct verb {
00921    void (*verboser)(const char *string);
00922    AST_LIST_ENTRY(verb) list;
00923 };
00924 
00925 static AST_RWLIST_HEAD_STATIC(verbosers, verb);
00926 
00927 static struct ast_cli_entry cli_logger[] = {
00928    AST_CLI_DEFINE(handle_logger_show_channels, "List configured log channels"),
00929    AST_CLI_DEFINE(handle_logger_reload, "Reopens the log files"),
00930    AST_CLI_DEFINE(handle_logger_rotate, "Rotates and reopens the log files"),
00931    AST_CLI_DEFINE(handle_logger_set_level, "Enables/Disables a specific logging level for this console")
00932 };
00933 
00934 static void _handle_SIGXFSZ(int sig)
00935 {
00936    /* Indicate need to reload */
00937    filesize_reload_needed = 1;
00938 }
00939 
00940 static struct sigaction handle_SIGXFSZ = {
00941    .sa_handler = _handle_SIGXFSZ,
00942    .sa_flags = SA_RESTART,
00943 };
00944 
00945 static void ast_log_vsyslog(struct logmsg *msg)
00946 {
00947    char buf[BUFSIZ];
00948    int syslog_level = ast_syslog_priority_from_loglevel(msg->level);
00949 
00950    if (syslog_level < 0) {
00951       /* we are locked here, so cannot ast_log() */
00952       fprintf(stderr, "ast_log_vsyslog called with bogus level: %d\n", msg->level);
00953       return;
00954    }
00955 
00956    snprintf(buf, sizeof(buf), "%s[%ld]: %s:%d in %s: %s",
00957        levels[msg->level], msg->process_id, msg->file, msg->line, msg->function, msg->message);
00958 
00959    term_strip(buf, buf, strlen(buf) + 1);
00960    syslog(syslog_level, "%s", buf);
00961 }
00962 
00963 /*! \brief Print a normal log message to the channels */
00964 static void logger_print_normal(struct logmsg *logmsg)
00965 {
00966    struct logchannel *chan = NULL;
00967    char buf[BUFSIZ];
00968    struct verb *v = NULL;
00969 
00970    if (logmsg->level == __LOG_VERBOSE) {
00971       char *tmpmsg = ast_strdupa(logmsg->message + 1);
00972       /* Iterate through the list of verbosers and pass them the log message string */
00973       AST_RWLIST_RDLOCK(&verbosers);
00974       AST_RWLIST_TRAVERSE(&verbosers, v, list)
00975          v->verboser(logmsg->message);
00976       AST_RWLIST_UNLOCK(&verbosers);
00977       ast_string_field_set(logmsg, message, tmpmsg);
00978    }
00979 
00980    AST_RWLIST_RDLOCK(&logchannels);
00981 
00982    if (!AST_RWLIST_EMPTY(&logchannels)) {
00983       AST_RWLIST_TRAVERSE(&logchannels, chan, list) {
00984          /* If the channel is disabled, then move on to the next one */
00985          if (chan->disabled)
00986             continue;
00987          /* Check syslog channels */
00988          if (chan->type == LOGTYPE_SYSLOG && (chan->logmask & (1 << logmsg->level))) {
00989             ast_log_vsyslog(logmsg);
00990          /* Console channels */
00991          } else if (chan->type == LOGTYPE_CONSOLE && (chan->logmask & (1 << logmsg->level))) {
00992             char linestr[128];
00993             char tmp1[80], tmp2[80], tmp3[80], tmp4[80];
00994 
00995             /* If the level is verbose, then skip it */
00996             if (logmsg->level == __LOG_VERBOSE)
00997                continue;
00998 
00999             /* Turn the numerical line number into a string */
01000             snprintf(linestr, sizeof(linestr), "%d", logmsg->line);
01001             /* Build string to print out */
01002             snprintf(buf, sizeof(buf), "[%s] %s[%ld]: %s:%s %s: %s",
01003                 logmsg->date,
01004                 term_color(tmp1, logmsg->level_name, colors[logmsg->level], 0, sizeof(tmp1)),
01005                 logmsg->process_id,
01006                 term_color(tmp2, logmsg->file, COLOR_BRWHITE, 0, sizeof(tmp2)),
01007                 term_color(tmp3, linestr, COLOR_BRWHITE, 0, sizeof(tmp3)),
01008                 term_color(tmp4, logmsg->function, COLOR_BRWHITE, 0, sizeof(tmp4)),
01009                 logmsg->message);
01010             /* Print out */
01011             ast_console_puts_mutable(buf, logmsg->level);
01012          /* File channels */
01013          } else if (chan->type == LOGTYPE_FILE && (chan->logmask & (1 << logmsg->level))) {
01014             int res = 0;
01015 
01016             /* If no file pointer exists, skip it */
01017             if (!chan->fileptr) {
01018                continue;
01019             }
01020 
01021             /* Print out to the file */
01022             res = fprintf(chan->fileptr, "[%s] %s[%ld] %s: %s",
01023                      logmsg->date, logmsg->level_name, logmsg->process_id, logmsg->file, term_strip(buf, logmsg->message, BUFSIZ));
01024             if (res <= 0 && !ast_strlen_zero(logmsg->message)) {
01025                fprintf(stderr, "**** Asterisk Logging Error: ***********\n");
01026                if (errno == ENOMEM || errno == ENOSPC)
01027                   fprintf(stderr, "Asterisk logging error: Out of disk space, can't log to log file %s\n", chan->filename);
01028                else
01029                   fprintf(stderr, "Logger Warning: Unable to write to log file '%s': %s (disabled)\n", chan->filename, strerror(errno));
01030                manager_event(EVENT_FLAG_SYSTEM, "LogChannel", "Channel: %s\r\nEnabled: No\r\nReason: %d - %s\r\n", chan->filename, errno, strerror(errno));
01031                chan->disabled = 1;
01032             } else if (res > 0) {
01033                fflush(chan->fileptr);
01034             }
01035          }
01036       }
01037    } else if (logmsg->level != __LOG_VERBOSE) {
01038       fputs(logmsg->message, stdout);
01039    }
01040 
01041    AST_RWLIST_UNLOCK(&logchannels);
01042 
01043    /* If we need to reload because of the file size, then do so */
01044    if (filesize_reload_needed) {
01045       reload_logger(-1);
01046       ast_verb(1, "Rotated Logs Per SIGXFSZ (Exceeded file size limit)\n");
01047    }
01048 
01049    return;
01050 }
01051 
01052 /*! \brief Actual logging thread */
01053 static void *logger_thread(void *data)
01054 {
01055    struct logmsg *next = NULL, *msg = NULL;
01056 
01057    for (;;) {
01058       /* We lock the message list, and see if any message exists... if not we wait on the condition to be signalled */
01059       AST_LIST_LOCK(&logmsgs);
01060       if (AST_LIST_EMPTY(&logmsgs)) {
01061          if (close_logger_thread) {
01062             break;
01063          } else {
01064             ast_cond_wait(&logcond, &logmsgs.lock);
01065          }
01066       }
01067       next = AST_LIST_FIRST(&logmsgs);
01068       AST_LIST_HEAD_INIT_NOLOCK(&logmsgs);
01069       AST_LIST_UNLOCK(&logmsgs);
01070 
01071       /* Otherwise go through and process each message in the order added */
01072       while ((msg = next)) {
01073          /* Get the next entry now so that we can free our current structure later */
01074          next = AST_LIST_NEXT(msg, list);
01075 
01076          /* Depending on the type, send it to the proper function */
01077          logger_print_normal(msg);
01078 
01079          /* Free the data since we are done */
01080          ast_free(msg);
01081       }
01082 
01083       /* If we should stop, then stop */
01084       if (close_logger_thread)
01085          break;
01086    }
01087 
01088    return NULL;
01089 }
01090 
01091 /*!
01092  * \internal
01093  * \brief Initialize the logger queue.
01094  *
01095  * \note Assumes logchannels is write locked on entry.
01096  *
01097  * \return Nothing
01098  */
01099 static void logger_queue_init(void)
01100 {
01101    ast_unload_realtime("queue_log");
01102    if (logfiles.queue_log) {
01103       char qfname[PATH_MAX];
01104 
01105       if (logger_queue_rt_start()) {
01106          return;
01107       }
01108 
01109       /* Open the log file. */
01110       snprintf(qfname, sizeof(qfname), "%s/%s", ast_config_AST_LOG_DIR,
01111          queue_log_name);
01112       if (qlog) {
01113          /* Just in case it was already open. */
01114          fclose(qlog);
01115       }
01116       qlog = fopen(qfname, "a");
01117       if (!qlog) {
01118          ast_log(LOG_ERROR, "Unable to create queue log: %s\n", strerror(errno));
01119       }
01120    }
01121 }
01122 
01123 int init_logger(void)
01124 {
01125    /* auto rotate if sig SIGXFSZ comes a-knockin */
01126    sigaction(SIGXFSZ, &handle_SIGXFSZ, NULL);
01127 
01128    /* start logger thread */
01129    ast_cond_init(&logcond, NULL);
01130    if (ast_pthread_create(&logthread, NULL, logger_thread, NULL) < 0) {
01131       ast_cond_destroy(&logcond);
01132       return -1;
01133    }
01134 
01135    /* register the logger cli commands */
01136    ast_cli_register_multiple(cli_logger, ARRAY_LEN(cli_logger));
01137 
01138    ast_mkdir(ast_config_AST_LOG_DIR, 0777);
01139 
01140    /* create log channels */
01141    init_logger_chain(0 /* locked */);
01142    logger_initialized = 1;
01143 
01144    return 0;
01145 }
01146 
01147 void close_logger(void)
01148 {
01149    struct logchannel *f = NULL;
01150 
01151    logger_initialized = 0;
01152 
01153    /* Stop logger thread */
01154    AST_LIST_LOCK(&logmsgs);
01155    close_logger_thread = 1;
01156    ast_cond_signal(&logcond);
01157    AST_LIST_UNLOCK(&logmsgs);
01158 
01159    if (logthread != AST_PTHREADT_NULL)
01160       pthread_join(logthread, NULL);
01161 
01162    AST_RWLIST_WRLOCK(&logchannels);
01163 
01164    if (qlog) {
01165       fclose(qlog);
01166       qlog = NULL;
01167    }
01168 
01169    AST_RWLIST_TRAVERSE(&logchannels, f, list) {
01170       if (f->fileptr && (f->fileptr != stdout) && (f->fileptr != stderr)) {
01171          fclose(f->fileptr);
01172          f->fileptr = NULL;
01173       }
01174    }
01175 
01176    closelog(); /* syslog */
01177 
01178    AST_RWLIST_UNLOCK(&logchannels);
01179 
01180    return;
01181 }
01182 
01183 /*!
01184  * \brief send log messages to syslog and/or the console
01185  */
01186 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
01187 {
01188    struct logmsg *logmsg = NULL;
01189    struct ast_str *buf = NULL;
01190    struct ast_tm tm;
01191    struct timeval now = ast_tvnow();
01192    int res = 0;
01193    va_list ap;
01194    char datestring[256];
01195 
01196    if (!(buf = ast_str_thread_get(&log_buf, LOG_BUF_INIT_SIZE)))
01197       return;
01198 
01199    if (level != __LOG_VERBOSE && AST_RWLIST_EMPTY(&logchannels)) {
01200       /*
01201        * we don't have the logger chain configured yet,
01202        * so just log to stdout
01203        */
01204       int result;
01205       va_start(ap, fmt);
01206       result = ast_str_set_va(&buf, BUFSIZ, fmt, ap); /* XXX BUFSIZ ? */
01207       va_end(ap);
01208       if (result != AST_DYNSTR_BUILD_FAILED) {
01209          term_filter_escapes(ast_str_buffer(buf));
01210          fputs(ast_str_buffer(buf), stdout);
01211       }
01212       return;
01213    }
01214    
01215    /* don't display LOG_DEBUG messages unless option_verbose _or_ option_debug
01216       are non-zero; LOG_DEBUG messages can still be displayed if option_debug
01217       is zero, if option_verbose is non-zero (this allows for 'level zero'
01218       LOG_DEBUG messages to be displayed, if the logmask on any channel
01219       allows it)
01220    */
01221    if (!option_verbose && !option_debug && (level == __LOG_DEBUG))
01222       return;
01223 
01224    /* Ignore anything that never gets logged anywhere */
01225    if (level != __LOG_VERBOSE && !(global_logmask & (1 << level)))
01226       return;
01227    
01228    /* Build string */
01229    va_start(ap, fmt);
01230    res = ast_str_set_va(&buf, BUFSIZ, fmt, ap);
01231    va_end(ap);
01232 
01233    /* If the build failed, then abort and free this structure */
01234    if (res == AST_DYNSTR_BUILD_FAILED)
01235       return;
01236 
01237    /* Create a new logging message */
01238    if (!(logmsg = ast_calloc_with_stringfields(1, struct logmsg, res + 128)))
01239       return;
01240 
01241    /* Copy string over */
01242    ast_string_field_set(logmsg, message, ast_str_buffer(buf));
01243 
01244    /* Set type */
01245    if (level == __LOG_VERBOSE) {
01246       logmsg->type = LOGMSG_VERBOSE;
01247    } else {
01248       logmsg->type = LOGMSG_NORMAL;
01249    }
01250 
01251    /* Create our date/time */
01252    ast_localtime(&now, &tm, NULL);
01253    ast_strftime(datestring, sizeof(datestring), dateformat, &tm);
01254    ast_string_field_set(logmsg, date, datestring);
01255 
01256    /* Copy over data */
01257    logmsg->level = level;
01258    logmsg->line = line;
01259    ast_string_field_set(logmsg, level_name, levels[level]);
01260    ast_string_field_set(logmsg, file, file);
01261    ast_string_field_set(logmsg, function, function);
01262    logmsg->process_id = (long) GETTID();
01263 
01264    /* If the logger thread is active, append it to the tail end of the list - otherwise skip that step */
01265    if (logthread != AST_PTHREADT_NULL) {
01266       AST_LIST_LOCK(&logmsgs);
01267       AST_LIST_INSERT_TAIL(&logmsgs, logmsg, list);
01268       ast_cond_signal(&logcond);
01269       AST_LIST_UNLOCK(&logmsgs);
01270    } else {
01271       logger_print_normal(logmsg);
01272       ast_free(logmsg);
01273    }
01274 
01275    return;
01276 }
01277 
01278 #ifdef HAVE_BKTR
01279 
01280 struct ast_bt *ast_bt_create(void) 
01281 {
01282    struct ast_bt *bt = ast_calloc(1, sizeof(*bt));
01283    if (!bt) {
01284       ast_log(LOG_ERROR, "Unable to allocate memory for backtrace structure!\n");
01285       return NULL;
01286    }
01287 
01288    bt->alloced = 1;
01289 
01290    ast_bt_get_addresses(bt);
01291 
01292    return bt;
01293 }
01294 
01295 int ast_bt_get_addresses(struct ast_bt *bt)
01296 {
01297    bt->num_frames = backtrace(bt->addresses, AST_MAX_BT_FRAMES);
01298 
01299    return 0;
01300 }
01301 
01302 void *ast_bt_destroy(struct ast_bt *bt)
01303 {
01304    if (bt->alloced) {
01305       ast_free(bt);
01306    }
01307 
01308    return NULL;
01309 }
01310 
01311 char **ast_bt_get_symbols(void **addresses, size_t num_frames)
01312 {
01313    char **strings = NULL;
01314 #if defined(BETTER_BACKTRACES)
01315    int stackfr;
01316    bfd *bfdobj;           /* bfd.h */
01317    Dl_info dli;           /* dlfcn.h */
01318    long allocsize;
01319    asymbol **syms = NULL; /* bfd.h */
01320    bfd_vma offset;        /* bfd.h */
01321    const char *lastslash;
01322    asection *section;
01323    const char *file, *func;
01324    unsigned int line;
01325    char address_str[128];
01326    char msg[1024];
01327    size_t strings_size;
01328    size_t *eachlen;
01329 #endif
01330 
01331 #if defined(BETTER_BACKTRACES)
01332    strings_size = num_frames * sizeof(*strings);
01333    eachlen = ast_calloc(num_frames, sizeof(*eachlen));
01334 
01335    if (!(strings = ast_calloc(num_frames, sizeof(*strings)))) {
01336       return NULL;
01337    }
01338 
01339    for (stackfr = 0; stackfr < num_frames; stackfr++) {
01340       int found = 0, symbolcount;
01341 
01342       msg[0] = '\0';
01343 
01344       if (!dladdr(addresses[stackfr], &dli)) {
01345          continue;
01346       }
01347 
01348       if (strcmp(dli.dli_fname, "asterisk") == 0) {
01349          char asteriskpath[256];
01350          if (!(dli.dli_fname = ast_utils_which("asterisk", asteriskpath, sizeof(asteriskpath)))) {
01351             /* This will fail to find symbols */
01352             ast_log(LOG_DEBUG, "Failed to find asterisk binary for debug symbols.\n");
01353             dli.dli_fname = "asterisk";
01354          }
01355       }
01356 
01357       lastslash = strrchr(dli.dli_fname, '/');
01358       if (  (bfdobj = bfd_openr(dli.dli_fname, NULL)) &&
01359             bfd_check_format(bfdobj, bfd_object) &&
01360             (allocsize = bfd_get_symtab_upper_bound(bfdobj)) > 0 &&
01361             (syms = ast_malloc(allocsize)) &&
01362             (symbolcount = bfd_canonicalize_symtab(bfdobj, syms))) {
01363 
01364          if (bfdobj->flags & DYNAMIC) {
01365             offset = addresses[stackfr] - dli.dli_fbase;
01366          } else {
01367             offset = addresses[stackfr] - (void *) 0;
01368          }
01369 
01370          for (section = bfdobj->sections; section; section = section->next) {
01371             if (  !bfd_get_section_flags(bfdobj, section) & SEC_ALLOC ||
01372                   section->vma > offset ||
01373                   section->size + section->vma < offset) {
01374                continue;
01375             }
01376 
01377             if (!bfd_find_nearest_line(bfdobj, section, syms, offset - section->vma, &file, &func, &line)) {
01378                continue;
01379             }
01380 
01381             /* Stack trace output */
01382             found++;
01383             if ((lastslash = strrchr(file, '/'))) {
01384                const char *prevslash;
01385                for (prevslash = lastslash - 1; *prevslash != '/' && prevslash >= file; prevslash--);
01386                if (prevslash >= file) {
01387                   lastslash = prevslash;
01388                }
01389             }
01390             if (dli.dli_saddr == NULL) {
01391                address_str[0] = '\0';
01392             } else {
01393                snprintf(address_str, sizeof(address_str), " (%p+%lX)",
01394                   dli.dli_saddr,
01395                   (unsigned long) (addresses[stackfr] - dli.dli_saddr));
01396             }
01397             snprintf(msg, sizeof(msg), "%s:%u %s()%s",
01398                lastslash ? lastslash + 1 : file, line,
01399                S_OR(func, "???"),
01400                address_str);
01401 
01402             break; /* out of section iteration */
01403          }
01404       }
01405       if (bfdobj) {
01406          bfd_close(bfdobj);
01407          if (syms) {
01408             ast_free(syms);
01409          }
01410       }
01411 
01412       /* Default output, if we cannot find the information within BFD */
01413       if (!found) {
01414          if (dli.dli_saddr == NULL) {
01415             address_str[0] = '\0';
01416          } else {
01417             snprintf(address_str, sizeof(address_str), " (%p+%lX)",
01418                dli.dli_saddr,
01419                (unsigned long) (addresses[stackfr] - dli.dli_saddr));
01420          }
01421          snprintf(msg, sizeof(msg), "%s %s()%s",
01422             lastslash ? lastslash + 1 : dli.dli_fname,
01423             S_OR(dli.dli_sname, "<unknown>"),
01424             address_str);
01425       }
01426 
01427       if (!ast_strlen_zero(msg)) {
01428          char **tmp;
01429          eachlen[stackfr] = strlen(msg);
01430          if (!(tmp = ast_realloc(strings, strings_size + eachlen[stackfr] + 1))) {
01431             ast_free(strings);
01432             strings = NULL;
01433             break; /* out of stack frame iteration */
01434          }
01435          strings = tmp;
01436          strings[stackfr] = (char *) strings + strings_size;
01437          ast_copy_string(strings[stackfr], msg, eachlen[stackfr] + 1);
01438          strings_size += eachlen[stackfr] + 1;
01439       }
01440    }
01441 
01442    if (strings) {
01443       /* Recalculate the offset pointers */
01444       strings[0] = (char *) strings + num_frames * sizeof(*strings);
01445       for (stackfr = 1; stackfr < num_frames; stackfr++) {
01446          strings[stackfr] = strings[stackfr - 1] + eachlen[stackfr - 1] + 1;
01447       }
01448    }
01449 #else /* !defined(BETTER_BACKTRACES) */
01450    strings = backtrace_symbols(addresses, num_frames);
01451 #endif /* defined(BETTER_BACKTRACES) */
01452    return strings;
01453 }
01454 
01455 #endif /* HAVE_BKTR */
01456 
01457 void ast_backtrace(void)
01458 {
01459 #ifdef HAVE_BKTR
01460    struct ast_bt *bt;
01461    int i = 0;
01462    char **strings;
01463 
01464    if (!(bt = ast_bt_create())) {
01465       ast_log(LOG_WARNING, "Unable to allocate space for backtrace structure\n");
01466       return;
01467    }
01468 
01469    if ((strings = ast_bt_get_symbols(bt->addresses, bt->num_frames))) {
01470       ast_debug(1, "Got %d backtrace record%c\n", bt->num_frames, bt->num_frames != 1 ? 's' : ' ');
01471       for (i = 3; i < bt->num_frames - 2; i++) {
01472          ast_log(LOG_DEBUG, "#%d: [%p] %s\n", i - 3, bt->addresses[i], strings[i]);
01473       }
01474 
01475       /* MALLOC_DEBUG will erroneously report an error here, unless we undef the macro. */
01476 #undef free
01477       free(strings);
01478    } else {
01479       ast_debug(1, "Could not allocate memory for backtrace\n");
01480    }
01481    ast_bt_destroy(bt);
01482 #else
01483    ast_log(LOG_WARNING, "Must run configure with '--with-execinfo' for stack backtraces.\n");
01484 #endif /* defined(HAVE_BKTR) */
01485 }
01486 
01487 void __ast_verbose_ap(const char *file, int line, const char *func, const char *fmt, va_list ap)
01488 {
01489    struct ast_str *buf = NULL;
01490    int res = 0;
01491 
01492    if (!(buf = ast_str_thread_get(&verbose_buf, VERBOSE_BUF_INIT_SIZE)))
01493       return;
01494 
01495    if (ast_opt_timestamp) {
01496       struct timeval now;
01497       struct ast_tm tm;
01498       char date[40];
01499       char *datefmt;
01500 
01501       now = ast_tvnow();
01502       ast_localtime(&now, &tm, NULL);
01503       ast_strftime(date, sizeof(date), dateformat, &tm);
01504       datefmt = alloca(strlen(date) + 3 + strlen(fmt) + 1);
01505       sprintf(datefmt, "%c[%s] %s", 127, date, fmt);
01506       fmt = datefmt;
01507    } else {
01508       char *tmp = alloca(strlen(fmt) + 2);
01509       sprintf(tmp, "%c%s", 127, fmt);
01510       fmt = tmp;
01511    }
01512 
01513    /* Build string */
01514    res = ast_str_set_va(&buf, 0, fmt, ap);
01515 
01516    /* If the build failed then we can drop this allocated message */
01517    if (res == AST_DYNSTR_BUILD_FAILED)
01518       return;
01519 
01520    ast_log(__LOG_VERBOSE, file, line, func, "%s", ast_str_buffer(buf));
01521 }
01522 
01523 void __ast_verbose(const char *file, int line, const char *func, const char *fmt, ...)
01524 {
01525    va_list ap;
01526 
01527    va_start(ap, fmt);
01528    __ast_verbose_ap(file, line, func, fmt, ap);
01529    va_end(ap);
01530 }
01531 
01532 /* No new code should use this directly, but we have the ABI for backwards compat */
01533 #undef ast_verbose
01534 void __attribute__((format(printf, 1,2))) ast_verbose(const char *fmt, ...);
01535 void ast_verbose(const char *fmt, ...)
01536 {
01537    va_list ap;
01538 
01539    va_start(ap, fmt);
01540    __ast_verbose_ap("", 0, "", fmt, ap);
01541    va_end(ap);
01542 }
01543 
01544 int ast_register_verbose(void (*v)(const char *string)) 
01545 {
01546    struct verb *verb;
01547 
01548    if (!(verb = ast_malloc(sizeof(*verb))))
01549       return -1;
01550 
01551    verb->verboser = v;
01552 
01553    AST_RWLIST_WRLOCK(&verbosers);
01554    AST_RWLIST_INSERT_HEAD(&verbosers, verb, list);
01555    AST_RWLIST_UNLOCK(&verbosers);
01556    
01557    return 0;
01558 }
01559 
01560 int ast_unregister_verbose(void (*v)(const char *string))
01561 {
01562    struct verb *cur;
01563 
01564    AST_RWLIST_WRLOCK(&verbosers);
01565    AST_RWLIST_TRAVERSE_SAFE_BEGIN(&verbosers, cur, list) {
01566       if (cur->verboser == v) {
01567          AST_RWLIST_REMOVE_CURRENT(list);
01568          ast_free(cur);
01569          break;
01570       }
01571    }
01572    AST_RWLIST_TRAVERSE_SAFE_END;
01573    AST_RWLIST_UNLOCK(&verbosers);
01574    
01575    return cur ? 0 : -1;
01576 }
01577 
01578 static void update_logchannels(void)
01579 {
01580    struct logchannel *cur;
01581 
01582    AST_RWLIST_WRLOCK(&logchannels);
01583 
01584    global_logmask = 0;
01585 
01586    AST_RWLIST_TRAVERSE(&logchannels, cur, list) {
01587       cur->logmask = make_components(cur->components, cur->lineno);
01588       global_logmask |= cur->logmask;
01589    }
01590 
01591    AST_RWLIST_UNLOCK(&logchannels);
01592 }
01593 
01594 int ast_logger_register_level(const char *name)
01595 {
01596    unsigned int level;
01597    unsigned int available = 0;
01598 
01599    AST_RWLIST_WRLOCK(&logchannels);
01600 
01601    for (level = 0; level < ARRAY_LEN(levels); level++) {
01602       if ((level >= 16) && !available && !levels[level]) {
01603          available = level;
01604          continue;
01605       }
01606 
01607       if (levels[level] && !strcasecmp(levels[level], name)) {
01608          ast_log(LOG_WARNING,
01609             "Unable to register dynamic logger level '%s': a standard logger level uses that name.\n",
01610             name);
01611          AST_RWLIST_UNLOCK(&logchannels);
01612 
01613          return -1;
01614       }
01615    }
01616 
01617    if (!available) {
01618       ast_log(LOG_WARNING,
01619          "Unable to register dynamic logger level '%s'; maximum number of levels registered.\n",
01620          name);
01621       AST_RWLIST_UNLOCK(&logchannels);
01622 
01623       return -1;
01624    }
01625 
01626    levels[available] = ast_strdup(name);
01627 
01628    AST_RWLIST_UNLOCK(&logchannels);
01629 
01630    ast_debug(1, "Registered dynamic logger level '%s' with index %d.\n", name, available);
01631 
01632    update_logchannels();
01633 
01634    return available;
01635 }
01636 
01637 void ast_logger_unregister_level(const char *name)
01638 {
01639    unsigned int found = 0;
01640    unsigned int x;
01641 
01642    AST_RWLIST_WRLOCK(&logchannels);
01643 
01644    for (x = 16; x < ARRAY_LEN(levels); x++) {
01645       if (!levels[x]) {
01646          continue;
01647       }
01648 
01649       if (strcasecmp(levels[x], name)) {
01650          continue;
01651       }
01652 
01653       found = 1;
01654       break;
01655    }
01656 
01657    if (found) {
01658       /* take this level out of the global_logmask, to ensure that no new log messages
01659        * will be queued for it
01660        */
01661 
01662       global_logmask &= ~(1 << x);
01663 
01664       free(levels[x]);
01665       levels[x] = NULL;
01666       AST_RWLIST_UNLOCK(&logchannels);
01667 
01668       ast_debug(1, "Unregistered dynamic logger level '%s' with index %d.\n", name, x);
01669 
01670       update_logchannels();
01671    } else {
01672       AST_RWLIST_UNLOCK(&logchannels);
01673    }
01674 }
01675