4 * Mark Spencer <markster@marko.net>
6 * Copyright(C)1999, Linux Support Services, Inc.
8 * Distributed under the terms of the GNU General Public License (GPL) Version 2
19 #include <asterisk/lock.h>
20 #include <asterisk/options.h>
21 #include <asterisk/channel.h>
22 #include <asterisk/config.h>
23 #include <asterisk/term.h>
24 #include <asterisk/cli.h>
25 #include <asterisk/utils.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 */
36 static int syslog_level_map[] = {
38 LOG_INFO, /* arbitrary equivalent of LOG_EVENT */
45 #define SYSLOG_NLEVELS 6
47 #include <asterisk/logger.h>
49 #define MAX_MSG_QUEUE 200
51 #if defined(__linux__) && defined(__NR_gettid)
52 #include <asm/unistd.h>
53 #define GETTID() syscall(__NR_gettid)
55 #define GETTID() getpid()
58 static char dateformat[256] = "%b %e %T"; /* Original Asterisk Format */
59 AST_MUTEX_DEFINE_STATIC(msglist_lock);
60 AST_MUTEX_DEFINE_STATIC(loglock);
61 static int pending_logger_reload = 0;
62 static int global_logmask = -1;
65 unsigned int queue_log:1;
66 unsigned int event_log:1;
67 } logfiles = { 1, 1 };
69 static struct msglist {
72 } *list = NULL, *last = NULL;
74 static char hostname[256];
78 int facility; /* syslog */
79 int syslog; /* syslog flag */
80 int console; /* console logging */
81 FILE *fileptr; /* logfile logging */
83 struct logchannel *next;
86 static struct logchannel *logchannels = NULL;
88 static int msgcnt = 0;
90 static FILE *eventlog = NULL;
92 static char *levels[] = {
101 static int colors[] = {
110 static int make_components(char *s, int lineno)
116 w = strsep(&stringp, ",");
118 while(*w && (*w < 33))
120 if (!strcasecmp(w, "error"))
121 res |= (1 << __LOG_ERROR);
122 else if (!strcasecmp(w, "warning"))
123 res |= (1 << __LOG_WARNING);
124 else if (!strcasecmp(w, "notice"))
125 res |= (1 << __LOG_NOTICE);
126 else if (!strcasecmp(w, "event"))
127 res |= (1 << __LOG_EVENT);
128 else if (!strcasecmp(w, "debug"))
129 res |= (1 << __LOG_DEBUG);
130 else if (!strcasecmp(w, "verbose"))
131 res |= (1 << __LOG_VERBOSE);
133 fprintf(stderr, "Logfile Warning: Unknown keyword '%s' at line %d of logger.conf\n", w, lineno);
135 w = strsep(&stringp, ",");
140 static struct logchannel *make_logchannel(char *channel, char *components, int lineno)
142 struct logchannel *chan;
148 if (ast_strlen_zero(channel))
150 chan = malloc(sizeof(struct logchannel));
153 memset(chan, 0, sizeof(struct logchannel));
154 if (!strcasecmp(channel, "console")) {
156 } else if (!strncasecmp(channel, "syslog", 6)) {
159 * syslog.facility => level,level,level
161 facility = strchr(channel, '.');
162 if(!facility++ || !facility) {
168 * Walk through the list of facilitynames (defined in sys/syslog.h)
169 * to see if we can find the one we have been given
172 cptr = facilitynames;
173 while (cptr->c_name) {
174 if (!strcasecmp(facility, cptr->c_name)) {
175 chan->facility = cptr->c_val;
182 if (!strcasecmp(facility, "kern"))
183 chan->facility = LOG_KERN;
184 else if (!strcasecmp(facility, "USER"))
185 chan->facility = LOG_USER;
186 else if (!strcasecmp(facility, "MAIL"))
187 chan->facility = LOG_MAIL;
188 else if (!strcasecmp(facility, "DAEMON"))
189 chan->facility = LOG_DAEMON;
190 else if (!strcasecmp(facility, "AUTH"))
191 chan->facility = LOG_AUTH;
192 else if (!strcasecmp(facility, "SYSLOG"))
193 chan->facility = LOG_SYSLOG;
194 else if (!strcasecmp(facility, "LPR"))
195 chan->facility = LOG_LPR;
196 else if (!strcasecmp(facility, "NEWS"))
197 chan->facility = LOG_NEWS;
198 else if (!strcasecmp(facility, "UUCP"))
199 chan->facility = LOG_UUCP;
200 else if (!strcasecmp(facility, "CRON"))
201 chan->facility = LOG_CRON;
202 else if (!strcasecmp(facility, "LOCAL0"))
203 chan->facility = LOG_LOCAL0;
204 else if (!strcasecmp(facility, "LOCAL1"))
205 chan->facility = LOG_LOCAL1;
206 else if (!strcasecmp(facility, "LOCAL2"))
207 chan->facility = LOG_LOCAL2;
208 else if (!strcasecmp(facility, "LOCAL3"))
209 chan->facility = LOG_LOCAL3;
210 else if (!strcasecmp(facility, "LOCAL4"))
211 chan->facility = LOG_LOCAL4;
212 else if (!strcasecmp(facility, "LOCAL5"))
213 chan->facility = LOG_LOCAL5;
214 else if (!strcasecmp(facility, "LOCAL6"))
215 chan->facility = LOG_LOCAL6;
216 else if (!strcasecmp(facility, "LOCAL7"))
217 chan->facility = LOG_LOCAL7;
220 if (0 > chan->facility) {
221 fprintf(stderr, "Logger Warning: bad syslog facility in logger.conf\n");
227 openlog("asterisk", LOG_PID, chan->facility);
229 if (channel[0] == '/') {
230 if(!ast_strlen_zero(hostname)) {
231 snprintf(chan->filename, sizeof(chan->filename) - 1,"%s.%s", channel, hostname);
233 strncpy(chan->filename, channel, sizeof(chan->filename) - 1);
237 if(!ast_strlen_zero(hostname)) {
238 snprintf(chan->filename, sizeof(chan->filename), "%s/%s.%s",(char *)ast_config_AST_LOG_DIR, channel, hostname);
240 snprintf(chan->filename, sizeof(chan->filename), "%s/%s", (char *)ast_config_AST_LOG_DIR, channel);
242 chan->fileptr = fopen(chan->filename, "a");
243 if (!chan->fileptr) {
244 /* Can't log here, since we're called with a lock */
245 fprintf(stderr, "Logger Warning: Unable to open log file '%s': %s\n", chan->filename, strerror(errno));
248 chan->logmask = make_components(components, lineno);
253 static void init_logger_chain(void)
255 struct logchannel *chan, *cur;
256 struct ast_config *cfg;
257 struct ast_variable *var;
260 /* delete our list of log channels */
261 ast_mutex_lock(&loglock);
269 ast_mutex_unlock(&loglock);
275 cfg = ast_config_load("logger.conf");
277 /* If no config file, we're fine */
281 ast_mutex_lock(&loglock);
282 if ((s = ast_variable_retrieve(cfg, "general", "appendhostname"))) {
284 if(gethostname(hostname, sizeof(hostname))) {
285 strncpy(hostname, "unknown", sizeof(hostname)-1);
286 ast_log(LOG_WARNING, "What box has no hostname???\n");
292 if ((s = ast_variable_retrieve(cfg, "general", "dateformat"))) {
293 strncpy(dateformat, s, sizeof(dateformat) - 1);
295 strncpy(dateformat, "%b %e %T", sizeof(dateformat) - 1);
296 if ((s = ast_variable_retrieve(cfg, "general", "queue_log"))) {
297 logfiles.queue_log = ast_true(s);
299 if ((s = ast_variable_retrieve(cfg, "general", "event_log"))) {
300 logfiles.event_log = ast_true(s);
303 var = ast_variable_browse(cfg, "logfiles");
305 chan = make_logchannel(var->name, var->value, var->lineno);
307 chan->next = logchannels;
309 global_logmask |= chan->logmask;
314 ast_config_destroy(cfg);
315 ast_mutex_unlock(&loglock);
318 static FILE *qlog = NULL;
319 AST_MUTEX_DEFINE_STATIC(qloglock);
321 void ast_queue_log(const char *queuename, const char *callid, const char *agent, const char *event, const char *fmt, ...)
324 ast_mutex_lock(&qloglock);
327 fprintf(qlog, "%ld|%s|%s|%s|%s|", (long)time(NULL), callid, queuename, agent, event);
328 vfprintf(qlog, fmt, ap);
333 ast_mutex_unlock(&qloglock);
336 static void queue_log_init(void)
340 ast_mutex_lock(&qloglock);
346 snprintf(filename, sizeof(filename), "%s/%s", (char *)ast_config_AST_LOG_DIR, "queue_log");
347 if (logfiles.queue_log) {
348 qlog = fopen(filename, "a");
350 ast_mutex_unlock(&qloglock);
352 ast_queue_log("NONE", "NONE", "NONE", "CONFIGRELOAD", "%s", "");
354 ast_queue_log("NONE", "NONE", "NONE", "QUEUESTART", "%s", "");
357 int reload_logger(int rotate)
359 char old[AST_CONFIG_MAX_PATH] = "";
360 char new[AST_CONFIG_MAX_PATH];
361 struct logchannel *f;
365 ast_mutex_lock(&loglock);
374 mkdir((char *)ast_config_AST_LOG_DIR, 0755);
375 snprintf(old, sizeof(old), "%s/%s", (char *)ast_config_AST_LOG_DIR, EVENTLOG);
377 if (logfiles.event_log) {
380 snprintf(new, sizeof(new), "%s/%s.%d", (char *)ast_config_AST_LOG_DIR, EVENTLOG,x);
381 myf = fopen((char *)new, "r");
390 fprintf(stderr, "Unable to rename file '%s' to '%s'\n", old, new);
393 eventlog = fopen(old, "a");
398 if (f->fileptr && (f->fileptr != stdout) && (f->fileptr != stderr)) {
402 strncpy(old, f->filename, sizeof(old) - 1);
405 snprintf(new, sizeof(new), "%s.%d", f->filename, x);
406 myf = fopen((char *)new, "r");
416 fprintf(stderr, "Unable to rename file '%s' to '%s'\n", old, new);
422 ast_mutex_unlock(&loglock);
427 if (logfiles.event_log) {
429 ast_log(LOG_EVENT, "Restarted Asterisk Event Logger\n");
431 ast_verbose("Asterisk Event Logger restarted\n");
434 ast_log(LOG_ERROR, "Unable to create event log: %s\n", strerror(errno));
436 pending_logger_reload = 0;
440 static int handle_logger_reload(int fd, int argc, char *argv[])
444 ast_cli(fd, "Failed to reload the logger\n");
445 return RESULT_FAILURE;
448 return RESULT_SUCCESS;
451 static int handle_logger_rotate(int fd, int argc, char *argv[])
455 ast_cli(fd, "Failed to reload the logger and rotate log files\n");
456 return RESULT_FAILURE;
459 return RESULT_SUCCESS;
463 void (*verboser)(const char *string, int opos, int replacelast, int complete);
468 static char logger_reload_help[] =
469 "Usage: logger reload\n"
470 " Reloads the logger subsystem state. Use after restarting syslogd(8)\n";
472 static char logger_rotate_help[] =
473 "Usage: logger rotate\n"
474 " Rotates and Reopens the log files.\n";
476 static struct ast_cli_entry reload_logger_cli =
477 { { "logger", "reload", NULL },
478 handle_logger_reload, "Reopens the log files",
479 logger_reload_help };
481 static struct ast_cli_entry rotate_logger_cli =
482 { { "logger", "rotate", NULL },
483 handle_logger_rotate, "Rotates and reopens the log files",
484 logger_rotate_help };
486 static int handle_SIGXFSZ(int sig)
488 /* Indicate need to reload */
489 pending_logger_reload = 1;
493 int init_logger(void)
497 /* auto rotate if sig SIGXFSZ comes a-knockin */
498 (void) signal(SIGXFSZ,(void *) handle_SIGXFSZ);
500 /* register the relaod logger cli command */
501 ast_cli_register(&reload_logger_cli);
502 ast_cli_register(&rotate_logger_cli);
504 /* initialize queue logger */
507 /* create log channels */
510 /* create the eventlog */
511 if (logfiles.event_log) {
512 mkdir((char *)ast_config_AST_LOG_DIR, 0755);
513 snprintf(tmp, sizeof(tmp), "%s/%s", (char *)ast_config_AST_LOG_DIR, EVENTLOG);
514 eventlog = fopen((char *)tmp, "a");
516 ast_log(LOG_EVENT, "Started Asterisk Event Logger\n");
518 ast_verbose("Asterisk Event Logger Started %s\n",(char *)tmp);
521 ast_log(LOG_ERROR, "Unable to create event log: %s\n", strerror(errno));
527 void close_logger(void)
529 struct msglist *m, *tmp;
531 ast_mutex_lock(&msglist_lock);
543 ast_mutex_unlock(&msglist_lock);
547 static void ast_log_vsyslog(int level, const char *file, int line, const char *function, const char *fmt, va_list args)
551 if (level >= SYSLOG_NLEVELS) {
552 /* we are locked here, so cannot ast_log() */
553 fprintf(stderr, "ast_log_vsyslog called with bogus level: %d\n", level);
556 if (level == __LOG_VERBOSE) {
557 snprintf(buf, sizeof(buf), "VERBOSE[%ld]: ", (long)GETTID());
560 snprintf(buf, sizeof(buf), "%s[%ld]: %s:%d in %s: ",
561 levels[level], (long)GETTID(), file, line, function);
563 vsnprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), fmt, args);
564 syslog(syslog_level_map[level], "%s", buf);
568 * send log messages to syslog and/or the console
570 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
572 struct logchannel *chan;
580 if (!option_verbose && !option_debug && (level == __LOG_DEBUG)) {
583 /* Ignore anything that never gets logged anywhere */
584 if (!(global_logmask & (1 << level)))
587 /* Ignore anything other than the currently debugged file if there is one */
588 if ((level == __LOG_DEBUG) && !ast_strlen_zero(debug_filename) && strcasecmp(debug_filename, file))
591 /* begin critical section */
592 ast_mutex_lock(&loglock);
595 localtime_r(&t, &tm);
596 strftime(date, sizeof(date), dateformat, &tm);
598 if (logfiles.event_log && level == __LOG_EVENT) {
601 fprintf(eventlog, "%s asterisk[%d]: ", date, getpid());
602 vfprintf(eventlog, fmt, ap);
606 ast_mutex_unlock(&loglock);
613 if (chan->syslog && (chan->logmask & (1 << level))) {
615 ast_log_vsyslog(level, file, line, function, fmt, ap);
617 } else if ((chan->logmask & (1 << level)) && (chan->console)) {
619 char tmp1[80], tmp2[80], tmp3[80], tmp4[80];
621 if (level != __LOG_VERBOSE) {
622 sprintf(linestr, "%d", line);
623 snprintf(buf, sizeof(buf), option_timestamp ? "[%s] %s[%ld]: %s:%s %s: " : "%s %s[%ld]: %s:%s %s: ",
625 term_color(tmp1, levels[level], colors[level], 0, sizeof(tmp1)),
627 term_color(tmp2, file, COLOR_BRWHITE, 0, sizeof(tmp2)),
628 term_color(tmp3, linestr, COLOR_BRWHITE, 0, sizeof(tmp3)),
629 term_color(tmp4, function, COLOR_BRWHITE, 0, sizeof(tmp4)));
631 ast_console_puts(buf);
633 vsnprintf(buf, sizeof(buf), fmt, ap);
635 ast_console_puts(buf);
637 } else if ((chan->logmask & (1 << level)) && (chan->fileptr)) {
638 snprintf(buf, sizeof(buf), option_timestamp ? "[%s] %s[%ld]: " : "%s %s[%ld]: ", date,
639 levels[level], (long)GETTID());
640 fprintf(chan->fileptr, buf);
642 vsnprintf(buf, sizeof(buf), fmt, ap);
644 fputs(buf, chan->fileptr);
645 fflush(chan->fileptr);
651 * we don't have the logger chain configured yet,
652 * so just log to stdout
654 if (level != __LOG_VERBOSE) {
656 vsnprintf(buf, sizeof(buf), fmt, ap);
662 ast_mutex_unlock(&loglock);
663 /* end critical section */
664 if (pending_logger_reload) {
666 ast_log(LOG_EVENT,"Rotated Logs Per SIGXFSZ\n");
668 ast_verbose("Rotated Logs Per SIGXFSZ\n");
672 extern void ast_verbose(const char *fmt, ...)
674 static char stuff[4096];
675 static int pos = 0, opos;
676 static int replacelast = 0, complete;
686 ast_mutex_lock(&msglist_lock);
688 localtime_r(&t, &tm);
689 strftime(date, sizeof(date), dateformat, &tm);
691 if (option_timestamp) {
692 datefmt = alloca(strlen(date) + 3 + strlen(fmt) + 1);
694 sprintf(datefmt, "[%s] %s", date, fmt);
698 vsnprintf(stuff + pos, sizeof(stuff) - pos, fmt, ap);
703 if (stuff[strlen(stuff)-1] == '\n')
708 if (msgcnt < MAX_MSG_QUEUE) {
709 /* Allocate new structure */
710 m = malloc(sizeof(struct msglist));
713 /* Recycle the oldest entry */
719 m->msg = strdup(stuff);
729 ast_log(LOG_ERROR, "Out of memory\n");
737 v->verboser(stuff, opos, replacelast, complete);
741 fprintf(stdout, stuff + opos); */
742 ast_log(LOG_VERBOSE, "%s", stuff);
744 if (stuff[strlen(stuff)-1] != '\n')
747 replacelast = pos = 0;
751 ast_mutex_unlock(&msglist_lock);
754 int ast_verbose_dmesg(void (*v)(const char *string, int opos, int replacelast, int complete))
757 ast_mutex_lock(&msglist_lock);
760 /* Send all the existing entries that we have queued (i.e. they're likely to have missed) */
764 ast_mutex_unlock(&msglist_lock);
768 int ast_register_verbose(void (*v)(const char *string, int opos, int replacelast, int complete))
772 /* XXX Should be more flexible here, taking > 1 verboser XXX */
773 if ((tmp = malloc(sizeof (struct verb)))) {
775 ast_mutex_lock(&msglist_lock);
776 tmp->next = verboser;
780 /* Send all the existing entries that we have queued (i.e. they're likely to have missed) */
784 ast_mutex_unlock(&msglist_lock);
790 int ast_unregister_verbose(void (*v)(const char *string, int opos, int replacelast, int complete))
793 struct verb *tmp, *tmpl=NULL;
794 ast_mutex_lock(&msglist_lock);
797 if (tmp->verboser == v) {
799 tmpl->next = tmp->next;
801 verboser = tmp->next;
810 ast_mutex_unlock(&msglist_lock);