Mon Mar 19 11:30:28 2012

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: 337973 $")
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 
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             break;
01066          } else {
01067             ast_cond_wait(&logcond, &logmsgs.lock);
01068          }
01069       }
01070       next = AST_LIST_FIRST(&logmsgs);
01071       AST_LIST_HEAD_INIT_NOLOCK(&logmsgs);
01072       AST_LIST_UNLOCK(&logmsgs);
01073 
01074       /* Otherwise go through and process each message in the order added */
01075       while ((msg = next)) {
01076          /* Get the next entry now so that we can free our current structure later */
01077          next = AST_LIST_NEXT(msg, list);
01078 
01079          /* Depending on the type, send it to the proper function */
01080          logger_print_normal(msg);
01081 
01082          /* Free the data since we are done */
01083          ast_free(msg);
01084       }
01085 
01086       /* If we should stop, then stop */
01087       if (close_logger_thread)
01088          break;
01089    }
01090 
01091    return NULL;
01092 }
01093 
01094 /*!
01095  * \internal
01096  * \brief Initialize the logger queue.
01097  *
01098  * \note Assumes logchannels is write locked on entry.
01099  *
01100  * \return Nothing
01101  */
01102 static void logger_queue_init(void)
01103 {
01104    ast_unload_realtime("queue_log");
01105    if (logfiles.queue_log) {
01106       char qfname[PATH_MAX];
01107 
01108       if (logger_queue_rt_start()) {
01109          return;
01110       }
01111 
01112       /* Open the log file. */
01113       snprintf(qfname, sizeof(qfname), "%s/%s", ast_config_AST_LOG_DIR,
01114          queue_log_name);
01115       if (qlog) {
01116          /* Just in case it was already open. */
01117          fclose(qlog);
01118       }
01119       qlog = fopen(qfname, "a");
01120       if (!qlog) {
01121          ast_log(LOG_ERROR, "Unable to create queue log: %s\n", strerror(errno));
01122       }
01123    }
01124 }
01125 
01126 int init_logger(void)
01127 {
01128    /* auto rotate if sig SIGXFSZ comes a-knockin */
01129    sigaction(SIGXFSZ, &handle_SIGXFSZ, NULL);
01130 
01131    /* start logger thread */
01132    ast_cond_init(&logcond, NULL);
01133    if (ast_pthread_create(&logthread, NULL, logger_thread, NULL) < 0) {
01134       ast_cond_destroy(&logcond);
01135       return -1;
01136    }
01137 
01138    /* register the logger cli commands */
01139    ast_cli_register_multiple(cli_logger, ARRAY_LEN(cli_logger));
01140 
01141    ast_mkdir(ast_config_AST_LOG_DIR, 0777);
01142 
01143    /* create log channels */
01144    init_logger_chain(0 /* locked */);
01145    logger_initialized = 1;
01146 
01147    return 0;
01148 }
01149 
01150 void close_logger(void)
01151 {
01152    struct logchannel *f = NULL;
01153 
01154    logger_initialized = 0;
01155 
01156    /* Stop logger thread */
01157    AST_LIST_LOCK(&logmsgs);
01158    close_logger_thread = 1;
01159    ast_cond_signal(&logcond);
01160    AST_LIST_UNLOCK(&logmsgs);
01161 
01162    if (logthread != AST_PTHREADT_NULL)
01163       pthread_join(logthread, NULL);
01164 
01165    AST_RWLIST_WRLOCK(&logchannels);
01166 
01167    if (qlog) {
01168       fclose(qlog);
01169       qlog = NULL;
01170    }
01171 
01172    AST_RWLIST_TRAVERSE(&logchannels, f, list) {
01173       if (f->fileptr && (f->fileptr != stdout) && (f->fileptr != stderr)) {
01174          fclose(f->fileptr);
01175          f->fileptr = NULL;
01176       }
01177    }
01178 
01179    closelog(); /* syslog */
01180 
01181    AST_RWLIST_UNLOCK(&logchannels);
01182 
01183    return;
01184 }
01185 
01186 /*!
01187  * \brief send log messages to syslog and/or the console
01188  */
01189 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
01190 {
01191    struct logmsg *logmsg = NULL;
01192    struct ast_str *buf = NULL;
01193    struct ast_tm tm;
01194    struct timeval now = ast_tvnow();
01195    int res = 0;
01196    va_list ap;
01197    char datestring[256];
01198 
01199    if (!(buf = ast_str_thread_get(&log_buf, LOG_BUF_INIT_SIZE)))
01200       return;
01201 
01202    if (level != __LOG_VERBOSE && AST_RWLIST_EMPTY(&logchannels)) {
01203       /*
01204        * we don't have the logger chain configured yet,
01205        * so just log to stdout
01206        */
01207       int result;
01208       va_start(ap, fmt);
01209       result = ast_str_set_va(&buf, BUFSIZ, fmt, ap); /* XXX BUFSIZ ? */
01210       va_end(ap);
01211       if (result != AST_DYNSTR_BUILD_FAILED) {
01212          term_filter_escapes(ast_str_buffer(buf));
01213          fputs(ast_str_buffer(buf), stdout);
01214       }
01215       return;
01216    }
01217    
01218    /* don't display LOG_DEBUG messages unless option_verbose _or_ option_debug
01219       are non-zero; LOG_DEBUG messages can still be displayed if option_debug
01220       is zero, if option_verbose is non-zero (this allows for 'level zero'
01221       LOG_DEBUG messages to be displayed, if the logmask on any channel
01222       allows it)
01223    */
01224    if (!option_verbose && !option_debug && (level == __LOG_DEBUG))
01225       return;
01226 
01227    /* Ignore anything that never gets logged anywhere */
01228    if (level != __LOG_VERBOSE && !(global_logmask & (1 << level)))
01229       return;
01230    
01231    /* Build string */
01232    va_start(ap, fmt);
01233    res = ast_str_set_va(&buf, BUFSIZ, fmt, ap);
01234    va_end(ap);
01235 
01236    /* If the build failed, then abort and free this structure */
01237    if (res == AST_DYNSTR_BUILD_FAILED)
01238       return;
01239 
01240    /* Create a new logging message */
01241    if (!(logmsg = ast_calloc_with_stringfields(1, struct logmsg, res + 128)))
01242       return;
01243 
01244    /* Copy string over */
01245    ast_string_field_set(logmsg, message, ast_str_buffer(buf));
01246 
01247    /* Set type */
01248    if (level == __LOG_VERBOSE) {
01249       logmsg->type = LOGMSG_VERBOSE;
01250    } else {
01251       logmsg->type = LOGMSG_NORMAL;
01252    }
01253 
01254    /* Create our date/time */
01255    ast_localtime(&now, &tm, NULL);
01256    ast_strftime(datestring, sizeof(datestring), dateformat, &tm);
01257    ast_string_field_set(logmsg, date, datestring);
01258 
01259    /* Copy over data */
01260    logmsg->level = level;
01261    logmsg->line = line;
01262    ast_string_field_set(logmsg, level_name, levels[level]);
01263    ast_string_field_set(logmsg, file, file);
01264    ast_string_field_set(logmsg, function, function);
01265    logmsg->process_id = (long) GETTID();
01266 
01267    /* If the logger thread is active, append it to the tail end of the list - otherwise skip that step */
01268    if (logthread != AST_PTHREADT_NULL) {
01269       AST_LIST_LOCK(&logmsgs);
01270       AST_LIST_INSERT_TAIL(&logmsgs, logmsg, list);
01271       ast_cond_signal(&logcond);
01272       AST_LIST_UNLOCK(&logmsgs);
01273    } else {
01274       logger_print_normal(logmsg);
01275       ast_free(logmsg);
01276    }
01277 
01278    return;
01279 }
01280 
01281 #ifdef HAVE_BKTR
01282 
01283 struct ast_bt *ast_bt_create(void) 
01284 {
01285    struct ast_bt *bt = ast_calloc(1, sizeof(*bt));
01286    if (!bt) {
01287       ast_log(LOG_ERROR, "Unable to allocate memory for backtrace structure!\n");
01288       return NULL;
01289    }
01290 
01291    bt->alloced = 1;
01292 
01293    ast_bt_get_addresses(bt);
01294 
01295    return bt;
01296 }
01297 
01298 int ast_bt_get_addresses(struct ast_bt *bt)
01299 {
01300    bt->num_frames = backtrace(bt->addresses, AST_MAX_BT_FRAMES);
01301 
01302    return 0;
01303 }
01304 
01305 void *ast_bt_destroy(struct ast_bt *bt)
01306 {
01307    if (bt->alloced) {
01308       ast_free(bt);
01309    }
01310 
01311    return NULL;
01312 }
01313 
01314 char **ast_bt_get_symbols(void **addresses, size_t num_frames)
01315 {
01316    char **strings = NULL;
01317 #if defined(BETTER_BACKTRACES)
01318    int stackfr;
01319    bfd *bfdobj;           /* bfd.h */
01320    Dl_info dli;           /* dlfcn.h */
01321    long allocsize;
01322    asymbol **syms = NULL; /* bfd.h */
01323    bfd_vma offset;        /* bfd.h */
01324    const char *lastslash;
01325    asection *section;
01326    const char *file, *func;
01327    unsigned int line;
01328    char address_str[128];
01329    char msg[1024];
01330    size_t strings_size;
01331    size_t *eachlen;
01332 #endif
01333 
01334 #if defined(BETTER_BACKTRACES)
01335    strings_size = num_frames * sizeof(*strings);
01336    eachlen = ast_calloc(num_frames, sizeof(*eachlen));
01337 
01338    if (!(strings = ast_calloc(num_frames, sizeof(*strings)))) {
01339       return NULL;
01340    }
01341 
01342    for (stackfr = 0; stackfr < num_frames; stackfr++) {
01343       int found = 0, symbolcount;
01344 
01345       msg[0] = '\0';
01346 
01347       if (!dladdr(addresses[stackfr], &dli)) {
01348          continue;
01349       }
01350 
01351       if (strcmp(dli.dli_fname, "asterisk") == 0) {
01352          char asteriskpath[256];
01353          if (!(dli.dli_fname = ast_utils_which("asterisk", asteriskpath, sizeof(asteriskpath)))) {
01354             /* This will fail to find symbols */
01355             ast_log(LOG_DEBUG, "Failed to find asterisk binary for debug symbols.\n");
01356             dli.dli_fname = "asterisk";
01357          }
01358       }
01359 
01360       lastslash = strrchr(dli.dli_fname, '/');
01361       if (  (bfdobj = bfd_openr(dli.dli_fname, NULL)) &&
01362             bfd_check_format(bfdobj, bfd_object) &&
01363             (allocsize = bfd_get_symtab_upper_bound(bfdobj)) > 0 &&
01364             (syms = ast_malloc(allocsize)) &&
01365             (symbolcount = bfd_canonicalize_symtab(bfdobj, syms))) {
01366 
01367          if (bfdobj->flags & DYNAMIC) {
01368             offset = addresses[stackfr] - dli.dli_fbase;
01369          } else {
01370             offset = addresses[stackfr] - (void *) 0;
01371          }
01372 
01373          for (section = bfdobj->sections; section; section = section->next) {
01374             if (  !bfd_get_section_flags(bfdobj, section) & SEC_ALLOC ||
01375                   section->vma > offset ||
01376                   section->size + section->vma < offset) {
01377                continue;
01378             }
01379 
01380             if (!bfd_find_nearest_line(bfdobj, section, syms, offset - section->vma, &file, &func, &line)) {
01381                continue;
01382             }
01383 
01384             /* Stack trace output */
01385             found++;
01386             if ((lastslash = strrchr(file, '/'))) {
01387                const char *prevslash;
01388                for (prevslash = lastslash - 1; *prevslash != '/' && prevslash >= file; prevslash--);
01389                if (prevslash >= file) {
01390                   lastslash = prevslash;
01391                }
01392             }
01393             if (dli.dli_saddr == NULL) {
01394                address_str[0] = '\0';
01395             } else {
01396                snprintf(address_str, sizeof(address_str), " (%p+%lX)",
01397                   dli.dli_saddr,
01398                   (unsigned long) (addresses[stackfr] - dli.dli_saddr));
01399             }
01400             snprintf(msg, sizeof(msg), "%s:%u %s()%s",
01401                lastslash ? lastslash + 1 : file, line,
01402                S_OR(func, "???"),
01403                address_str);
01404 
01405             break; /* out of section iteration */
01406          }
01407       }
01408       if (bfdobj) {
01409          bfd_close(bfdobj);
01410          if (syms) {
01411             ast_free(syms);
01412          }
01413       }
01414 
01415       /* Default output, if we cannot find the information within BFD */
01416       if (!found) {
01417          if (dli.dli_saddr == NULL) {
01418             address_str[0] = '\0';
01419          } else {
01420             snprintf(address_str, sizeof(address_str), " (%p+%lX)",
01421                dli.dli_saddr,
01422                (unsigned long) (addresses[stackfr] - dli.dli_saddr));
01423          }
01424          snprintf(msg, sizeof(msg), "%s %s()%s",
01425             lastslash ? lastslash + 1 : dli.dli_fname,
01426             S_OR(dli.dli_sname, "<unknown>"),
01427             address_str);
01428       }
01429 
01430       if (!ast_strlen_zero(msg)) {
01431          char **tmp;
01432          eachlen[stackfr] = strlen(msg);
01433          if (!(tmp = ast_realloc(strings, strings_size + eachlen[stackfr] + 1))) {
01434             ast_free(strings);
01435             strings = NULL;
01436             break; /* out of stack frame iteration */
01437          }
01438          strings = tmp;
01439          strings[stackfr] = (char *) strings + strings_size;
01440          ast_copy_string(strings[stackfr], msg, eachlen[stackfr] + 1);
01441          strings_size += eachlen[stackfr] + 1;
01442       }
01443    }
01444 
01445    if (strings) {
01446       /* Recalculate the offset pointers */
01447       strings[0] = (char *) strings + num_frames * sizeof(*strings);
01448       for (stackfr = 1; stackfr < num_frames; stackfr++) {
01449          strings[stackfr] = strings[stackfr - 1] + eachlen[stackfr - 1] + 1;
01450       }
01451    }
01452 #else /* !defined(BETTER_BACKTRACES) */
01453    strings = backtrace_symbols(addresses, num_frames);
01454 #endif /* defined(BETTER_BACKTRACES) */
01455    return strings;
01456 }
01457 
01458 #endif /* HAVE_BKTR */
01459 
01460 void ast_backtrace(void)
01461 {
01462 #ifdef HAVE_BKTR
01463    struct ast_bt *bt;
01464    int i = 0;
01465    char **strings;
01466 
01467    if (!(bt = ast_bt_create())) {
01468       ast_log(LOG_WARNING, "Unable to allocate space for backtrace structure\n");
01469       return;
01470    }
01471 
01472    if ((strings = ast_bt_get_symbols(bt->addresses, bt->num_frames))) {
01473       ast_debug(1, "Got %d backtrace record%c\n", bt->num_frames, bt->num_frames != 1 ? 's' : ' ');
01474       for (i = 3; i < bt->num_frames - 2; i++) {
01475          ast_log(LOG_DEBUG, "#%d: [%p] %s\n", i - 3, bt->addresses[i], strings[i]);
01476       }
01477 
01478       /* MALLOC_DEBUG will erroneously report an error here, unless we undef the macro. */
01479 #undef free
01480       free(strings);
01481    } else {
01482       ast_debug(1, "Could not allocate memory for backtrace\n");
01483    }
01484    ast_bt_destroy(bt);
01485 #else
01486    ast_log(LOG_WARNING, "Must run configure with '--with-execinfo' for stack backtraces.\n");
01487 #endif /* defined(HAVE_BKTR) */
01488 }
01489 
01490 void __ast_verbose_ap(const char *file, int line, const char *func, const char *fmt, va_list ap)
01491 {
01492    struct ast_str *buf = NULL;
01493    int res = 0;
01494 
01495    if (!(buf = ast_str_thread_get(&verbose_buf, VERBOSE_BUF_INIT_SIZE)))
01496       return;
01497 
01498    if (ast_opt_timestamp) {
01499       struct timeval now;
01500       struct ast_tm tm;
01501       char date[40];
01502       char *datefmt;
01503 
01504       now = ast_tvnow();
01505       ast_localtime(&now, &tm, NULL);
01506       ast_strftime(date, sizeof(date), dateformat, &tm);
01507       datefmt = alloca(strlen(date) + 3 + strlen(fmt) + 1);
01508       sprintf(datefmt, "%c[%s] %s", 127, date, fmt);
01509       fmt = datefmt;
01510    } else {
01511       char *tmp = alloca(strlen(fmt) + 2);
01512       sprintf(tmp, "%c%s", 127, fmt);
01513       fmt = tmp;
01514    }
01515 
01516    /* Build string */
01517    res = ast_str_set_va(&buf, 0, fmt, ap);
01518 
01519    /* If the build failed then we can drop this allocated message */
01520    if (res == AST_DYNSTR_BUILD_FAILED)
01521       return;
01522 
01523    ast_log(__LOG_VERBOSE, file, line, func, "%s", ast_str_buffer(buf));
01524 }
01525 
01526 void __ast_verbose(const char *file, int line, const char *func, const char *fmt, ...)
01527 {
01528    va_list ap;
01529 
01530    va_start(ap, fmt);
01531    __ast_verbose_ap(file, line, func, fmt, ap);
01532    va_end(ap);
01533 }
01534 
01535 /* No new code should use this directly, but we have the ABI for backwards compat */
01536 #undef ast_verbose
01537 void __attribute__((format(printf, 1,2))) ast_verbose(const char *fmt, ...);
01538 void ast_verbose(const char *fmt, ...)
01539 {
01540    va_list ap;
01541 
01542    va_start(ap, fmt);
01543    __ast_verbose_ap("", 0, "", fmt, ap);
01544    va_end(ap);
01545 }
01546 
01547 int ast_register_verbose(void (*v)(const char *string)) 
01548 {
01549    struct verb *verb;
01550 
01551    if (!(verb = ast_malloc(sizeof(*verb))))
01552       return -1;
01553 
01554    verb->verboser = v;
01555 
01556    AST_RWLIST_WRLOCK(&verbosers);
01557    AST_RWLIST_INSERT_HEAD(&verbosers, verb, list);
01558    AST_RWLIST_UNLOCK(&verbosers);
01559    
01560    return 0;
01561 }
01562 
01563 int ast_unregister_verbose(void (*v)(const char *string))
01564 {
01565    struct verb *cur;
01566 
01567    AST_RWLIST_WRLOCK(&verbosers);
01568    AST_RWLIST_TRAVERSE_SAFE_BEGIN(&verbosers, cur, list) {
01569       if (cur->verboser == v) {
01570          AST_RWLIST_REMOVE_CURRENT(list);
01571          ast_free(cur);
01572          break;
01573       }
01574    }
01575    AST_RWLIST_TRAVERSE_SAFE_END;
01576    AST_RWLIST_UNLOCK(&verbosers);
01577    
01578    return cur ? 0 : -1;
01579 }
01580 
01581 static void update_logchannels(void)
01582 {
01583    struct logchannel *cur;
01584 
01585    AST_RWLIST_WRLOCK(&logchannels);
01586 
01587    global_logmask = 0;
01588 
01589    AST_RWLIST_TRAVERSE(&logchannels, cur, list) {
01590       cur->logmask = make_components(cur->components, cur->lineno);
01591       global_logmask |= cur->logmask;
01592    }
01593 
01594    AST_RWLIST_UNLOCK(&logchannels);
01595 }
01596 
01597 int ast_logger_register_level(const char *name)
01598 {
01599    unsigned int level;
01600    unsigned int available = 0;
01601 
01602    AST_RWLIST_WRLOCK(&logchannels);
01603 
01604    for (level = 0; level < ARRAY_LEN(levels); level++) {
01605       if ((level >= 16) && !available && !levels[level]) {
01606          available = level;
01607          continue;
01608       }
01609 
01610       if (levels[level] && !strcasecmp(levels[level], name)) {
01611          ast_log(LOG_WARNING,
01612             "Unable to register dynamic logger level '%s': a standard logger level uses that name.\n",
01613             name);
01614          AST_RWLIST_UNLOCK(&logchannels);
01615 
01616          return -1;
01617       }
01618    }
01619 
01620    if (!available) {
01621       ast_log(LOG_WARNING,
01622          "Unable to register dynamic logger level '%s'; maximum number of levels registered.\n",
01623          name);
01624       AST_RWLIST_UNLOCK(&logchannels);
01625 
01626       return -1;
01627    }
01628 
01629    levels[available] = ast_strdup(name);
01630 
01631    AST_RWLIST_UNLOCK(&logchannels);
01632 
01633    ast_debug(1, "Registered dynamic logger level '%s' with index %d.\n", name, available);
01634 
01635    update_logchannels();
01636 
01637    return available;
01638 }
01639 
01640 void ast_logger_unregister_level(const char *name)
01641 {
01642    unsigned int found = 0;
01643    unsigned int x;
01644 
01645    AST_RWLIST_WRLOCK(&logchannels);
01646 
01647    for (x = 16; x < ARRAY_LEN(levels); x++) {
01648       if (!levels[x]) {
01649          continue;
01650       }
01651 
01652       if (strcasecmp(levels[x], name)) {
01653          continue;
01654       }
01655 
01656       found = 1;
01657       break;
01658    }
01659 
01660    if (found) {
01661       /* take this level out of the global_logmask, to ensure that no new log messages
01662        * will be queued for it
01663        */
01664 
01665       global_logmask &= ~(1 << x);
01666 
01667       ast_free(levels[x]);
01668       levels[x] = NULL;
01669       AST_RWLIST_UNLOCK(&logchannels);
01670 
01671       ast_debug(1, "Unregistered dynamic logger level '%s' with index %d.\n", name, x);
01672 
01673       update_logchannels();
01674    } else {
01675       AST_RWLIST_UNLOCK(&logchannels);
01676    }
01677 }
01678 

Generated on Mon Mar 19 11:30:28 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7