00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "asterisk.h"
00029
00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 287863 $")
00031
00032
00033
00034 #include <syslog.h>
00035
00036 #include "asterisk/_private.h"
00037 #include "asterisk/paths.h"
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 #endif
00060
00061 #if defined(__linux__) && !defined(__NR_gettid)
00062 #include <asm/unistd.h>
00063 #endif
00064
00065 #if defined(__linux__) && defined(__NR_gettid)
00066 #define GETTID() syscall(__NR_gettid)
00067 #else
00068 #define GETTID() getpid()
00069 #endif
00070 static char dateformat[256] = "%b %e %T";
00071
00072 static char queue_log_name[256] = QUEUELOG;
00073 static char exec_after_rotate[256] = "";
00074
00075 static int filesize_reload_needed;
00076 static unsigned int global_logmask = 0xFFFF;
00077 static int queuelog_init;
00078
00079 static enum rotatestrategy {
00080 SEQUENTIAL = 1 << 0,
00081 ROTATE = 1 << 1,
00082 TIMESTAMP = 1 << 2,
00083 } rotatestrategy = SEQUENTIAL;
00084
00085 static struct {
00086 unsigned int queue_log:1;
00087 unsigned int queue_log_to_file:1;
00088 unsigned int queue_adaptive_realtime:1;
00089 } logfiles = { 1 };
00090
00091 static char hostname[MAXHOSTNAMELEN];
00092
00093 enum logtypes {
00094 LOGTYPE_SYSLOG,
00095 LOGTYPE_FILE,
00096 LOGTYPE_CONSOLE,
00097 };
00098
00099 struct logchannel {
00100
00101 unsigned int logmask;
00102
00103 int disabled;
00104
00105 int facility;
00106
00107 enum logtypes type;
00108
00109 FILE *fileptr;
00110
00111 char filename[PATH_MAX];
00112
00113 AST_LIST_ENTRY(logchannel) list;
00114
00115 int lineno;
00116
00117 char components[0];
00118 };
00119
00120 static AST_RWLIST_HEAD_STATIC(logchannels, logchannel);
00121
00122 enum logmsgtypes {
00123 LOGMSG_NORMAL = 0,
00124 LOGMSG_VERBOSE,
00125 };
00126
00127 struct logmsg {
00128 enum logmsgtypes type;
00129 int level;
00130 int line;
00131 long process_id;
00132 AST_DECLARE_STRING_FIELDS(
00133 AST_STRING_FIELD(date);
00134 AST_STRING_FIELD(file);
00135 AST_STRING_FIELD(function);
00136 AST_STRING_FIELD(message);
00137 AST_STRING_FIELD(level_name);
00138 );
00139 AST_LIST_ENTRY(logmsg) list;
00140 };
00141
00142 static AST_LIST_HEAD_STATIC(logmsgs, logmsg);
00143 static pthread_t logthread = AST_PTHREADT_NULL;
00144 static ast_cond_t logcond;
00145 static int close_logger_thread = 0;
00146
00147 static FILE *qlog;
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160 static char *levels[32] = {
00161 "DEBUG",
00162 "---EVENT---",
00163 "NOTICE",
00164 "WARNING",
00165 "ERROR",
00166 "VERBOSE",
00167 "DTMF",
00168 };
00169
00170
00171 static const int colors[32] = {
00172 COLOR_BRGREEN,
00173 COLOR_BRBLUE,
00174 COLOR_YELLOW,
00175 COLOR_BRRED,
00176 COLOR_RED,
00177 COLOR_GREEN,
00178 COLOR_BRGREEN,
00179 0,
00180 0,
00181 0,
00182 0,
00183 0,
00184 0,
00185 0,
00186 0,
00187 0,
00188 COLOR_BRBLUE,
00189 COLOR_BRBLUE,
00190 COLOR_BRBLUE,
00191 COLOR_BRBLUE,
00192 COLOR_BRBLUE,
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 };
00205
00206 AST_THREADSTORAGE(verbose_buf);
00207 #define VERBOSE_BUF_INIT_SIZE 256
00208
00209 AST_THREADSTORAGE(log_buf);
00210 #define LOG_BUF_INIT_SIZE 256
00211
00212 static void logger_queue_init(void);
00213
00214 static unsigned int make_components(const char *s, int lineno)
00215 {
00216 char *w;
00217 unsigned int res = 0;
00218 char *stringp = ast_strdupa(s);
00219 unsigned int x;
00220
00221 while ((w = strsep(&stringp, ","))) {
00222 w = ast_skip_blanks(w);
00223
00224 if (!strcmp(w, "*")) {
00225 res = 0xFFFFFFFF;
00226 break;
00227 } else for (x = 0; x < ARRAY_LEN(levels); x++) {
00228 if (levels[x] && !strcasecmp(w, levels[x])) {
00229 res |= (1 << x);
00230 break;
00231 }
00232 }
00233 }
00234
00235 return res;
00236 }
00237
00238 static struct logchannel *make_logchannel(const char *channel, const char *components, int lineno)
00239 {
00240 struct logchannel *chan;
00241 char *facility;
00242
00243 if (ast_strlen_zero(channel) || !(chan = ast_calloc(1, sizeof(*chan) + strlen(components) + 1)))
00244 return NULL;
00245
00246 strcpy(chan->components, components);
00247 chan->lineno = lineno;
00248
00249 if (!strcasecmp(channel, "console")) {
00250 chan->type = LOGTYPE_CONSOLE;
00251 } else if (!strncasecmp(channel, "syslog", 6)) {
00252
00253
00254
00255
00256 facility = strchr(channel, '.');
00257 if (!facility++ || !facility) {
00258 facility = "local0";
00259 }
00260
00261 chan->facility = ast_syslog_facility(facility);
00262
00263 if (chan->facility < 0) {
00264 fprintf(stderr, "Logger Warning: bad syslog facility in logger.conf\n");
00265 ast_free(chan);
00266 return NULL;
00267 }
00268
00269 chan->type = LOGTYPE_SYSLOG;
00270 ast_copy_string(chan->filename, channel, sizeof(chan->filename));
00271 openlog("asterisk", LOG_PID, chan->facility);
00272 } else {
00273 if (!ast_strlen_zero(hostname)) {
00274 snprintf(chan->filename, sizeof(chan->filename), "%s/%s.%s",
00275 channel[0] != '/' ? ast_config_AST_LOG_DIR : "", channel, hostname);
00276 } else {
00277 snprintf(chan->filename, sizeof(chan->filename), "%s/%s",
00278 channel[0] != '/' ? ast_config_AST_LOG_DIR : "", channel);
00279 }
00280 if (!(chan->fileptr = fopen(chan->filename, "a"))) {
00281
00282 fprintf(stderr, "Logger Warning: Unable to open log file '%s': %s\n", chan->filename, strerror(errno));
00283 }
00284 chan->type = LOGTYPE_FILE;
00285 }
00286 chan->logmask = make_components(chan->components, lineno);
00287
00288 return chan;
00289 }
00290
00291 static void init_logger_chain(int locked)
00292 {
00293 struct logchannel *chan;
00294 struct ast_config *cfg;
00295 struct ast_variable *var;
00296 const char *s;
00297 struct ast_flags config_flags = { 0 };
00298
00299 if (!(cfg = ast_config_load2("logger.conf", "logger", config_flags)) || cfg == CONFIG_STATUS_FILEINVALID) {
00300 return;
00301 }
00302
00303
00304 if (!locked) {
00305 AST_RWLIST_WRLOCK(&logchannels);
00306 }
00307 while ((chan = AST_RWLIST_REMOVE_HEAD(&logchannels, list))) {
00308 ast_free(chan);
00309 }
00310 global_logmask = 0;
00311 if (!locked) {
00312 AST_RWLIST_UNLOCK(&logchannels);
00313 }
00314
00315 errno = 0;
00316
00317 closelog();
00318
00319
00320 if (!cfg) {
00321 if (errno) {
00322 fprintf(stderr, "Unable to open logger.conf: %s; default settings will be used.\n", strerror(errno));
00323 } else {
00324 fprintf(stderr, "Errors detected in logger.conf: see above; default settings will be used.\n");
00325 }
00326 if (!(chan = ast_calloc(1, sizeof(*chan)))) {
00327 return;
00328 }
00329 chan->type = LOGTYPE_CONSOLE;
00330 chan->logmask = __LOG_WARNING | __LOG_NOTICE | __LOG_ERROR;
00331 if (!locked) {
00332 AST_RWLIST_WRLOCK(&logchannels);
00333 }
00334 AST_RWLIST_INSERT_HEAD(&logchannels, chan, list);
00335 global_logmask |= chan->logmask;
00336 if (!locked) {
00337 AST_RWLIST_UNLOCK(&logchannels);
00338 }
00339 return;
00340 }
00341
00342 if ((s = ast_variable_retrieve(cfg, "general", "appendhostname"))) {
00343 if (ast_true(s)) {
00344 if (gethostname(hostname, sizeof(hostname) - 1)) {
00345 ast_copy_string(hostname, "unknown", sizeof(hostname));
00346 fprintf(stderr, "What box has no hostname???\n");
00347 }
00348 } else
00349 hostname[0] = '\0';
00350 } else
00351 hostname[0] = '\0';
00352 if ((s = ast_variable_retrieve(cfg, "general", "dateformat")))
00353 ast_copy_string(dateformat, s, sizeof(dateformat));
00354 else
00355 ast_copy_string(dateformat, "%b %e %T", sizeof(dateformat));
00356 if ((s = ast_variable_retrieve(cfg, "general", "queue_log"))) {
00357 logfiles.queue_log = ast_true(s);
00358 }
00359 if ((s = ast_variable_retrieve(cfg, "general", "queue_log_to_file"))) {
00360 logfiles.queue_log_to_file = ast_true(s);
00361 }
00362 if ((s = ast_variable_retrieve(cfg, "general", "queue_log_name"))) {
00363 ast_copy_string(queue_log_name, s, sizeof(queue_log_name));
00364 }
00365 if ((s = ast_variable_retrieve(cfg, "general", "exec_after_rotate"))) {
00366 ast_copy_string(exec_after_rotate, s, sizeof(exec_after_rotate));
00367 }
00368 if ((s = ast_variable_retrieve(cfg, "general", "rotatestrategy"))) {
00369 if (strcasecmp(s, "timestamp") == 0) {
00370 rotatestrategy = TIMESTAMP;
00371 } else if (strcasecmp(s, "rotate") == 0) {
00372 rotatestrategy = ROTATE;
00373 } else if (strcasecmp(s, "sequential") == 0) {
00374 rotatestrategy = SEQUENTIAL;
00375 } else {
00376 fprintf(stderr, "Unknown rotatestrategy: %s\n", s);
00377 }
00378 } else {
00379 if ((s = ast_variable_retrieve(cfg, "general", "rotatetimestamp"))) {
00380 rotatestrategy = ast_true(s) ? TIMESTAMP : SEQUENTIAL;
00381 fprintf(stderr, "rotatetimestamp option has been deprecated. Please use rotatestrategy instead.\n");
00382 }
00383 }
00384
00385 if (!locked) {
00386 AST_RWLIST_WRLOCK(&logchannels);
00387 }
00388 var = ast_variable_browse(cfg, "logfiles");
00389 for (; var; var = var->next) {
00390 if (!(chan = make_logchannel(var->name, var->value, var->lineno))) {
00391 continue;
00392 }
00393 AST_RWLIST_INSERT_HEAD(&logchannels, chan, list);
00394 global_logmask |= chan->logmask;
00395 }
00396 if (qlog) {
00397 char tmp[4096];
00398 fclose(qlog);
00399 snprintf(tmp, sizeof(tmp), "%s/%s", ast_config_AST_LOG_DIR, queue_log_name);
00400 qlog = fopen(tmp, "a");
00401 }
00402
00403 if (!locked) {
00404 AST_RWLIST_UNLOCK(&logchannels);
00405 }
00406
00407 ast_config_destroy(cfg);
00408 }
00409
00410 void ast_child_verbose(int level, const char *fmt, ...)
00411 {
00412 char *msg = NULL, *emsg = NULL, *sptr, *eptr;
00413 va_list ap, aq;
00414 int size;
00415
00416
00417 if (option_verbose < level) {
00418 return;
00419 }
00420
00421 va_start(ap, fmt);
00422 va_copy(aq, ap);
00423 if ((size = vsnprintf(msg, 0, fmt, ap)) < 0) {
00424 va_end(ap);
00425 va_end(aq);
00426 return;
00427 }
00428 va_end(ap);
00429
00430 if (!(msg = ast_malloc(size + 1))) {
00431 va_end(aq);
00432 return;
00433 }
00434
00435 vsnprintf(msg, size + 1, fmt, aq);
00436 va_end(aq);
00437
00438 if (!(emsg = ast_malloc(size * 2 + 1))) {
00439 ast_free(msg);
00440 return;
00441 }
00442
00443 for (sptr = msg, eptr = emsg; ; sptr++) {
00444 if (*sptr == '"') {
00445 *eptr++ = '\\';
00446 }
00447 *eptr++ = *sptr;
00448 if (*sptr == '\0') {
00449 break;
00450 }
00451 }
00452 ast_free(msg);
00453
00454 fprintf(stdout, "verbose \"%s\" %d\n", emsg, level);
00455 fflush(stdout);
00456 ast_free(emsg);
00457 }
00458
00459 void ast_queue_log(const char *queuename, const char *callid, const char *agent, const char *event, const char *fmt, ...)
00460 {
00461 va_list ap;
00462 struct timeval tv;
00463 struct ast_tm tm;
00464 char qlog_msg[8192];
00465 int qlog_len;
00466 char time_str[30];
00467
00468 if (!queuelog_init) {
00469 queuelog_init = 1;
00470 logger_queue_init();
00471 }
00472
00473 if (ast_check_realtime("queue_log")) {
00474 tv = ast_tvnow();
00475 ast_localtime(&tv, &tm, NULL);
00476 ast_strftime(time_str, sizeof(time_str), "%F %T.%6q", &tm);
00477 va_start(ap, fmt);
00478 vsnprintf(qlog_msg, sizeof(qlog_msg), fmt, ap);
00479 va_end(ap);
00480 if (logfiles.queue_adaptive_realtime) {
00481 AST_DECLARE_APP_ARGS(args,
00482 AST_APP_ARG(data)[5];
00483 );
00484 AST_NONSTANDARD_APP_ARGS(args, qlog_msg, '|');
00485
00486 ast_realtime_require_field("queue_log", "data1", RQ_CHAR, strlen(S_OR(args.data[0], "")),
00487 "data2", RQ_CHAR, strlen(S_OR(args.data[1], "")),
00488 "data3", RQ_CHAR, strlen(S_OR(args.data[2], "")),
00489 "data4", RQ_CHAR, strlen(S_OR(args.data[3], "")),
00490 "data5", RQ_CHAR, strlen(S_OR(args.data[4], "")),
00491 SENTINEL);
00492
00493
00494 ast_store_realtime("queue_log", "time", time_str,
00495 "callid", callid,
00496 "queuename", queuename,
00497 "agent", agent,
00498 "event", event,
00499 "data1", S_OR(args.data[0], ""),
00500 "data2", S_OR(args.data[1], ""),
00501 "data3", S_OR(args.data[2], ""),
00502 "data4", S_OR(args.data[3], ""),
00503 "data5", S_OR(args.data[4], ""),
00504 SENTINEL);
00505 } else {
00506 ast_store_realtime("queue_log", "time", time_str,
00507 "callid", callid,
00508 "queuename", queuename,
00509 "agent", agent,
00510 "event", event,
00511 "data", qlog_msg,
00512 SENTINEL);
00513 }
00514
00515 if (!logfiles.queue_log_to_file) {
00516 return;
00517 }
00518 }
00519
00520 if (qlog) {
00521 va_start(ap, fmt);
00522 qlog_len = snprintf(qlog_msg, sizeof(qlog_msg), "%ld|%s|%s|%s|%s|", (long)time(NULL), callid, queuename, agent, event);
00523 vsnprintf(qlog_msg + qlog_len, sizeof(qlog_msg) - qlog_len, fmt, ap);
00524 va_end(ap);
00525 AST_RWLIST_RDLOCK(&logchannels);
00526 if (qlog) {
00527 fprintf(qlog, "%s\n", qlog_msg);
00528 fflush(qlog);
00529 }
00530 AST_RWLIST_UNLOCK(&logchannels);
00531 }
00532 }
00533
00534 static int rotate_file(const char *filename)
00535 {
00536 char old[PATH_MAX];
00537 char new[PATH_MAX];
00538 int x, y, which, found, res = 0, fd;
00539 char *suffixes[4] = { "", ".gz", ".bz2", ".Z" };
00540
00541 switch (rotatestrategy) {
00542 case SEQUENTIAL:
00543 for (x = 0; ; x++) {
00544 snprintf(new, sizeof(new), "%s.%d", filename, x);
00545 fd = open(new, O_RDONLY);
00546 if (fd > -1)
00547 close(fd);
00548 else
00549 break;
00550 }
00551 if (rename(filename, new)) {
00552 fprintf(stderr, "Unable to rename file '%s' to '%s'\n", filename, new);
00553 res = -1;
00554 } else {
00555 filename = new;
00556 }
00557 break;
00558 case TIMESTAMP:
00559 snprintf(new, sizeof(new), "%s.%ld", filename, (long)time(NULL));
00560 if (rename(filename, new)) {
00561 fprintf(stderr, "Unable to rename file '%s' to '%s'\n", filename, new);
00562 res = -1;
00563 } else {
00564 filename = new;
00565 }
00566 break;
00567 case ROTATE:
00568
00569 for (x = 0; ; x++) {
00570 found = 0;
00571 for (which = 0; which < ARRAY_LEN(suffixes); which++) {
00572 snprintf(new, sizeof(new), "%s.%d%s", filename, x, suffixes[which]);
00573 fd = open(new, O_RDONLY);
00574 if (fd > -1) {
00575 close(fd);
00576 found = 1;
00577 break;
00578 }
00579 }
00580 if (!found) {
00581 break;
00582 }
00583 }
00584
00585
00586 for (y = x; y > 0; y--) {
00587 for (which = 0; which < ARRAY_LEN(suffixes); which++) {
00588 snprintf(old, sizeof(old), "%s.%d%s", filename, y - 1, suffixes[which]);
00589 fd = open(old, O_RDONLY);
00590 if (fd > -1) {
00591
00592 close(fd);
00593 snprintf(new, sizeof(new), "%s.%d%s", filename, y, suffixes[which]);
00594 if (rename(old, new)) {
00595 fprintf(stderr, "Unable to rename file '%s' to '%s'\n", old, new);
00596 res = -1;
00597 }
00598 break;
00599 }
00600 }
00601 }
00602
00603
00604 snprintf(new, sizeof(new), "%s.0", filename);
00605 if (rename(filename, new)) {
00606 fprintf(stderr, "Unable to rename file '%s' to '%s'\n", filename, new);
00607 res = -1;
00608 }
00609 }
00610
00611 if (!ast_strlen_zero(exec_after_rotate)) {
00612 struct ast_channel *c = ast_dummy_channel_alloc();
00613 char buf[512];
00614 pbx_builtin_setvar_helper(c, "filename", filename);
00615 pbx_substitute_variables_helper(c, exec_after_rotate, buf, sizeof(buf));
00616 if (ast_safe_system(buf) == -1) {
00617 ast_log(LOG_WARNING, "error executing '%s'\n", buf);
00618 }
00619 c = ast_channel_release(c);
00620 }
00621 return res;
00622 }
00623
00624 static int reload_logger(int rotate)
00625 {
00626 char old[PATH_MAX] = "";
00627 int queue_rotate = rotate;
00628 struct logchannel *f;
00629 int res = 0;
00630
00631 AST_RWLIST_WRLOCK(&logchannels);
00632
00633 if (qlog) {
00634 if (rotate < 0) {
00635
00636 if (ftello(qlog) > 0x40000000) {
00637 fclose(qlog);
00638 qlog = NULL;
00639 } else {
00640 queue_rotate = 0;
00641 }
00642 } else {
00643 fclose(qlog);
00644 qlog = NULL;
00645 }
00646 } else {
00647 queue_rotate = 0;
00648 }
00649
00650 ast_mkdir(ast_config_AST_LOG_DIR, 0777);
00651
00652 AST_RWLIST_TRAVERSE(&logchannels, f, list) {
00653 if (f->disabled) {
00654 f->disabled = 0;
00655 manager_event(EVENT_FLAG_SYSTEM, "LogChannel", "Channel: %s\r\nEnabled: Yes\r\n", f->filename);
00656 }
00657 if (f->fileptr && (f->fileptr != stdout) && (f->fileptr != stderr)) {
00658 int rotate_this = 0;
00659 if (ftello(f->fileptr) > 0x40000000) {
00660
00661 rotate_this = 1;
00662 }
00663 fclose(f->fileptr);
00664 f->fileptr = NULL;
00665 if (rotate || rotate_this) {
00666 rotate_file(f->filename);
00667 }
00668 }
00669 }
00670
00671 filesize_reload_needed = 0;
00672
00673 init_logger_chain(1 );
00674
00675 if (logfiles.queue_log) {
00676 do {
00677 ast_unload_realtime("queue_log");
00678 if (ast_check_realtime("queue_log")) {
00679 if (!ast_realtime_require_field("queue_log",
00680 "time", RQ_DATETIME, 26, "data1", RQ_CHAR, 20,
00681 "data2", RQ_CHAR, 20, "data3", RQ_CHAR, 20,
00682 "data4", RQ_CHAR, 20, "data5", RQ_CHAR, 20, SENTINEL)) {
00683 logfiles.queue_adaptive_realtime = 1;
00684 } else {
00685 logfiles.queue_adaptive_realtime = 0;
00686 }
00687
00688 if (!logfiles.queue_log_to_file) {
00689
00690 break;
00691 }
00692 }
00693 if (qlog) {
00694 fclose(qlog);
00695 qlog = NULL;
00696 }
00697 snprintf(old, sizeof(old), "%s/%s", ast_config_AST_LOG_DIR, queue_log_name);
00698 if (queue_rotate) {
00699 rotate_file(old);
00700 }
00701
00702 qlog = fopen(old, "a");
00703 if (qlog) {
00704 AST_RWLIST_UNLOCK(&logchannels);
00705 ast_queue_log("NONE", "NONE", "NONE", "CONFIGRELOAD", "%s", "");
00706 AST_RWLIST_WRLOCK(&logchannels);
00707 ast_verb(1, "Asterisk Queue Logger restarted\n");
00708 } else {
00709 ast_log(LOG_ERROR, "Unable to create queue log: %s\n", strerror(errno));
00710 res = -1;
00711 }
00712 } while (0);
00713 }
00714
00715 AST_RWLIST_UNLOCK(&logchannels);
00716
00717 return res;
00718 }
00719
00720
00721
00722 int logger_reload(void)
00723 {
00724 if (reload_logger(0)) {
00725 return RESULT_FAILURE;
00726 }
00727 return RESULT_SUCCESS;
00728 }
00729
00730 static char *handle_logger_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00731 {
00732 switch (cmd) {
00733 case CLI_INIT:
00734 e->command = "logger reload";
00735 e->usage =
00736 "Usage: logger reload\n"
00737 " Reloads the logger subsystem state. Use after restarting syslogd(8) if you are using syslog logging.\n";
00738 return NULL;
00739 case CLI_GENERATE:
00740 return NULL;
00741 }
00742 if (reload_logger(0)) {
00743 ast_cli(a->fd, "Failed to reload the logger\n");
00744 return CLI_FAILURE;
00745 }
00746 return CLI_SUCCESS;
00747 }
00748
00749 static char *handle_logger_rotate(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00750 {
00751 switch (cmd) {
00752 case CLI_INIT:
00753 e->command = "logger rotate";
00754 e->usage =
00755 "Usage: logger rotate\n"
00756 " Rotates and Reopens the log files.\n";
00757 return NULL;
00758 case CLI_GENERATE:
00759 return NULL;
00760 }
00761 if (reload_logger(1)) {
00762 ast_cli(a->fd, "Failed to reload the logger and rotate log files\n");
00763 return CLI_FAILURE;
00764 }
00765 return CLI_SUCCESS;
00766 }
00767
00768 static char *handle_logger_set_level(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00769 {
00770 int x;
00771 int state;
00772 int level = -1;
00773
00774 switch (cmd) {
00775 case CLI_INIT:
00776 e->command = "logger set level {DEBUG|NOTICE|WARNING|ERROR|VERBOSE|DTMF} {on|off}";
00777 e->usage =
00778 "Usage: logger set level {DEBUG|NOTICE|WARNING|ERROR|VERBOSE|DTMF} {on|off}\n"
00779 " Set a specific log level to enabled/disabled for this console.\n";
00780 return NULL;
00781 case CLI_GENERATE:
00782 return NULL;
00783 }
00784
00785 if (a->argc < 5)
00786 return CLI_SHOWUSAGE;
00787
00788 AST_RWLIST_WRLOCK(&logchannels);
00789
00790 for (x = 0; x < ARRAY_LEN(levels); x++) {
00791 if (levels[x] && !strcasecmp(a->argv[3], levels[x])) {
00792 level = x;
00793 break;
00794 }
00795 }
00796
00797 AST_RWLIST_UNLOCK(&logchannels);
00798
00799 state = ast_true(a->argv[4]) ? 1 : 0;
00800
00801 if (level != -1) {
00802 ast_console_toggle_loglevel(a->fd, level, state);
00803 ast_cli(a->fd, "Logger status for '%s' has been set to '%s'.\n", levels[level], state ? "on" : "off");
00804 } else
00805 return CLI_SHOWUSAGE;
00806
00807 return CLI_SUCCESS;
00808 }
00809
00810
00811 static char *handle_logger_show_channels(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00812 {
00813 #define FORMATL "%-35.35s %-8.8s %-9.9s "
00814 struct logchannel *chan;
00815 switch (cmd) {
00816 case CLI_INIT:
00817 e->command = "logger show channels";
00818 e->usage =
00819 "Usage: logger show channels\n"
00820 " List configured logger channels.\n";
00821 return NULL;
00822 case CLI_GENERATE:
00823 return NULL;
00824 }
00825 ast_cli(a->fd, FORMATL, "Channel", "Type", "Status");
00826 ast_cli(a->fd, "Configuration\n");
00827 ast_cli(a->fd, FORMATL, "-------", "----", "------");
00828 ast_cli(a->fd, "-------------\n");
00829 AST_RWLIST_RDLOCK(&logchannels);
00830 AST_RWLIST_TRAVERSE(&logchannels, chan, list) {
00831 unsigned int level;
00832
00833 ast_cli(a->fd, FORMATL, chan->filename, chan->type == LOGTYPE_CONSOLE ? "Console" : (chan->type == LOGTYPE_SYSLOG ? "Syslog" : "File"),
00834 chan->disabled ? "Disabled" : "Enabled");
00835 ast_cli(a->fd, " - ");
00836 for (level = 0; level < ARRAY_LEN(levels); level++) {
00837 if ((chan->logmask & (1 << level)) && levels[level]) {
00838 ast_cli(a->fd, "%s ", levels[level]);
00839 }
00840 }
00841 ast_cli(a->fd, "\n");
00842 }
00843 AST_RWLIST_UNLOCK(&logchannels);
00844 ast_cli(a->fd, "\n");
00845
00846 return CLI_SUCCESS;
00847 }
00848
00849 struct verb {
00850 void (*verboser)(const char *string);
00851 AST_LIST_ENTRY(verb) list;
00852 };
00853
00854 static AST_RWLIST_HEAD_STATIC(verbosers, verb);
00855
00856 static struct ast_cli_entry cli_logger[] = {
00857 AST_CLI_DEFINE(handle_logger_show_channels, "List configured log channels"),
00858 AST_CLI_DEFINE(handle_logger_reload, "Reopens the log files"),
00859 AST_CLI_DEFINE(handle_logger_rotate, "Rotates and reopens the log files"),
00860 AST_CLI_DEFINE(handle_logger_set_level, "Enables/Disables a specific logging level for this console")
00861 };
00862
00863 static void _handle_SIGXFSZ(int sig)
00864 {
00865
00866 filesize_reload_needed = 1;
00867 }
00868
00869 static struct sigaction handle_SIGXFSZ = {
00870 .sa_handler = _handle_SIGXFSZ,
00871 .sa_flags = SA_RESTART,
00872 };
00873
00874 static void ast_log_vsyslog(struct logmsg *msg)
00875 {
00876 char buf[BUFSIZ];
00877 int syslog_level = ast_syslog_priority_from_loglevel(msg->level);
00878
00879 if (syslog_level < 0) {
00880
00881 fprintf(stderr, "ast_log_vsyslog called with bogus level: %d\n", msg->level);
00882 return;
00883 }
00884
00885 snprintf(buf, sizeof(buf), "%s[%ld]: %s:%d in %s: %s",
00886 levels[msg->level], msg->process_id, msg->file, msg->line, msg->function, msg->message);
00887
00888 term_strip(buf, buf, strlen(buf) + 1);
00889 syslog(syslog_level, "%s", buf);
00890 }
00891
00892
00893 static void logger_print_normal(struct logmsg *logmsg)
00894 {
00895 struct logchannel *chan = NULL;
00896 char buf[BUFSIZ];
00897 struct verb *v = NULL;
00898
00899 if (logmsg->level == __LOG_VERBOSE) {
00900 char *tmpmsg = ast_strdupa(logmsg->message + 1);
00901
00902 AST_RWLIST_RDLOCK(&verbosers);
00903 AST_RWLIST_TRAVERSE(&verbosers, v, list)
00904 v->verboser(logmsg->message);
00905 AST_RWLIST_UNLOCK(&verbosers);
00906 ast_string_field_set(logmsg, message, tmpmsg);
00907 }
00908
00909 AST_RWLIST_RDLOCK(&logchannels);
00910
00911 if (!AST_RWLIST_EMPTY(&logchannels)) {
00912 AST_RWLIST_TRAVERSE(&logchannels, chan, list) {
00913
00914 if (chan->disabled)
00915 continue;
00916
00917 if (chan->type == LOGTYPE_SYSLOG && (chan->logmask & (1 << logmsg->level))) {
00918 ast_log_vsyslog(logmsg);
00919
00920 } else if (chan->type == LOGTYPE_CONSOLE && (chan->logmask & (1 << logmsg->level))) {
00921 char linestr[128];
00922 char tmp1[80], tmp2[80], tmp3[80], tmp4[80];
00923
00924
00925 if (logmsg->level == __LOG_VERBOSE)
00926 continue;
00927
00928
00929 snprintf(linestr, sizeof(linestr), "%d", logmsg->line);
00930
00931 snprintf(buf, sizeof(buf), "[%s] %s[%ld]: %s:%s %s: %s",
00932 logmsg->date,
00933 term_color(tmp1, logmsg->level_name, colors[logmsg->level], 0, sizeof(tmp1)),
00934 logmsg->process_id,
00935 term_color(tmp2, logmsg->file, COLOR_BRWHITE, 0, sizeof(tmp2)),
00936 term_color(tmp3, linestr, COLOR_BRWHITE, 0, sizeof(tmp3)),
00937 term_color(tmp4, logmsg->function, COLOR_BRWHITE, 0, sizeof(tmp4)),
00938 logmsg->message);
00939
00940 ast_console_puts_mutable(buf, logmsg->level);
00941
00942 } else if (chan->type == LOGTYPE_FILE && (chan->logmask & (1 << logmsg->level))) {
00943 int res = 0;
00944
00945
00946 if (!chan->fileptr) {
00947 continue;
00948 }
00949
00950
00951 res = fprintf(chan->fileptr, "[%s] %s[%ld] %s: %s",
00952 logmsg->date, logmsg->level_name, logmsg->process_id, logmsg->file, term_strip(buf, logmsg->message, BUFSIZ));
00953 if (res <= 0 && !ast_strlen_zero(logmsg->message)) {
00954 fprintf(stderr, "**** Asterisk Logging Error: ***********\n");
00955 if (errno == ENOMEM || errno == ENOSPC)
00956 fprintf(stderr, "Asterisk logging error: Out of disk space, can't log to log file %s\n", chan->filename);
00957 else
00958 fprintf(stderr, "Logger Warning: Unable to write to log file '%s': %s (disabled)\n", chan->filename, strerror(errno));
00959 manager_event(EVENT_FLAG_SYSTEM, "LogChannel", "Channel: %s\r\nEnabled: No\r\nReason: %d - %s\r\n", chan->filename, errno, strerror(errno));
00960 chan->disabled = 1;
00961 } else if (res > 0) {
00962 fflush(chan->fileptr);
00963 }
00964 }
00965 }
00966 } else if (logmsg->level != __LOG_VERBOSE) {
00967 fputs(logmsg->message, stdout);
00968 }
00969
00970 AST_RWLIST_UNLOCK(&logchannels);
00971
00972
00973 if (filesize_reload_needed) {
00974 reload_logger(-1);
00975 ast_verb(1, "Rotated Logs Per SIGXFSZ (Exceeded file size limit)\n");
00976 }
00977
00978 return;
00979 }
00980
00981
00982 static void *logger_thread(void *data)
00983 {
00984 struct logmsg *next = NULL, *msg = NULL;
00985
00986 for (;;) {
00987
00988 AST_LIST_LOCK(&logmsgs);
00989 if (AST_LIST_EMPTY(&logmsgs)) {
00990 if (close_logger_thread) {
00991 break;
00992 } else {
00993 ast_cond_wait(&logcond, &logmsgs.lock);
00994 }
00995 }
00996 next = AST_LIST_FIRST(&logmsgs);
00997 AST_LIST_HEAD_INIT_NOLOCK(&logmsgs);
00998 AST_LIST_UNLOCK(&logmsgs);
00999
01000
01001 while ((msg = next)) {
01002
01003 next = AST_LIST_NEXT(msg, list);
01004
01005
01006 logger_print_normal(msg);
01007
01008
01009 ast_free(msg);
01010 }
01011
01012
01013 if (close_logger_thread)
01014 break;
01015 }
01016
01017 return NULL;
01018 }
01019
01020 static void logger_queue_init(void)
01021 {
01022
01023 ast_unload_realtime("queue_log");
01024 if (logfiles.queue_log && ast_check_realtime("queue_log")) {
01025 if (!ast_realtime_require_field("queue_log",
01026 "time", RQ_DATETIME, 26, "data1", RQ_CHAR, 20,
01027 "data2", RQ_CHAR, 20, "data3", RQ_CHAR, 20,
01028 "data4", RQ_CHAR, 20, "data5", RQ_CHAR, 20, SENTINEL)) {
01029 logfiles.queue_adaptive_realtime = 1;
01030 } else {
01031 logfiles.queue_adaptive_realtime = 0;
01032 }
01033 }
01034
01035 ast_queue_log("NONE", "NONE", "NONE", "QUEUESTART", "%s", "");
01036 }
01037
01038 int init_logger(void)
01039 {
01040
01041 sigaction(SIGXFSZ, &handle_SIGXFSZ, NULL);
01042
01043
01044 ast_cond_init(&logcond, NULL);
01045 if (ast_pthread_create(&logthread, NULL, logger_thread, NULL) < 0) {
01046 ast_cond_destroy(&logcond);
01047 return -1;
01048 }
01049
01050
01051 ast_cli_register_multiple(cli_logger, ARRAY_LEN(cli_logger));
01052
01053 ast_mkdir(ast_config_AST_LOG_DIR, 0777);
01054
01055
01056 init_logger_chain(0 );
01057
01058 return 0;
01059 }
01060
01061 void close_logger(void)
01062 {
01063 struct logchannel *f = NULL;
01064
01065
01066 AST_LIST_LOCK(&logmsgs);
01067 close_logger_thread = 1;
01068 ast_cond_signal(&logcond);
01069 AST_LIST_UNLOCK(&logmsgs);
01070
01071 if (logthread != AST_PTHREADT_NULL)
01072 pthread_join(logthread, NULL);
01073
01074 AST_RWLIST_WRLOCK(&logchannels);
01075
01076 if (qlog) {
01077 fclose(qlog);
01078 qlog = NULL;
01079 }
01080
01081 AST_RWLIST_TRAVERSE(&logchannels, f, list) {
01082 if (f->fileptr && (f->fileptr != stdout) && (f->fileptr != stderr)) {
01083 fclose(f->fileptr);
01084 f->fileptr = NULL;
01085 }
01086 }
01087
01088 closelog();
01089
01090 AST_RWLIST_UNLOCK(&logchannels);
01091
01092 return;
01093 }
01094
01095
01096
01097
01098 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
01099 {
01100 struct logmsg *logmsg = NULL;
01101 struct ast_str *buf = NULL;
01102 struct ast_tm tm;
01103 struct timeval now = ast_tvnow();
01104 int res = 0;
01105 va_list ap;
01106 char datestring[256];
01107
01108 if (!(buf = ast_str_thread_get(&log_buf, LOG_BUF_INIT_SIZE)))
01109 return;
01110
01111 if (AST_RWLIST_EMPTY(&logchannels)) {
01112
01113
01114
01115
01116 if (level != __LOG_VERBOSE) {
01117 int result;
01118 va_start(ap, fmt);
01119 result = ast_str_set_va(&buf, BUFSIZ, fmt, ap);
01120 va_end(ap);
01121 if (result != AST_DYNSTR_BUILD_FAILED) {
01122 term_filter_escapes(ast_str_buffer(buf));
01123 fputs(ast_str_buffer(buf), stdout);
01124 }
01125 }
01126 return;
01127 }
01128
01129
01130
01131
01132
01133
01134
01135 if (!option_verbose && !option_debug && (level == __LOG_DEBUG))
01136 return;
01137
01138
01139 if (level != __LOG_VERBOSE && !(global_logmask & (1 << level)))
01140 return;
01141
01142
01143 va_start(ap, fmt);
01144 res = ast_str_set_va(&buf, BUFSIZ, fmt, ap);
01145 va_end(ap);
01146
01147
01148 if (res == AST_DYNSTR_BUILD_FAILED)
01149 return;
01150
01151
01152 if (!(logmsg = ast_calloc_with_stringfields(1, struct logmsg, res + 128)))
01153 return;
01154
01155
01156 ast_string_field_set(logmsg, message, ast_str_buffer(buf));
01157
01158
01159 if (level == __LOG_VERBOSE) {
01160 logmsg->type = LOGMSG_VERBOSE;
01161 } else {
01162 logmsg->type = LOGMSG_NORMAL;
01163 }
01164
01165
01166 ast_localtime(&now, &tm, NULL);
01167 ast_strftime(datestring, sizeof(datestring), dateformat, &tm);
01168 ast_string_field_set(logmsg, date, datestring);
01169
01170
01171 logmsg->level = level;
01172 logmsg->line = line;
01173 ast_string_field_set(logmsg, level_name, levels[level]);
01174 ast_string_field_set(logmsg, file, file);
01175 ast_string_field_set(logmsg, function, function);
01176 logmsg->process_id = (long) GETTID();
01177
01178
01179 if (logthread != AST_PTHREADT_NULL) {
01180 AST_LIST_LOCK(&logmsgs);
01181 AST_LIST_INSERT_TAIL(&logmsgs, logmsg, list);
01182 ast_cond_signal(&logcond);
01183 AST_LIST_UNLOCK(&logmsgs);
01184 } else {
01185 logger_print_normal(logmsg);
01186 ast_free(logmsg);
01187 }
01188
01189 return;
01190 }
01191
01192 #ifdef HAVE_BKTR
01193
01194 struct ast_bt *ast_bt_create(void)
01195 {
01196 struct ast_bt *bt = ast_calloc(1, sizeof(*bt));
01197 if (!bt) {
01198 ast_log(LOG_ERROR, "Unable to allocate memory for backtrace structure!\n");
01199 return NULL;
01200 }
01201
01202 bt->alloced = 1;
01203
01204 ast_bt_get_addresses(bt);
01205
01206 return bt;
01207 }
01208
01209 int ast_bt_get_addresses(struct ast_bt *bt)
01210 {
01211 bt->num_frames = backtrace(bt->addresses, AST_MAX_BT_FRAMES);
01212
01213 return 0;
01214 }
01215
01216 void *ast_bt_destroy(struct ast_bt *bt)
01217 {
01218 if (bt->alloced) {
01219 ast_free(bt);
01220 }
01221
01222 return NULL;
01223 }
01224
01225 #endif
01226
01227 void ast_backtrace(void)
01228 {
01229 #ifdef HAVE_BKTR
01230 struct ast_bt *bt;
01231 int i = 0;
01232 char **strings;
01233
01234 if (!(bt = ast_bt_create())) {
01235 ast_log(LOG_WARNING, "Unable to allocate space for backtrace structure\n");
01236 return;
01237 }
01238
01239 if ((strings = backtrace_symbols(bt->addresses, bt->num_frames))) {
01240 ast_debug(1, "Got %d backtrace record%c\n", bt->num_frames, bt->num_frames != 1 ? 's' : ' ');
01241 for (i = 3; i < bt->num_frames - 2; i++) {
01242 ast_log(LOG_DEBUG, "#%d: [%p] %s\n", i - 3, bt->addresses[i], strings[i]);
01243 }
01244
01245
01246 #undef free
01247 free(strings);
01248 } else {
01249 ast_debug(1, "Could not allocate memory for backtrace\n");
01250 }
01251 ast_bt_destroy(bt);
01252 #else
01253 ast_log(LOG_WARNING, "Must run configure with '--with-execinfo' for stack backtraces.\n");
01254 #endif
01255 }
01256
01257 void __ast_verbose_ap(const char *file, int line, const char *func, const char *fmt, va_list ap)
01258 {
01259 struct ast_str *buf = NULL;
01260 int res = 0;
01261
01262 if (!(buf = ast_str_thread_get(&verbose_buf, VERBOSE_BUF_INIT_SIZE)))
01263 return;
01264
01265 if (ast_opt_timestamp) {
01266 struct timeval now;
01267 struct ast_tm tm;
01268 char date[40];
01269 char *datefmt;
01270
01271 now = ast_tvnow();
01272 ast_localtime(&now, &tm, NULL);
01273 ast_strftime(date, sizeof(date), dateformat, &tm);
01274 datefmt = alloca(strlen(date) + 3 + strlen(fmt) + 1);
01275 sprintf(datefmt, "%c[%s] %s", 127, date, fmt);
01276 fmt = datefmt;
01277 } else {
01278 char *tmp = alloca(strlen(fmt) + 2);
01279 sprintf(tmp, "%c%s", 127, fmt);
01280 fmt = tmp;
01281 }
01282
01283
01284 res = ast_str_set_va(&buf, 0, fmt, ap);
01285
01286
01287 if (res == AST_DYNSTR_BUILD_FAILED)
01288 return;
01289
01290 ast_log(__LOG_VERBOSE, file, line, func, "%s", ast_str_buffer(buf));
01291 }
01292
01293 void __ast_verbose(const char *file, int line, const char *func, const char *fmt, ...)
01294 {
01295 va_list ap;
01296
01297 va_start(ap, fmt);
01298 __ast_verbose_ap(file, line, func, fmt, ap);
01299 va_end(ap);
01300 }
01301
01302
01303 #undef ast_verbose
01304 void __attribute__((format(printf, 1,2))) ast_verbose(const char *fmt, ...);
01305 void ast_verbose(const char *fmt, ...)
01306 {
01307 va_list ap;
01308
01309 va_start(ap, fmt);
01310 __ast_verbose_ap("", 0, "", fmt, ap);
01311 va_end(ap);
01312 }
01313
01314 int ast_register_verbose(void (*v)(const char *string))
01315 {
01316 struct verb *verb;
01317
01318 if (!(verb = ast_malloc(sizeof(*verb))))
01319 return -1;
01320
01321 verb->verboser = v;
01322
01323 AST_RWLIST_WRLOCK(&verbosers);
01324 AST_RWLIST_INSERT_HEAD(&verbosers, verb, list);
01325 AST_RWLIST_UNLOCK(&verbosers);
01326
01327 return 0;
01328 }
01329
01330 int ast_unregister_verbose(void (*v)(const char *string))
01331 {
01332 struct verb *cur;
01333
01334 AST_RWLIST_WRLOCK(&verbosers);
01335 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&verbosers, cur, list) {
01336 if (cur->verboser == v) {
01337 AST_RWLIST_REMOVE_CURRENT(list);
01338 ast_free(cur);
01339 break;
01340 }
01341 }
01342 AST_RWLIST_TRAVERSE_SAFE_END;
01343 AST_RWLIST_UNLOCK(&verbosers);
01344
01345 return cur ? 0 : -1;
01346 }
01347
01348 static void update_logchannels(void)
01349 {
01350 struct logchannel *cur;
01351
01352 AST_RWLIST_WRLOCK(&logchannels);
01353
01354 global_logmask = 0;
01355
01356 AST_RWLIST_TRAVERSE(&logchannels, cur, list) {
01357 cur->logmask = make_components(cur->components, cur->lineno);
01358 global_logmask |= cur->logmask;
01359 }
01360
01361 AST_RWLIST_UNLOCK(&logchannels);
01362 }
01363
01364 int ast_logger_register_level(const char *name)
01365 {
01366 unsigned int level;
01367 unsigned int available = 0;
01368
01369 AST_RWLIST_WRLOCK(&logchannels);
01370
01371 for (level = 0; level < ARRAY_LEN(levels); level++) {
01372 if ((level >= 16) && !available && !levels[level]) {
01373 available = level;
01374 continue;
01375 }
01376
01377 if (levels[level] && !strcasecmp(levels[level], name)) {
01378 ast_log(LOG_WARNING,
01379 "Unable to register dynamic logger level '%s': a standard logger level uses that name.\n",
01380 name);
01381 AST_RWLIST_UNLOCK(&logchannels);
01382
01383 return -1;
01384 }
01385 }
01386
01387 if (!available) {
01388 ast_log(LOG_WARNING,
01389 "Unable to register dynamic logger level '%s'; maximum number of levels registered.\n",
01390 name);
01391 AST_RWLIST_UNLOCK(&logchannels);
01392
01393 return -1;
01394 }
01395
01396 levels[available] = ast_strdup(name);
01397
01398 AST_RWLIST_UNLOCK(&logchannels);
01399
01400 ast_debug(1, "Registered dynamic logger level '%s' with index %d.\n", name, available);
01401
01402 update_logchannels();
01403
01404 return available;
01405 }
01406
01407 void ast_logger_unregister_level(const char *name)
01408 {
01409 unsigned int found = 0;
01410 unsigned int x;
01411
01412 AST_RWLIST_WRLOCK(&logchannels);
01413
01414 for (x = 16; x < ARRAY_LEN(levels); x++) {
01415 if (!levels[x]) {
01416 continue;
01417 }
01418
01419 if (strcasecmp(levels[x], name)) {
01420 continue;
01421 }
01422
01423 found = 1;
01424 break;
01425 }
01426
01427 if (found) {
01428
01429
01430
01431
01432 global_logmask &= ~(1 << x);
01433
01434 free(levels[x]);
01435 levels[x] = NULL;
01436 AST_RWLIST_UNLOCK(&logchannels);
01437
01438 ast_debug(1, "Unregistered dynamic logger level '%s' with index %d.\n", name, x);
01439
01440 update_logchannels();
01441 } else {
01442 AST_RWLIST_UNLOCK(&logchannels);
01443 }
01444 }
01445