logger.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. * \brief Asterisk Logger
  20. *
  21. * Logging routines
  22. *
  23. */
  24. #include <signal.h>
  25. #include <stdarg.h>
  26. #include <stdio.h>
  27. #include <unistd.h>
  28. #include <time.h>
  29. #include <string.h>
  30. #include <stdlib.h>
  31. #include <errno.h>
  32. #include <sys/stat.h>
  33. #define SYSLOG_NAMES /* so we can map syslog facilities names to their numeric values,
  34. from <syslog.h> which is included by logger.h */
  35. #include <syslog.h>
  36. #include "asterisk.h"
  37. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  38. static int syslog_level_map[] = {
  39. LOG_DEBUG,
  40. LOG_INFO, /* arbitrary equivalent of LOG_EVENT */
  41. LOG_NOTICE,
  42. LOG_WARNING,
  43. LOG_ERR,
  44. LOG_DEBUG,
  45. LOG_DEBUG
  46. };
  47. #define SYSLOG_NLEVELS 6
  48. #include "asterisk/logger.h"
  49. #include "asterisk/lock.h"
  50. #include "asterisk/options.h"
  51. #include "asterisk/channel.h"
  52. #include "asterisk/config.h"
  53. #include "asterisk/term.h"
  54. #include "asterisk/cli.h"
  55. #include "asterisk/utils.h"
  56. #include "asterisk/manager.h"
  57. #define MAX_MSG_QUEUE 200
  58. #if defined(__linux__) && !defined(__NR_gettid)
  59. #include <asm/unistd.h>
  60. #endif
  61. #if defined(__linux__) && defined(__NR_gettid)
  62. #define GETTID() syscall(__NR_gettid)
  63. #else
  64. #define GETTID() getpid()
  65. #endif
  66. static char dateformat[256] = "%b %e %T"; /* Original Asterisk Format */
  67. AST_MUTEX_DEFINE_STATIC(msglist_lock);
  68. AST_MUTEX_DEFINE_STATIC(loglock);
  69. static int filesize_reload_needed = 0;
  70. static int global_logmask = -1;
  71. static struct {
  72. unsigned int queue_log:1;
  73. unsigned int event_log:1;
  74. } logfiles = { 1, 1 };
  75. static struct msglist {
  76. char *msg;
  77. struct msglist *next;
  78. } *list = NULL, *last = NULL;
  79. static char hostname[MAXHOSTNAMELEN];
  80. enum logtypes {
  81. LOGTYPE_SYSLOG,
  82. LOGTYPE_FILE,
  83. LOGTYPE_CONSOLE,
  84. };
  85. struct logchannel {
  86. int logmask; /* What to log to this channel */
  87. int disabled; /* If this channel is disabled or not */
  88. int facility; /* syslog facility */
  89. enum logtypes type; /* Type of log channel */
  90. FILE *fileptr; /* logfile logging file pointer */
  91. char filename[256]; /* Filename */
  92. struct logchannel *next; /* Next channel in chain */
  93. };
  94. static struct logchannel *logchannels = NULL;
  95. static int msgcnt = 0;
  96. static FILE *eventlog = NULL;
  97. static char *levels[] = {
  98. "DEBUG",
  99. "EVENT",
  100. "NOTICE",
  101. "WARNING",
  102. "ERROR",
  103. "VERBOSE",
  104. "DTMF"
  105. };
  106. static int colors[] = {
  107. COLOR_BRGREEN,
  108. COLOR_BRBLUE,
  109. COLOR_YELLOW,
  110. COLOR_BRRED,
  111. COLOR_RED,
  112. COLOR_GREEN,
  113. COLOR_BRGREEN
  114. };
  115. static int make_components(char *s, int lineno)
  116. {
  117. char *w;
  118. int res = 0;
  119. char *stringp=NULL;
  120. stringp=s;
  121. w = strsep(&stringp, ",");
  122. while(w) {
  123. while(*w && (*w < 33))
  124. w++;
  125. if (!strcasecmp(w, "error"))
  126. res |= (1 << __LOG_ERROR);
  127. else if (!strcasecmp(w, "warning"))
  128. res |= (1 << __LOG_WARNING);
  129. else if (!strcasecmp(w, "notice"))
  130. res |= (1 << __LOG_NOTICE);
  131. else if (!strcasecmp(w, "event"))
  132. res |= (1 << __LOG_EVENT);
  133. else if (!strcasecmp(w, "debug"))
  134. res |= (1 << __LOG_DEBUG);
  135. else if (!strcasecmp(w, "verbose"))
  136. res |= (1 << __LOG_VERBOSE);
  137. else if (!strcasecmp(w, "dtmf"))
  138. res |= (1 << __LOG_DTMF);
  139. else {
  140. fprintf(stderr, "Logfile Warning: Unknown keyword '%s' at line %d of logger.conf\n", w, lineno);
  141. }
  142. w = strsep(&stringp, ",");
  143. }
  144. return res;
  145. }
  146. static struct logchannel *make_logchannel(char *channel, char *components, int lineno)
  147. {
  148. struct logchannel *chan;
  149. char *facility;
  150. #ifndef SOLARIS
  151. CODE *cptr;
  152. #endif
  153. if (ast_strlen_zero(channel))
  154. return NULL;
  155. chan = malloc(sizeof(struct logchannel));
  156. if (!chan) /* Can't allocate memory */
  157. return NULL;
  158. memset(chan, 0, sizeof(struct logchannel));
  159. if (!strcasecmp(channel, "console")) {
  160. chan->type = LOGTYPE_CONSOLE;
  161. } else if (!strncasecmp(channel, "syslog", 6)) {
  162. /*
  163. * syntax is:
  164. * syslog.facility => level,level,level
  165. */
  166. facility = strchr(channel, '.');
  167. if(!facility++ || !facility) {
  168. facility = "local0";
  169. }
  170. #ifndef SOLARIS
  171. /*
  172. * Walk through the list of facilitynames (defined in sys/syslog.h)
  173. * to see if we can find the one we have been given
  174. */
  175. chan->facility = -1;
  176. cptr = facilitynames;
  177. while (cptr->c_name) {
  178. if (!strcasecmp(facility, cptr->c_name)) {
  179. chan->facility = cptr->c_val;
  180. break;
  181. }
  182. cptr++;
  183. }
  184. #else
  185. chan->facility = -1;
  186. if (!strcasecmp(facility, "kern"))
  187. chan->facility = LOG_KERN;
  188. else if (!strcasecmp(facility, "USER"))
  189. chan->facility = LOG_USER;
  190. else if (!strcasecmp(facility, "MAIL"))
  191. chan->facility = LOG_MAIL;
  192. else if (!strcasecmp(facility, "DAEMON"))
  193. chan->facility = LOG_DAEMON;
  194. else if (!strcasecmp(facility, "AUTH"))
  195. chan->facility = LOG_AUTH;
  196. else if (!strcasecmp(facility, "SYSLOG"))
  197. chan->facility = LOG_SYSLOG;
  198. else if (!strcasecmp(facility, "LPR"))
  199. chan->facility = LOG_LPR;
  200. else if (!strcasecmp(facility, "NEWS"))
  201. chan->facility = LOG_NEWS;
  202. else if (!strcasecmp(facility, "UUCP"))
  203. chan->facility = LOG_UUCP;
  204. else if (!strcasecmp(facility, "CRON"))
  205. chan->facility = LOG_CRON;
  206. else if (!strcasecmp(facility, "LOCAL0"))
  207. chan->facility = LOG_LOCAL0;
  208. else if (!strcasecmp(facility, "LOCAL1"))
  209. chan->facility = LOG_LOCAL1;
  210. else if (!strcasecmp(facility, "LOCAL2"))
  211. chan->facility = LOG_LOCAL2;
  212. else if (!strcasecmp(facility, "LOCAL3"))
  213. chan->facility = LOG_LOCAL3;
  214. else if (!strcasecmp(facility, "LOCAL4"))
  215. chan->facility = LOG_LOCAL4;
  216. else if (!strcasecmp(facility, "LOCAL5"))
  217. chan->facility = LOG_LOCAL5;
  218. else if (!strcasecmp(facility, "LOCAL6"))
  219. chan->facility = LOG_LOCAL6;
  220. else if (!strcasecmp(facility, "LOCAL7"))
  221. chan->facility = LOG_LOCAL7;
  222. #endif /* Solaris */
  223. if (0 > chan->facility) {
  224. fprintf(stderr, "Logger Warning: bad syslog facility in logger.conf\n");
  225. free(chan);
  226. return NULL;
  227. }
  228. chan->type = LOGTYPE_SYSLOG;
  229. snprintf(chan->filename, sizeof(chan->filename), "%s", channel);
  230. openlog("asterisk", LOG_PID, chan->facility);
  231. } else {
  232. if (channel[0] == '/') {
  233. if(!ast_strlen_zero(hostname)) {
  234. snprintf(chan->filename, sizeof(chan->filename) - 1,"%s.%s", channel, hostname);
  235. } else {
  236. ast_copy_string(chan->filename, channel, sizeof(chan->filename));
  237. }
  238. }
  239. if(!ast_strlen_zero(hostname)) {
  240. snprintf(chan->filename, sizeof(chan->filename), "%s/%s.%s",(char *)ast_config_AST_LOG_DIR, channel, hostname);
  241. } else {
  242. snprintf(chan->filename, sizeof(chan->filename), "%s/%s", (char *)ast_config_AST_LOG_DIR, channel);
  243. }
  244. chan->fileptr = fopen(chan->filename, "a");
  245. if (!chan->fileptr) {
  246. /* Can't log here, since we're called with a lock */
  247. fprintf(stderr, "Logger Warning: Unable to open log file '%s': %s\n", chan->filename, strerror(errno));
  248. }
  249. chan->type = LOGTYPE_FILE;
  250. }
  251. chan->logmask = make_components(components, lineno);
  252. return chan;
  253. }
  254. static void init_logger_chain(void)
  255. {
  256. struct logchannel *chan, *cur;
  257. struct ast_config *cfg;
  258. struct ast_variable *var;
  259. char *s;
  260. /* delete our list of log channels */
  261. ast_mutex_lock(&loglock);
  262. chan = logchannels;
  263. while (chan) {
  264. cur = chan->next;
  265. free(chan);
  266. chan = cur;
  267. }
  268. logchannels = NULL;
  269. ast_mutex_unlock(&loglock);
  270. global_logmask = 0;
  271. /* close syslog */
  272. closelog();
  273. cfg = ast_config_load("logger.conf");
  274. /* If no config file, we're fine */
  275. if (!cfg)
  276. return;
  277. ast_mutex_lock(&loglock);
  278. if ((s = ast_variable_retrieve(cfg, "general", "appendhostname"))) {
  279. if(ast_true(s)) {
  280. if(gethostname(hostname, sizeof(hostname)-1)) {
  281. ast_copy_string(hostname, "unknown", sizeof(hostname));
  282. ast_log(LOG_WARNING, "What box has no hostname???\n");
  283. }
  284. } else
  285. hostname[0] = '\0';
  286. } else
  287. hostname[0] = '\0';
  288. if ((s = ast_variable_retrieve(cfg, "general", "dateformat"))) {
  289. ast_copy_string(dateformat, s, sizeof(dateformat));
  290. } else
  291. ast_copy_string(dateformat, "%b %e %T", sizeof(dateformat));
  292. if ((s = ast_variable_retrieve(cfg, "general", "queue_log"))) {
  293. logfiles.queue_log = ast_true(s);
  294. }
  295. if ((s = ast_variable_retrieve(cfg, "general", "event_log"))) {
  296. logfiles.event_log = ast_true(s);
  297. }
  298. var = ast_variable_browse(cfg, "logfiles");
  299. while(var) {
  300. chan = make_logchannel(var->name, var->value, var->lineno);
  301. if (chan) {
  302. chan->next = logchannels;
  303. logchannels = chan;
  304. global_logmask |= chan->logmask;
  305. }
  306. var = var->next;
  307. }
  308. ast_config_destroy(cfg);
  309. ast_mutex_unlock(&loglock);
  310. }
  311. static FILE *qlog = NULL;
  312. AST_MUTEX_DEFINE_STATIC(qloglock);
  313. void ast_queue_log(const char *queuename, const char *callid, const char *agent, const char *event, const char *fmt, ...)
  314. {
  315. va_list ap;
  316. ast_mutex_lock(&qloglock);
  317. if (qlog) {
  318. va_start(ap, fmt);
  319. fprintf(qlog, "%ld|%s|%s|%s|%s|", (long)time(NULL), callid, queuename, agent, event);
  320. vfprintf(qlog, fmt, ap);
  321. fprintf(qlog, "\n");
  322. va_end(ap);
  323. fflush(qlog);
  324. }
  325. ast_mutex_unlock(&qloglock);
  326. }
  327. static void queue_log_init(void)
  328. {
  329. char filename[256];
  330. int reloaded = 0;
  331. ast_mutex_lock(&qloglock);
  332. if (qlog) {
  333. reloaded = 1;
  334. fclose(qlog);
  335. qlog = NULL;
  336. }
  337. snprintf(filename, sizeof(filename), "%s/%s", (char *)ast_config_AST_LOG_DIR, "queue_log");
  338. if (logfiles.queue_log) {
  339. qlog = fopen(filename, "a");
  340. }
  341. ast_mutex_unlock(&qloglock);
  342. if (reloaded)
  343. ast_queue_log("NONE", "NONE", "NONE", "CONFIGRELOAD", "%s", "");
  344. else
  345. ast_queue_log("NONE", "NONE", "NONE", "QUEUESTART", "%s", "");
  346. }
  347. int reload_logger(int rotate)
  348. {
  349. char old[AST_CONFIG_MAX_PATH] = "";
  350. char new[AST_CONFIG_MAX_PATH];
  351. struct logchannel *f;
  352. FILE *myf;
  353. int x;
  354. ast_mutex_lock(&loglock);
  355. if (eventlog)
  356. fclose(eventlog);
  357. else
  358. rotate = 0;
  359. eventlog = NULL;
  360. mkdir((char *)ast_config_AST_LOG_DIR, 0755);
  361. snprintf(old, sizeof(old), "%s/%s", (char *)ast_config_AST_LOG_DIR, EVENTLOG);
  362. if (logfiles.event_log) {
  363. if (rotate) {
  364. for (x=0;;x++) {
  365. snprintf(new, sizeof(new), "%s/%s.%d", (char *)ast_config_AST_LOG_DIR, EVENTLOG,x);
  366. myf = fopen((char *)new, "r");
  367. if (myf) /* File exists */
  368. fclose(myf);
  369. else
  370. break;
  371. }
  372. /* do it */
  373. if (rename(old,new))
  374. fprintf(stderr, "Unable to rename file '%s' to '%s'\n", old, new);
  375. }
  376. eventlog = fopen(old, "a");
  377. }
  378. f = logchannels;
  379. while(f) {
  380. if (f->disabled) {
  381. f->disabled = 0; /* Re-enable logging at reload */
  382. manager_event(EVENT_FLAG_SYSTEM, "LogChannel", "Channel: %s\r\nEnabled: Yes\r\n", f->filename);
  383. }
  384. if (f->fileptr && (f->fileptr != stdout) && (f->fileptr != stderr)) {
  385. fclose(f->fileptr); /* Close file */
  386. f->fileptr = NULL;
  387. if(rotate) {
  388. ast_copy_string(old, f->filename, sizeof(old));
  389. for(x=0;;x++) {
  390. snprintf(new, sizeof(new), "%s.%d", f->filename, x);
  391. myf = fopen((char *)new, "r");
  392. if (myf) {
  393. fclose(myf);
  394. } else {
  395. break;
  396. }
  397. }
  398. /* do it */
  399. if (rename(old,new))
  400. fprintf(stderr, "Unable to rename file '%s' to '%s'\n", old, new);
  401. }
  402. }
  403. f = f->next;
  404. }
  405. ast_mutex_unlock(&loglock);
  406. filesize_reload_needed = 0;
  407. queue_log_init();
  408. init_logger_chain();
  409. if (logfiles.event_log) {
  410. if (eventlog) {
  411. ast_log(LOG_EVENT, "Restarted Asterisk Event Logger\n");
  412. if (option_verbose)
  413. ast_verbose("Asterisk Event Logger restarted\n");
  414. return 0;
  415. } else
  416. ast_log(LOG_ERROR, "Unable to create event log: %s\n", strerror(errno));
  417. } else
  418. return 0;
  419. return -1;
  420. }
  421. static int handle_logger_reload(int fd, int argc, char *argv[])
  422. {
  423. if(reload_logger(0)) {
  424. ast_cli(fd, "Failed to reload the logger\n");
  425. return RESULT_FAILURE;
  426. } else
  427. return RESULT_SUCCESS;
  428. }
  429. static int handle_logger_rotate(int fd, int argc, char *argv[])
  430. {
  431. if(reload_logger(1)) {
  432. ast_cli(fd, "Failed to reload the logger and rotate log files\n");
  433. return RESULT_FAILURE;
  434. } else
  435. return RESULT_SUCCESS;
  436. }
  437. /*--- handle_logger_show_channels: CLI command to show logging system
  438. configuration */
  439. static int handle_logger_show_channels(int fd, int argc, char *argv[])
  440. {
  441. #define FORMATL "%-35.35s %-8.8s %-9.9s "
  442. struct logchannel *chan;
  443. ast_mutex_lock(&loglock);
  444. chan = logchannels;
  445. ast_cli(fd,FORMATL, "Channel", "Type", "Status");
  446. ast_cli(fd, "Configuration\n");
  447. ast_cli(fd,FORMATL, "-------", "----", "------");
  448. ast_cli(fd, "-------------\n");
  449. while (chan) {
  450. ast_cli(fd, FORMATL, chan->filename, chan->type==LOGTYPE_CONSOLE ? "Console" : (chan->type==LOGTYPE_SYSLOG ? "Syslog" : "File"),
  451. chan->disabled ? "Disabled" : "Enabled");
  452. ast_cli(fd, " - ");
  453. if (chan->logmask & (1 << __LOG_DEBUG))
  454. ast_cli(fd, "Debug ");
  455. if (chan->logmask & (1 << __LOG_DTMF))
  456. ast_cli(fd, "DTMF ");
  457. if (chan->logmask & (1 << __LOG_VERBOSE))
  458. ast_cli(fd, "Verbose ");
  459. if (chan->logmask & (1 << __LOG_WARNING))
  460. ast_cli(fd, "Warning ");
  461. if (chan->logmask & (1 << __LOG_NOTICE))
  462. ast_cli(fd, "Notice ");
  463. if (chan->logmask & (1 << __LOG_ERROR))
  464. ast_cli(fd, "Error ");
  465. if (chan->logmask & (1 << __LOG_EVENT))
  466. ast_cli(fd, "Event ");
  467. ast_cli(fd, "\n");
  468. chan = chan->next;
  469. }
  470. ast_cli(fd, "\n");
  471. ast_mutex_unlock(&loglock);
  472. return RESULT_SUCCESS;
  473. }
  474. static struct verb {
  475. void (*verboser)(const char *string, int opos, int replacelast, int complete);
  476. struct verb *next;
  477. } *verboser = NULL;
  478. static char logger_reload_help[] =
  479. "Usage: logger reload\n"
  480. " Reloads the logger subsystem state. Use after restarting syslogd(8) if you are using syslog logging.\n";
  481. static char logger_rotate_help[] =
  482. "Usage: logger rotate\n"
  483. " Rotates and Reopens the log files.\n";
  484. static char logger_show_channels_help[] =
  485. "Usage: logger show channels\n"
  486. " Show configured logger channels.\n";
  487. static struct ast_cli_entry logger_show_channels_cli =
  488. { { "logger", "show", "channels", NULL },
  489. handle_logger_show_channels, "List configured log channels",
  490. logger_show_channels_help };
  491. static struct ast_cli_entry reload_logger_cli =
  492. { { "logger", "reload", NULL },
  493. handle_logger_reload, "Reopens the log files",
  494. logger_reload_help };
  495. static struct ast_cli_entry rotate_logger_cli =
  496. { { "logger", "rotate", NULL },
  497. handle_logger_rotate, "Rotates and reopens the log files",
  498. logger_rotate_help };
  499. static int handle_SIGXFSZ(int sig)
  500. {
  501. /* Indicate need to reload */
  502. filesize_reload_needed = 1;
  503. return 0;
  504. }
  505. int init_logger(void)
  506. {
  507. char tmp[256];
  508. /* auto rotate if sig SIGXFSZ comes a-knockin */
  509. (void) signal(SIGXFSZ,(void *) handle_SIGXFSZ);
  510. /* register the relaod logger cli command */
  511. ast_cli_register(&reload_logger_cli);
  512. ast_cli_register(&rotate_logger_cli);
  513. ast_cli_register(&logger_show_channels_cli);
  514. /* initialize queue logger */
  515. queue_log_init();
  516. /* create log channels */
  517. init_logger_chain();
  518. /* create the eventlog */
  519. if (logfiles.event_log) {
  520. mkdir((char *)ast_config_AST_LOG_DIR, 0755);
  521. snprintf(tmp, sizeof(tmp), "%s/%s", (char *)ast_config_AST_LOG_DIR, EVENTLOG);
  522. eventlog = fopen((char *)tmp, "a");
  523. if (eventlog) {
  524. ast_log(LOG_EVENT, "Started Asterisk Event Logger\n");
  525. if (option_verbose)
  526. ast_verbose("Asterisk Event Logger Started %s\n",(char *)tmp);
  527. return 0;
  528. } else
  529. ast_log(LOG_ERROR, "Unable to create event log: %s\n", strerror(errno));
  530. } else
  531. return 0;
  532. return -1;
  533. }
  534. void close_logger(void)
  535. {
  536. struct msglist *m, *tmp;
  537. ast_mutex_lock(&msglist_lock);
  538. m = list;
  539. while(m) {
  540. if (m->msg) {
  541. free(m->msg);
  542. }
  543. tmp = m->next;
  544. free(m);
  545. m = tmp;
  546. }
  547. list = last = NULL;
  548. msgcnt = 0;
  549. ast_mutex_unlock(&msglist_lock);
  550. return;
  551. }
  552. static void strip_coloring(char *str)
  553. {
  554. char *src, *dest, *end;
  555. if (!str)
  556. return;
  557. /* find the first potential escape sequence in the string */
  558. src = strchr(str, '\033');
  559. if (!src)
  560. return;
  561. dest = src;
  562. while (*src) {
  563. /* at the top of this loop, *src will always be an ESC character */
  564. if ((src[1] == '[') && ((end = strchr(src + 2, 'm'))))
  565. src = end + 1;
  566. else
  567. *dest++ = *src++;
  568. /* copy characters, checking for ESC as we go */
  569. while (*src && (*src != '\033'))
  570. *dest++ = *src++;
  571. }
  572. *dest = '\0';
  573. }
  574. static void ast_log_vsyslog(int level, const char *file, int line, const char *function, const char *fmt, va_list args)
  575. {
  576. char buf[BUFSIZ];
  577. char *s;
  578. if (level >= SYSLOG_NLEVELS) {
  579. /* we are locked here, so cannot ast_log() */
  580. fprintf(stderr, "ast_log_vsyslog called with bogus level: %d\n", level);
  581. return;
  582. }
  583. if (level == __LOG_VERBOSE) {
  584. snprintf(buf, sizeof(buf), "VERBOSE[%ld]: ", (long)GETTID());
  585. level = __LOG_DEBUG;
  586. } else if (level == __LOG_DTMF) {
  587. snprintf(buf, sizeof(buf), "DTMF[%ld]: ", (long)GETTID());
  588. level = __LOG_DEBUG;
  589. } else {
  590. snprintf(buf, sizeof(buf), "%s[%ld]: %s:%d in %s: ",
  591. levels[level], (long)GETTID(), file, line, function);
  592. }
  593. s = buf + strlen(buf);
  594. vsnprintf(s, sizeof(buf) - strlen(buf), fmt, args);
  595. strip_coloring(s);
  596. syslog(syslog_level_map[level], "%s", buf);
  597. }
  598. /*
  599. * send log messages to syslog and/or the console
  600. */
  601. void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
  602. {
  603. struct logchannel *chan;
  604. char buf[BUFSIZ];
  605. time_t t;
  606. struct tm tm;
  607. char date[256];
  608. va_list ap;
  609. /* don't display LOG_DEBUG messages unless option_verbose _or_ option_debug
  610. are non-zero; LOG_DEBUG messages can still be displayed if option_debug
  611. is zero, if option_verbose is non-zero (this allows for 'level zero'
  612. LOG_DEBUG messages to be displayed, if the logmask on any channel
  613. allows it)
  614. */
  615. if (!option_verbose && !option_debug && (level == __LOG_DEBUG)) {
  616. return;
  617. }
  618. /* Ignore anything that never gets logged anywhere */
  619. if (!(global_logmask & (1 << level)))
  620. return;
  621. /* Ignore anything other than the currently debugged file if there is one */
  622. if ((level == __LOG_DEBUG) && !ast_strlen_zero(debug_filename) && strcasecmp(debug_filename, file))
  623. return;
  624. /* begin critical section */
  625. ast_mutex_lock(&loglock);
  626. time(&t);
  627. localtime_r(&t, &tm);
  628. strftime(date, sizeof(date), dateformat, &tm);
  629. if (logfiles.event_log && level == __LOG_EVENT) {
  630. va_start(ap, fmt);
  631. fprintf(eventlog, "%s asterisk[%d]: ", date, getpid());
  632. vfprintf(eventlog, fmt, ap);
  633. fflush(eventlog);
  634. va_end(ap);
  635. ast_mutex_unlock(&loglock);
  636. return;
  637. }
  638. if (logchannels) {
  639. chan = logchannels;
  640. while(chan && !chan->disabled) {
  641. /* Check syslog channels */
  642. if (chan->type == LOGTYPE_SYSLOG && (chan->logmask & (1 << level))) {
  643. va_start(ap, fmt);
  644. ast_log_vsyslog(level, file, line, function, fmt, ap);
  645. va_end(ap);
  646. /* Console channels */
  647. } else if ((chan->logmask & (1 << level)) && (chan->type == LOGTYPE_CONSOLE)) {
  648. char linestr[128];
  649. char tmp1[80], tmp2[80], tmp3[80], tmp4[80];
  650. if (level != __LOG_VERBOSE) {
  651. sprintf(linestr, "%d", line);
  652. snprintf(buf, sizeof(buf), option_timestamp ? "[%s] %s[%ld]: %s:%s %s: " : "%s %s[%ld]: %s:%s %s: ",
  653. date,
  654. term_color(tmp1, levels[level], colors[level], 0, sizeof(tmp1)),
  655. (long)GETTID(),
  656. term_color(tmp2, file, COLOR_BRWHITE, 0, sizeof(tmp2)),
  657. term_color(tmp3, linestr, COLOR_BRWHITE, 0, sizeof(tmp3)),
  658. term_color(tmp4, function, COLOR_BRWHITE, 0, sizeof(tmp4)));
  659. ast_console_puts(buf);
  660. va_start(ap, fmt);
  661. vsnprintf(buf, sizeof(buf), fmt, ap);
  662. va_end(ap);
  663. ast_console_puts(buf);
  664. }
  665. /* File channels */
  666. } else if ((chan->logmask & (1 << level)) && (chan->fileptr)) {
  667. int res;
  668. snprintf(buf, sizeof(buf), option_timestamp ? "[%s] %s[%ld]: " : "%s %s[%ld] %s: ", date,
  669. levels[level], (long)GETTID(), file);
  670. res = fprintf(chan->fileptr, buf);
  671. if (res <= 0 && buf[0] != '\0') { /* Error, no characters printed */
  672. fprintf(stderr,"**** Asterisk Logging Error: ***********\n");
  673. if (errno == ENOMEM || errno == ENOSPC) {
  674. fprintf(stderr, "Asterisk logging error: Out of disk space, can't log to log file %s\n", chan->filename);
  675. } else
  676. fprintf(stderr, "Logger Warning: Unable to write to log file '%s': %s (disabled)\n", chan->filename, strerror(errno));
  677. manager_event(EVENT_FLAG_SYSTEM, "LogChannel", "Channel: %s\r\nEnabled: No\r\nReason: %d - %s\r\n", chan->filename, errno, strerror(errno));
  678. chan->disabled = 1;
  679. } else {
  680. /* No error message, continue printing */
  681. va_start(ap, fmt);
  682. vsnprintf(buf, sizeof(buf), fmt, ap);
  683. va_end(ap);
  684. strip_coloring(buf);
  685. fputs(buf, chan->fileptr);
  686. fflush(chan->fileptr);
  687. }
  688. }
  689. chan = chan->next;
  690. }
  691. } else {
  692. /*
  693. * we don't have the logger chain configured yet,
  694. * so just log to stdout
  695. */
  696. if (level != __LOG_VERBOSE) {
  697. va_start(ap, fmt);
  698. vsnprintf(buf, sizeof(buf), fmt, ap);
  699. va_end(ap);
  700. fputs(buf, stdout);
  701. }
  702. }
  703. ast_mutex_unlock(&loglock);
  704. /* end critical section */
  705. if (filesize_reload_needed) {
  706. reload_logger(1);
  707. ast_log(LOG_EVENT,"Rotated Logs Per SIGXFSZ (Exceeded file size limit)\n");
  708. if (option_verbose)
  709. ast_verbose("Rotated Logs Per SIGXFSZ (Exceeded file size limit)\n");
  710. }
  711. }
  712. void ast_verbose(const char *fmt, ...)
  713. {
  714. static char stuff[4096];
  715. static int len = 0;
  716. static int replacelast = 0;
  717. int complete;
  718. int olen;
  719. struct msglist *m;
  720. struct verb *v;
  721. va_list ap;
  722. va_start(ap, fmt);
  723. if (option_timestamp) {
  724. time_t t;
  725. struct tm tm;
  726. char date[40];
  727. char *datefmt;
  728. time(&t);
  729. localtime_r(&t, &tm);
  730. strftime(date, sizeof(date), dateformat, &tm);
  731. datefmt = alloca(strlen(date) + 3 + strlen(fmt) + 1);
  732. if (datefmt) {
  733. sprintf(datefmt, "[%s] %s", date, fmt);
  734. fmt = datefmt;
  735. }
  736. }
  737. /* this lock is also protecting against multiple threads
  738. being in this function at the same time, so it must be
  739. held before any of the static variables are accessed
  740. */
  741. ast_mutex_lock(&msglist_lock);
  742. /* there is a potential security problem here: if formatting
  743. the current date using 'dateformat' results in a string
  744. containing '%', then the vsnprintf() call below will
  745. probably try to access random memory
  746. */
  747. vsnprintf(stuff + len, sizeof(stuff) - len, fmt, ap);
  748. va_end(ap);
  749. olen = len;
  750. len = strlen(stuff);
  751. complete = (stuff[len - 1] == '\n') ? 1 : 0;
  752. /* If we filled up the stuff completely, then log it even without the '\n' */
  753. if (len >= sizeof(stuff) - 1) {
  754. complete = 1;
  755. len = 0;
  756. }
  757. if (complete) {
  758. if (msgcnt < MAX_MSG_QUEUE) {
  759. /* Allocate new structure */
  760. if ((m = malloc(sizeof(*m))))
  761. msgcnt++;
  762. } else {
  763. /* Recycle the oldest entry */
  764. m = list;
  765. list = list->next;
  766. free(m->msg);
  767. }
  768. if (m) {
  769. m->msg = strdup(stuff);
  770. if (m->msg) {
  771. if (last)
  772. last->next = m;
  773. else
  774. list = m;
  775. m->next = NULL;
  776. last = m;
  777. } else {
  778. msgcnt--;
  779. ast_log(LOG_ERROR, "Out of memory\n");
  780. free(m);
  781. }
  782. }
  783. }
  784. for (v = verboser; v; v = v->next)
  785. v->verboser(stuff, olen, replacelast, complete);
  786. ast_log(LOG_VERBOSE, "%s", stuff);
  787. if (len) {
  788. if (!complete)
  789. replacelast = 1;
  790. else
  791. replacelast = len = 0;
  792. }
  793. ast_mutex_unlock(&msglist_lock);
  794. }
  795. int ast_verbose_dmesg(void (*v)(const char *string, int opos, int replacelast, int complete))
  796. {
  797. struct msglist *m;
  798. ast_mutex_lock(&msglist_lock);
  799. m = list;
  800. while(m) {
  801. /* Send all the existing entries that we have queued (i.e. they're likely to have missed) */
  802. v(m->msg, 0, 0, 1);
  803. m = m->next;
  804. }
  805. ast_mutex_unlock(&msglist_lock);
  806. return 0;
  807. }
  808. int ast_register_verbose(void (*v)(const char *string, int opos, int replacelast, int complete))
  809. {
  810. struct msglist *m;
  811. struct verb *tmp;
  812. /* XXX Should be more flexible here, taking > 1 verboser XXX */
  813. if ((tmp = malloc(sizeof (struct verb)))) {
  814. tmp->verboser = v;
  815. ast_mutex_lock(&msglist_lock);
  816. tmp->next = verboser;
  817. verboser = tmp;
  818. m = list;
  819. while(m) {
  820. /* Send all the existing entries that we have queued (i.e. they're likely to have missed) */
  821. v(m->msg, 0, 0, 1);
  822. m = m->next;
  823. }
  824. ast_mutex_unlock(&msglist_lock);
  825. return 0;
  826. }
  827. return -1;
  828. }
  829. int ast_unregister_verbose(void (*v)(const char *string, int opos, int replacelast, int complete))
  830. {
  831. int res = -1;
  832. struct verb *tmp, *tmpl=NULL;
  833. ast_mutex_lock(&msglist_lock);
  834. tmp = verboser;
  835. while(tmp) {
  836. if (tmp->verboser == v) {
  837. if (tmpl)
  838. tmpl->next = tmp->next;
  839. else
  840. verboser = tmp->next;
  841. free(tmp);
  842. break;
  843. }
  844. tmpl = tmp;
  845. tmp = tmp->next;
  846. }
  847. if (tmp)
  848. res = 0;
  849. ast_mutex_unlock(&msglist_lock);
  850. return res;
  851. }