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>
26 #include <asterisk/manager.h>
34 #define SYSLOG_NAMES /* so we can map syslog facilities names to their numeric values,
35 from <syslog.h> which is included by logger.h */
37 static int syslog_level_map[] = {
39 LOG_INFO, /* arbitrary equivalent of LOG_EVENT */
46 #define SYSLOG_NLEVELS 6
48 #include <asterisk/logger.h>
50 #define MAX_MSG_QUEUE 200
52 #if defined(__linux__) && defined(__NR_gettid)
53 #include <asm/unistd.h>
54 #define GETTID() syscall(__NR_gettid)
56 #define GETTID() getpid()
59 static char dateformat[256] = "%b %e %T"; /* Original Asterisk Format */
61 AST_MUTEX_DEFINE_STATIC(msglist_lock);
62 AST_MUTEX_DEFINE_STATIC(loglock);
63 static int pending_logger_reload = 0;
64 static int global_logmask = -1;
67 unsigned int queue_log:1;
68 unsigned int event_log:1;
69 } logfiles = { 1, 1 };
71 static struct msglist {
74 } *list = NULL, *last = NULL;
76 static char hostname[256];
85 int logmask; /* What to log to this channel */
86 int disabled; /* If this channel is disabled or not */
87 int facility; /* syslog facility */
88 enum logtypes type; /* Type of log channel */
89 FILE *fileptr; /* logfile logging file pointer */
90 char filename[256]; /* Filename */
91 struct logchannel *next; /* Next channel in chain */
94 static struct logchannel *logchannels = NULL;
96 static int msgcnt = 0;
98 static FILE *eventlog = NULL;
100 static char *levels[] = {
109 static int colors[] = {
118 static int make_components(char *s, int lineno)
124 w = strsep(&stringp, ",");
126 while(*w && (*w < 33))
128 if (!strcasecmp(w, "error"))
129 res |= (1 << __LOG_ERROR);
130 else if (!strcasecmp(w, "warning"))
131 res |= (1 << __LOG_WARNING);
132 else if (!strcasecmp(w, "notice"))
133 res |= (1 << __LOG_NOTICE);
134 else if (!strcasecmp(w, "event"))
135 res |= (1 << __LOG_EVENT);
136 else if (!strcasecmp(w, "debug"))
137 res |= (1 << __LOG_DEBUG);
138 else if (!strcasecmp(w, "verbose"))
139 res |= (1 << __LOG_VERBOSE);
141 fprintf(stderr, "Logfile Warning: Unknown keyword '%s' at line %d of logger.conf\n", w, lineno);
143 w = strsep(&stringp, ",");
148 static struct logchannel *make_logchannel(char *channel, char *components, int lineno)
150 struct logchannel *chan;
156 if (ast_strlen_zero(channel))
158 chan = malloc(sizeof(struct logchannel));
161 memset(chan, 0, sizeof(struct logchannel));
162 if (!strcasecmp(channel, "console")) {
163 chan->type = LOGTYPE_CONSOLE;
164 } else if (!strncasecmp(channel, "syslog", 6)) {
167 * syslog.facility => level,level,level
169 facility = strchr(channel, '.');
170 if(!facility++ || !facility) {
176 * Walk through the list of facilitynames (defined in sys/syslog.h)
177 * to see if we can find the one we have been given
180 cptr = facilitynames;
181 while (cptr->c_name) {
182 if (!strcasecmp(facility, cptr->c_name)) {
183 chan->facility = cptr->c_val;
190 if (!strcasecmp(facility, "kern"))
191 chan->facility = LOG_KERN;
192 else if (!strcasecmp(facility, "USER"))
193 chan->facility = LOG_USER;
194 else if (!strcasecmp(facility, "MAIL"))
195 chan->facility = LOG_MAIL;
196 else if (!strcasecmp(facility, "DAEMON"))
197 chan->facility = LOG_DAEMON;
198 else if (!strcasecmp(facility, "AUTH"))
199 chan->facility = LOG_AUTH;
200 else if (!strcasecmp(facility, "SYSLOG"))
201 chan->facility = LOG_SYSLOG;
202 else if (!strcasecmp(facility, "LPR"))
203 chan->facility = LOG_LPR;
204 else if (!strcasecmp(facility, "NEWS"))
205 chan->facility = LOG_NEWS;
206 else if (!strcasecmp(facility, "UUCP"))
207 chan->facility = LOG_UUCP;
208 else if (!strcasecmp(facility, "CRON"))
209 chan->facility = LOG_CRON;
210 else if (!strcasecmp(facility, "LOCAL0"))
211 chan->facility = LOG_LOCAL0;
212 else if (!strcasecmp(facility, "LOCAL1"))
213 chan->facility = LOG_LOCAL1;
214 else if (!strcasecmp(facility, "LOCAL2"))
215 chan->facility = LOG_LOCAL2;
216 else if (!strcasecmp(facility, "LOCAL3"))
217 chan->facility = LOG_LOCAL3;
218 else if (!strcasecmp(facility, "LOCAL4"))
219 chan->facility = LOG_LOCAL4;
220 else if (!strcasecmp(facility, "LOCAL5"))
221 chan->facility = LOG_LOCAL5;
222 else if (!strcasecmp(facility, "LOCAL6"))
223 chan->facility = LOG_LOCAL6;
224 else if (!strcasecmp(facility, "LOCAL7"))
225 chan->facility = LOG_LOCAL7;
228 if (0 > chan->facility) {
229 fprintf(stderr, "Logger Warning: bad syslog facility in logger.conf\n");
234 chan->type = LOGTYPE_SYSLOG;
235 openlog("asterisk", LOG_PID, chan->facility);
237 if (channel[0] == '/') {
238 if(!ast_strlen_zero(hostname)) {
239 snprintf(chan->filename, sizeof(chan->filename) - 1,"%s.%s", channel, hostname);
241 strncpy(chan->filename, channel, sizeof(chan->filename) - 1);
245 if(!ast_strlen_zero(hostname)) {
246 snprintf(chan->filename, sizeof(chan->filename), "%s/%s.%s",(char *)ast_config_AST_LOG_DIR, channel, hostname);
248 snprintf(chan->filename, sizeof(chan->filename), "%s/%s", (char *)ast_config_AST_LOG_DIR, channel);
250 chan->fileptr = fopen(chan->filename, "a");
251 if (!chan->fileptr) {
252 /* Can't log here, since we're called with a lock */
253 fprintf(stderr, "Logger Warning: Unable to open log file '%s': %s\n", chan->filename, strerror(errno));
255 chan->type = LOGTYPE_FILE;
257 chan->logmask = make_components(components, lineno);
262 static void init_logger_chain(void)
264 struct logchannel *chan, *cur;
265 struct ast_config *cfg;
266 struct ast_variable *var;
269 /* delete our list of log channels */
270 ast_mutex_lock(&loglock);
278 ast_mutex_unlock(&loglock);
284 cfg = ast_config_load("logger.conf");
286 /* If no config file, we're fine */
290 ast_mutex_lock(&loglock);
291 if ((s = ast_variable_retrieve(cfg, "general", "appendhostname"))) {
293 if(gethostname(hostname, sizeof(hostname))) {
294 strncpy(hostname, "unknown", sizeof(hostname)-1);
295 ast_log(LOG_WARNING, "What box has no hostname???\n");
301 if ((s = ast_variable_retrieve(cfg, "general", "dateformat"))) {
302 strncpy(dateformat, s, sizeof(dateformat) - 1);
304 strncpy(dateformat, "%b %e %T", sizeof(dateformat) - 1);
305 if ((s = ast_variable_retrieve(cfg, "general", "queue_log"))) {
306 logfiles.queue_log = ast_true(s);
308 if ((s = ast_variable_retrieve(cfg, "general", "event_log"))) {
309 logfiles.event_log = ast_true(s);
312 var = ast_variable_browse(cfg, "logfiles");
314 chan = make_logchannel(var->name, var->value, var->lineno);
316 chan->next = logchannels;
318 global_logmask |= chan->logmask;
323 ast_config_destroy(cfg);
324 ast_mutex_unlock(&loglock);
327 static FILE *qlog = NULL;
328 AST_MUTEX_DEFINE_STATIC(qloglock);
330 void ast_queue_log(const char *queuename, const char *callid, const char *agent, const char *event, const char *fmt, ...)
333 ast_mutex_lock(&qloglock);
336 fprintf(qlog, "%ld|%s|%s|%s|%s|", (long)time(NULL), callid, queuename, agent, event);
337 vfprintf(qlog, fmt, ap);
342 ast_mutex_unlock(&qloglock);
345 static void queue_log_init(void)
350 ast_mutex_lock(&qloglock);
356 snprintf(filename, sizeof(filename), "%s/%s", (char *)ast_config_AST_LOG_DIR, "queue_log");
357 if (logfiles.queue_log) {
358 qlog = fopen(filename, "a");
360 ast_mutex_unlock(&qloglock);
362 ast_queue_log("NONE", "NONE", "NONE", "CONFIGRELOAD", "%s", "");
364 ast_queue_log("NONE", "NONE", "NONE", "QUEUESTART", "%s", "");
367 int reload_logger(int rotate)
369 char old[AST_CONFIG_MAX_PATH] = "";
370 char new[AST_CONFIG_MAX_PATH];
371 struct logchannel *f;
375 ast_mutex_lock(&loglock);
382 mkdir((char *)ast_config_AST_LOG_DIR, 0755);
383 snprintf(old, sizeof(old), "%s/%s", (char *)ast_config_AST_LOG_DIR, EVENTLOG);
385 if (logfiles.event_log) {
388 snprintf(new, sizeof(new), "%s/%s.%d", (char *)ast_config_AST_LOG_DIR, EVENTLOG,x);
389 myf = fopen((char *)new, "r");
390 if (myf) /* File exists */
398 fprintf(stderr, "Unable to rename file '%s' to '%s'\n", old, new);
401 eventlog = fopen(old, "a");
407 f->disabled = 0; /* Re-enable logging at reload */
408 manager_event(EVENT_FLAG_SYSTEM, "LogChannel", "Channel: %s\r\nEnabled: Yes\r\n", f->filename);
410 if (f->fileptr && (f->fileptr != stdout) && (f->fileptr != stderr)) {
411 fclose(f->fileptr); /* Close file */
414 strncpy(old, f->filename, sizeof(old) - 1);
417 snprintf(new, sizeof(new), "%s.%d", f->filename, x);
418 myf = fopen((char *)new, "r");
428 fprintf(stderr, "Unable to rename file '%s' to '%s'\n", old, new);
434 ast_mutex_unlock(&loglock);
439 if (logfiles.event_log) {
441 ast_log(LOG_EVENT, "Restarted Asterisk Event Logger\n");
443 ast_verbose("Asterisk Event Logger restarted\n");
446 ast_log(LOG_ERROR, "Unable to create event log: %s\n", strerror(errno));
448 pending_logger_reload = 0;
452 static int handle_logger_reload(int fd, int argc, char *argv[])
454 if(reload_logger(0)) {
455 ast_cli(fd, "Failed to reload the logger\n");
456 return RESULT_FAILURE;
458 return RESULT_SUCCESS;
461 static int handle_logger_rotate(int fd, int argc, char *argv[])
463 if(reload_logger(1)) {
464 ast_cli(fd, "Failed to reload the logger and rotate log files\n");
465 return RESULT_FAILURE;
467 return RESULT_SUCCESS;
470 /*--- handle_logger_show_channels: CLI command to show logging system
472 static int handle_logger_show_channels(int fd, int argc, char *argv[])
474 #define FORMATL "%-35.35s %-8.8s %-9.9s "
475 struct logchannel *chan;
477 ast_mutex_lock(&loglock);
480 ast_cli(fd,FORMATL, "Channel", "Type", "Status");
481 ast_cli(fd, "Configuration\n");
482 ast_cli(fd,FORMATL, "-------", "----", "------");
483 ast_cli(fd, "-------------\n");
485 ast_cli(fd, FORMATL, chan->filename, chan->type==LOGTYPE_CONSOLE ? "Console" : (chan->type==LOGTYPE_SYSLOG ? "Syslog" : "File"),
486 chan->disabled ? "Disabled" : "Enabled");
488 if (chan->logmask & (1 << __LOG_DEBUG))
489 ast_cli(fd, "Debug ");
490 if (chan->logmask & (1 << __LOG_VERBOSE))
491 ast_cli(fd, "Verbose ");
492 if (chan->logmask & (1 << __LOG_WARNING))
493 ast_cli(fd, "Warning ");
494 if (chan->logmask & (1 << __LOG_NOTICE))
495 ast_cli(fd, "Notice ");
496 if (chan->logmask & (1 << __LOG_ERROR))
497 ast_cli(fd, "Error ");
498 if (chan->logmask & (1 << __LOG_EVENT))
499 ast_cli(fd, "Event ");
505 ast_mutex_unlock(&loglock);
507 return RESULT_SUCCESS;
511 void (*verboser)(const char *string, int opos, int replacelast, int complete);
516 static char logger_reload_help[] =
517 "Usage: logger reload\n"
518 " Reloads the logger subsystem state. Use after restarting syslogd(8) if you are using syslog logging.\n";
520 static char logger_rotate_help[] =
521 "Usage: logger rotate\n"
522 " Rotates and Reopens the log files.\n";
524 static char logger_show_channels_help[] =
525 "Usage: logger show channels\n"
526 " Show configured logger channels.\n";
528 static struct ast_cli_entry logger_show_channels_cli =
529 { { "logger", "show", "channels", NULL },
530 handle_logger_show_channels, "List configured log channels",
531 logger_show_channels_help };
533 static struct ast_cli_entry reload_logger_cli =
534 { { "logger", "reload", NULL },
535 handle_logger_reload, "Reopens the log files",
536 logger_reload_help };
538 static struct ast_cli_entry rotate_logger_cli =
539 { { "logger", "rotate", NULL },
540 handle_logger_rotate, "Rotates and reopens the log files",
541 logger_rotate_help };
543 static int handle_SIGXFSZ(int sig)
545 /* Indicate need to reload */
546 pending_logger_reload = 1;
550 int init_logger(void)
554 /* auto rotate if sig SIGXFSZ comes a-knockin */
555 (void) signal(SIGXFSZ,(void *) handle_SIGXFSZ);
557 /* register the relaod logger cli command */
558 ast_cli_register(&reload_logger_cli);
559 ast_cli_register(&rotate_logger_cli);
560 ast_cli_register(&logger_show_channels_cli);
562 /* initialize queue logger */
565 /* create log channels */
568 /* create the eventlog */
569 if (logfiles.event_log) {
570 mkdir((char *)ast_config_AST_LOG_DIR, 0755);
571 snprintf(tmp, sizeof(tmp), "%s/%s", (char *)ast_config_AST_LOG_DIR, EVENTLOG);
572 eventlog = fopen((char *)tmp, "a");
574 ast_log(LOG_EVENT, "Started Asterisk Event Logger\n");
576 ast_verbose("Asterisk Event Logger Started %s\n",(char *)tmp);
579 ast_log(LOG_ERROR, "Unable to create event log: %s\n", strerror(errno));
585 void close_logger(void)
587 struct msglist *m, *tmp;
589 ast_mutex_lock(&msglist_lock);
601 ast_mutex_unlock(&msglist_lock);
605 static void ast_log_vsyslog(int level, const char *file, int line, const char *function, const char *fmt, va_list args)
609 if (level >= SYSLOG_NLEVELS) {
610 /* we are locked here, so cannot ast_log() */
611 fprintf(stderr, "ast_log_vsyslog called with bogus level: %d\n", level);
614 if (level == __LOG_VERBOSE) {
615 snprintf(buf, sizeof(buf), "VERBOSE[%ld]: ", (long)GETTID());
618 snprintf(buf, sizeof(buf), "%s[%ld]: %s:%d in %s: ",
619 levels[level], (long)GETTID(), file, line, function);
621 vsnprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), fmt, args);
622 syslog(syslog_level_map[level], "%s", buf);
626 * send log messages to syslog and/or the console
628 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
630 struct logchannel *chan;
638 if (!option_verbose && !option_debug && (level == __LOG_DEBUG)) {
641 /* Ignore anything that never gets logged anywhere */
642 if (!(global_logmask & (1 << level)))
645 /* Ignore anything other than the currently debugged file if there is one */
646 if ((level == __LOG_DEBUG) && !ast_strlen_zero(debug_filename) && strcasecmp(debug_filename, file))
649 /* begin critical section */
650 ast_mutex_lock(&loglock);
653 localtime_r(&t, &tm);
654 strftime(date, sizeof(date), dateformat, &tm);
656 if (logfiles.event_log && level == __LOG_EVENT) {
659 fprintf(eventlog, "%s asterisk[%d]: ", date, getpid());
660 vfprintf(eventlog, fmt, ap);
664 ast_mutex_unlock(&loglock);
670 while(chan && !chan->disabled) {
671 /* Check syslog channels */
672 if (chan->type == LOG_SYSLOG && (chan->logmask & (1 << level))) {
674 ast_log_vsyslog(level, file, line, function, fmt, ap);
676 /* Console channels */
677 } else if ((chan->logmask & (1 << level)) && (chan->type == LOGTYPE_CONSOLE)) {
679 char tmp1[80], tmp2[80], tmp3[80], tmp4[80];
681 if (level != __LOG_VERBOSE) {
682 sprintf(linestr, "%d", line);
683 snprintf(buf, sizeof(buf), option_timestamp ? "[%s] %s[%ld]: %s:%s %s: " : "%s %s[%ld]: %s:%s %s: ",
685 term_color(tmp1, levels[level], colors[level], 0, sizeof(tmp1)),
687 term_color(tmp2, file, COLOR_BRWHITE, 0, sizeof(tmp2)),
688 term_color(tmp3, linestr, COLOR_BRWHITE, 0, sizeof(tmp3)),
689 term_color(tmp4, function, COLOR_BRWHITE, 0, sizeof(tmp4)));
691 ast_console_puts(buf);
693 vsnprintf(buf, sizeof(buf), fmt, ap);
695 ast_console_puts(buf);
698 } else if ((chan->logmask & (1 << level)) && (chan->fileptr)) {
700 snprintf(buf, sizeof(buf), option_timestamp ? "[%s] %s[%ld]: " : "%s %s[%ld] %s: ", date,
701 levels[level], (long)GETTID(), file);
702 res = fprintf(chan->fileptr, buf);
703 if (res <= 0 && buf[0] != '\0') { /* Error, no characters printed */
704 fprintf(stderr,"**** Asterisk Logging Error: ***********\n");
705 if (errno == ENOMEM || errno == ENOSPC) {
706 fprintf(stderr, "Asterisk logging error: Out of disk space, can't log to log file %s\n", chan->filename);
708 fprintf(stderr, "Logger Warning: Unable to write to log file '%s': %s (disabled)\n", chan->filename, strerror(errno));
709 manager_event(EVENT_FLAG_SYSTEM, "LogChannel", "Channel: %s\r\nEnabled: No\r\nReason: %d - %s\r\n", chan->filename, errno, strerror(errno));
712 /* No error message, continue printing */
714 vsnprintf(buf, sizeof(buf), fmt, ap);
716 fputs(buf, chan->fileptr);
717 fflush(chan->fileptr);
724 * we don't have the logger chain configured yet,
725 * so just log to stdout
727 if (level != __LOG_VERBOSE) {
729 vsnprintf(buf, sizeof(buf), fmt, ap);
735 ast_mutex_unlock(&loglock);
736 /* end critical section */
737 if (pending_logger_reload) {
739 ast_log(LOG_EVENT,"Rotated Logs Per SIGXFSZ (Exceeded file size limit)\n");
741 ast_verbose("Rotated Logs Per SIGXFSZ (Exceeded file size limit)\n");
745 extern void ast_verbose(const char *fmt, ...)
747 static char stuff[4096];
748 static int pos = 0, opos;
749 static int replacelast = 0, complete;
759 ast_mutex_lock(&msglist_lock);
761 localtime_r(&t, &tm);
762 strftime(date, sizeof(date), dateformat, &tm);
764 if (option_timestamp) {
765 datefmt = alloca(strlen(date) + 3 + strlen(fmt) + 1);
767 sprintf(datefmt, "[%s] %s", date, fmt);
771 vsnprintf(stuff + pos, sizeof(stuff) - pos, fmt, ap);
776 if (stuff[strlen(stuff)-1] == '\n')
781 if (msgcnt < MAX_MSG_QUEUE) {
782 /* Allocate new structure */
783 m = malloc(sizeof(struct msglist));
786 /* Recycle the oldest entry */
792 m->msg = strdup(stuff);
802 ast_log(LOG_ERROR, "Out of memory\n");
810 v->verboser(stuff, opos, replacelast, complete);
814 fprintf(stdout, stuff + opos); */
815 ast_log(LOG_VERBOSE, "%s", stuff);
817 if (stuff[strlen(stuff)-1] != '\n')
820 replacelast = pos = 0;
824 ast_mutex_unlock(&msglist_lock);
827 int ast_verbose_dmesg(void (*v)(const char *string, int opos, int replacelast, int complete))
830 ast_mutex_lock(&msglist_lock);
833 /* Send all the existing entries that we have queued (i.e. they're likely to have missed) */
837 ast_mutex_unlock(&msglist_lock);
841 int ast_register_verbose(void (*v)(const char *string, int opos, int replacelast, int complete))
845 /* XXX Should be more flexible here, taking > 1 verboser XXX */
846 if ((tmp = malloc(sizeof (struct verb)))) {
848 ast_mutex_lock(&msglist_lock);
849 tmp->next = verboser;
853 /* Send all the existing entries that we have queued (i.e. they're likely to have missed) */
857 ast_mutex_unlock(&msglist_lock);
863 int ast_unregister_verbose(void (*v)(const char *string, int opos, int replacelast, int complete))
866 struct verb *tmp, *tmpl=NULL;
867 ast_mutex_lock(&msglist_lock);
870 if (tmp->verboser == v) {
872 tmpl->next = tmp->next;
874 verboser = tmp->next;
883 ast_mutex_unlock(&msglist_lock);