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>
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 */
44 #define SYSLOG_NLEVELS 6
46 #include <asterisk/logger.h>
48 #define MAX_MSG_QUEUE 200
50 static ast_mutex_t msglist_lock = AST_MUTEX_INITIALIZER;
51 static ast_mutex_t loglock = AST_MUTEX_INITIALIZER;
52 static int pending_logger_reload = 0;
54 static struct msglist {
57 } *list = NULL, *last = NULL;
61 int facility; /* syslog */
62 int syslog; /* syslog flag */
63 int console; /* console logging */
64 FILE *fileptr; /* logfile logging */
66 struct logchannel *next;
69 static struct logchannel *logchannels = NULL;
71 static int msgcnt = 0;
73 static FILE *eventlog = NULL;
75 static char *levels[] = {
84 static int colors[] = {
93 static int make_components(char *s, int lineno)
99 w = strsep(&stringp, ",");
101 while(*w && (*w < 33))
103 if (!strcasecmp(w, "error"))
104 res |= (1 << __LOG_ERROR);
105 else if (!strcasecmp(w, "warning"))
106 res |= (1 << __LOG_WARNING);
107 else if (!strcasecmp(w, "notice"))
108 res |= (1 << __LOG_NOTICE);
109 else if (!strcasecmp(w, "event"))
110 res |= (1 << __LOG_EVENT);
111 else if (!strcasecmp(w, "debug"))
112 res |= (1 << __LOG_DEBUG);
113 else if (!strcasecmp(w, "verbose"))
114 res |= (1 << __LOG_VERBOSE);
116 fprintf(stderr, "Logfile Warning: Unknown keyword '%s' at line %d of logger.conf\n", w, lineno);
118 w = strsep(&stringp, ",");
123 static struct logchannel *make_logchannel(char *channel, char *components, int lineno)
125 struct logchannel *chan;
129 if (!strlen(channel))
131 chan = malloc(sizeof(struct logchannel));
134 memset(chan, 0, sizeof(struct logchannel));
135 if (!strcasecmp(channel, "console")) {
137 } else if (!strncasecmp(channel, "syslog", 6)) {
140 * syslog.facility => level,level,level
142 facility = strchr(channel, '.');
143 if(!facility++ || !facility) {
147 * Walk through the list of facilitynames (defined in sys/syslog.h)
148 * to see if we can find the one we have been given
151 cptr = facilitynames;
152 while (cptr->c_name) {
153 if (!strncasecmp(facility, cptr->c_name, sizeof(cptr->c_name))) {
154 chan->facility = cptr->c_val;
159 if (0 > chan->facility) {
160 fprintf(stderr, "Logger Warning: bad syslog facility in logger.conf\n");
166 openlog("asterisk", LOG_PID, chan->facility);
168 if (channel[0] == '/')
169 strncpy(chan->filename, channel, sizeof(chan->filename) - 1);
171 snprintf(chan->filename, sizeof(chan->filename), "%s/%s", (char *)ast_config_AST_LOG_DIR, channel);
172 chan->fileptr = fopen(chan->filename, "a");
173 if (!chan->fileptr) {
174 /* Can't log here, since we're called with a lock */
175 fprintf(stderr, "Logger Warning: Unable to open log file '%s': %s\n", chan->filename, strerror(errno));
178 chan->logmask = make_components(components, lineno);
183 static void init_logger_chain(void)
185 struct logchannel *chan, *cur;
186 struct ast_config *cfg;
187 struct ast_variable *var;
189 /* delete our list of log channels */
190 ast_mutex_lock(&loglock);
198 ast_mutex_unlock(&loglock);
203 cfg = ast_load("logger.conf");
205 /* If no config file, we're fine */
209 ast_mutex_lock(&loglock);
210 var = ast_variable_browse(cfg, "logfiles");
212 chan = make_logchannel(var->name, var->value, var->lineno);
214 chan->next = logchannels;
221 ast_mutex_unlock(&loglock);
224 static FILE *qlog = NULL;
225 static ast_mutex_t qloglock = AST_MUTEX_INITIALIZER;
227 void ast_queue_log(const char *queuename, const char *callid, const char *agent, const char *event, const char *fmt, ...)
230 ast_mutex_lock(&qloglock);
233 fprintf(qlog, "%ld|%s|%s|%s|%s|", (long)time(NULL), callid, queuename, agent, event);
234 vfprintf(qlog, fmt, ap);
239 ast_mutex_unlock(&qloglock);
242 static void queue_log_init(void)
246 ast_mutex_lock(&qloglock);
252 snprintf(filename, sizeof(filename), "%s/%s", (char *)ast_config_AST_LOG_DIR, "queue_log");
253 qlog = fopen(filename, "a");
254 ast_mutex_unlock(&qloglock);
256 ast_queue_log("NONE", "NONE", "NONE", "CONFIGRELOAD", "%s", "");
258 ast_queue_log("NONE", "NONE", "NONE", "QUEUESTART", "%s", "");
261 int reload_logger(int rotate)
263 char old[AST_CONFIG_MAX_PATH];
264 char new[AST_CONFIG_MAX_PATH];
265 struct logchannel *f;
269 ast_mutex_lock(&loglock);
278 mkdir((char *)ast_config_AST_LOG_DIR, 0755);
279 snprintf(old, sizeof(old), "%s/%s", (char *)ast_config_AST_LOG_DIR, EVENTLOG);
283 snprintf(new, sizeof(new), "%s/%s.%d", (char *)ast_config_AST_LOG_DIR, EVENTLOG,x);
284 myf = fopen((char *)new, "r");
293 fprintf(stderr, "Unable to rename file '%s' to '%s'\n", old, new);
296 eventlog = fopen(old, "a");
300 if (f->fileptr && (f->fileptr != stdout) && (f->fileptr != stderr)) {
304 strncpy(old, f->filename, sizeof(old));
307 snprintf(new, sizeof(new), "%s.%d", f->filename, x);
308 myf = fopen((char *)new, "r");
318 fprintf(stderr, "Unable to rename file '%s' to '%s'\n", old, new);
324 ast_mutex_unlock(&loglock);
330 ast_log(LOG_EVENT, "Restarted Asterisk Event Logger\n");
332 ast_verbose("Asterisk Event Logger restarted\n");
335 ast_log(LOG_ERROR, "Unable to create event log: %s\n", strerror(errno));
337 pending_logger_reload = 0;
341 static int handle_logger_reload(int fd, int argc, char *argv[])
345 ast_cli(fd, "Failed to reloadthe logger\n");
346 return RESULT_FAILURE;
349 return RESULT_SUCCESS;
352 static int handle_logger_rotate(int fd, int argc, char *argv[])
356 ast_cli(fd, "Failed to reloadthe logger\n");
357 return RESULT_FAILURE;
360 return RESULT_SUCCESS;
364 void (*verboser)(const char *string, int opos, int replacelast, int complete);
369 static char logger_reload_help[] =
370 "Usage: logger reload\n"
371 " Reloads the logger subsystem state. Use after restarting syslogd(8)\n";
373 static char logger_rotate_help[] =
374 "Usage: logger rotate\n"
375 " Rotates and Reopens the log files.\n";
377 static struct ast_cli_entry reload_logger_cli =
378 { { "logger", "reload", NULL },
379 handle_logger_reload, "Reopens the log files",
380 logger_reload_help };
382 static struct ast_cli_entry rotate_logger_cli =
383 { { "logger", "rotate", NULL },
384 handle_logger_rotate, "Rotates and reopens the log files",
385 logger_rotate_help };
387 static int handle_SIGXFSZ(int sig) {
388 /* Indicate need to reload */
389 pending_logger_reload = 1;
393 int init_logger(void)
397 /* auto rotate if sig SIGXFSZ comes a-knockin */
398 (void) signal(SIGXFSZ,(void *) handle_SIGXFSZ);
400 /* register the relaod logger cli command */
401 ast_cli_register(&reload_logger_cli);
402 ast_cli_register(&rotate_logger_cli);
404 /* initialize queue logger */
407 /* create the eventlog */
408 mkdir((char *)ast_config_AST_LOG_DIR, 0755);
409 snprintf(tmp, sizeof(tmp), "%s/%s", (char *)ast_config_AST_LOG_DIR, EVENTLOG);
410 eventlog = fopen((char *)tmp, "a");
413 ast_log(LOG_EVENT, "Started Asterisk Event Logger\n");
415 ast_verbose("Asterisk Event Logger Started %s\n",(char *)tmp);
418 ast_log(LOG_ERROR, "Unable to create event log: %s\n", strerror(errno));
422 /* create log channels */
427 static void ast_log_vsyslog(int level, const char *file, int line, const char *function, const char *fmt, va_list args) {
430 if(level >= SYSLOG_NLEVELS) {
431 /* we are locked here, so cannot ast_log() */
432 fprintf(stderr, "ast_log_vsyslog called with bogus level: %d\n", level);
435 if(level == __LOG_VERBOSE) {
436 snprintf(buf, sizeof(buf), "VERBOSE[%ld]: ", (long)pthread_self());
439 snprintf(buf, sizeof(buf), "%s[%ld]: %s:%d in %s: ",
440 levels[level], (long)pthread_self(), file, line, function);
442 vsnprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), fmt, args);
443 syslog(syslog_level_map[level], "%s", buf);
447 * send log messages to syslog and/or the console
449 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
451 struct logchannel *chan;
459 if (!option_verbose && !option_debug && (level == __LOG_DEBUG)) {
463 /* begin critical section */
464 ast_mutex_lock(&loglock);
467 localtime_r(&t, &tm);
468 strftime(date, sizeof(date), "%b %e %T", &tm);
471 if (level == __LOG_EVENT) {
474 fprintf(eventlog, "%s asterisk[%d]: ", date, getpid());
475 vfprintf(eventlog, fmt, ap);
479 ast_mutex_unlock(&loglock);
486 if (chan->syslog && (chan->logmask & (1 << level))) {
488 ast_log_vsyslog(level, file, line, function, fmt, ap);
490 } else if ((chan->logmask & (1 << level)) && (chan->console)) {
492 char tmp1[80], tmp2[80], tmp3[80], tmp4[80];
494 if(level != __LOG_VERBOSE) {
495 sprintf(linestr, "%d", line);
496 snprintf(buf, sizeof(buf), "%s %s[%ld]: %s:%s %s: ",
498 term_color(tmp1, levels[level], colors[level], 0, sizeof(tmp1)),
499 (long)pthread_self(),
500 term_color(tmp2, file, COLOR_BRWHITE, 0, sizeof(tmp2)),
501 term_color(tmp3, linestr, COLOR_BRWHITE, 0, sizeof(tmp3)),
502 term_color(tmp4, function, COLOR_BRWHITE, 0, sizeof(tmp4)));
504 ast_console_puts(buf);
506 vsnprintf(buf, sizeof(buf), fmt, ap);
508 ast_console_puts(buf);
510 } else if ((chan->logmask & (1 << level)) && (chan->fileptr)) {
511 snprintf(buf, sizeof(buf), "%s %s[%ld]: ", date,
512 levels[level], (long)pthread_self());
513 fprintf(chan->fileptr, buf);
515 vsnprintf(buf, sizeof(buf), fmt, ap);
517 fputs(buf, chan->fileptr);
518 fflush(chan->fileptr);
524 * we don't have the logger chain configured yet,
525 * so just log to stdout
527 if (level != __LOG_VERBOSE) {
529 vsnprintf(buf, sizeof(buf), fmt, ap);
535 ast_mutex_unlock(&loglock);
536 /* end critical section */
537 if (pending_logger_reload) {
539 ast_log(LOG_EVENT,"Rotated Logs Per SIGXFSZ\n");
541 ast_verbose("Rotated Logs Per SIGXFSZ\n");
545 extern void ast_verbose(const char *fmt, ...)
547 static char stuff[4096];
548 static int pos = 0, opos;
549 static int replacelast = 0, complete;
554 ast_mutex_lock(&msglist_lock);
555 vsnprintf(stuff + pos, sizeof(stuff) - pos, fmt, ap);
558 if (fmt[strlen(fmt)-1] == '\n')
563 if (msgcnt < MAX_MSG_QUEUE) {
564 /* Allocate new structure */
565 m = malloc(sizeof(struct msglist));
568 /* Recycle the oldest entry */
574 m->msg = strdup(stuff);
584 ast_log(LOG_ERROR, "Out of memory\n");
592 v->verboser(stuff, opos, replacelast, complete);
596 fprintf(stdout, stuff + opos); */
598 ast_log(LOG_VERBOSE, stuff);
600 if (fmt[strlen(fmt)-1] != '\n')
603 replacelast = pos = 0;
606 ast_mutex_unlock(&msglist_lock);
609 int ast_verbose_dmesg(void (*v)(const char *string, int opos, int replacelast, int complete))
613 ast_mutex_lock(&msglist_lock);
615 /* Send all the existing entries that we have queued (i.e. they're likely to have missed) */
619 ast_mutex_unlock(&msglist_lock);
623 int ast_register_verbose(void (*v)(const char *string, int opos, int replacelast, int complete))
627 /* XXX Should be more flexible here, taking > 1 verboser XXX */
628 if ((tmp = malloc(sizeof (struct verb)))) {
630 ast_mutex_lock(&msglist_lock);
631 tmp->next = verboser;
635 /* Send all the existing entries that we have queued (i.e. they're likely to have missed) */
639 ast_mutex_unlock(&msglist_lock);
645 int ast_unregister_verbose(void (*v)(const char *string, int opos, int replacelast, int complete))
648 struct verb *tmp, *tmpl=NULL;
649 ast_mutex_lock(&msglist_lock);
652 if (tmp->verboser == v) {
654 tmpl->next = tmp->next;
656 verboser = tmp->next;
665 ast_mutex_unlock(&msglist_lock);