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 */
43 #define SYSLOG_NLEVELS 5
45 #include <asterisk/logger.h>
47 #define MAX_MSG_QUEUE 200
49 static ast_mutex_t msglist_lock = AST_MUTEX_INITIALIZER;
50 static ast_mutex_t loglock = AST_MUTEX_INITIALIZER;
52 static struct msglist {
55 } *list = NULL, *last = NULL;
59 int facility; /* syslog */
60 int syslog; /* syslog flag */
61 int console; /* console logging */
62 FILE *fileptr; /* logfile logging */
64 struct logchannel *next;
67 static struct logchannel *logchannels = NULL;
69 static int msgcnt = 0;
71 static FILE *eventlog = NULL;
73 static char *levels[] = {
81 static int colors[] = {
89 static int make_components(char *s, int lineno)
95 w = strsep(&stringp, ",");
97 while(*w && (*w < 33))
99 if (!strcasecmp(w, "error"))
100 res |= (1 << __LOG_ERROR);
101 else if (!strcasecmp(w, "warning"))
102 res |= (1 << __LOG_WARNING);
103 else if (!strcasecmp(w, "notice"))
104 res |= (1 << __LOG_NOTICE);
105 else if (!strcasecmp(w, "event"))
106 res |= (1 << __LOG_EVENT);
107 else if (!strcasecmp(w, "debug"))
108 res |= (1 << __LOG_DEBUG);
110 fprintf(stderr, "Logfile Warning: Unknown keyword '%s' at line %d of logger.conf\n", w, lineno);
112 w = strsep(&stringp, ",");
117 static struct logchannel *make_logchannel(char *channel, char *components, int lineno)
119 struct logchannel *chan;
123 if (!strlen(channel))
125 chan = malloc(sizeof(struct logchannel));
128 memset(chan, 0, sizeof(chan));
129 if (!strcasecmp(channel, "console")) {
131 } else if (!strncasecmp(channel, "syslog", 6)) {
134 * syslog.facility => level,level,level
136 facility = strchr(channel, '.');
137 if(!facility++ || !facility) {
141 * Walk through the list of facilitynames (defined in sys/syslog.h)
142 * to see if we can find the one we have been given
145 cptr = facilitynames;
146 while (cptr->c_name) {
147 if (!strncasecmp(facility, cptr->c_name, sizeof(cptr->c_name))) {
148 chan->facility = cptr->c_val;
153 if (0 > chan->facility) {
154 fprintf(stderr, "Logger Warning: bad syslog facility in logger.conf\n");
160 openlog("asterisk", LOG_PID, chan->facility);
162 if (channel[0] == '/')
163 strncpy(chan->filename, channel, sizeof(chan->filename) - 1);
165 snprintf(chan->filename, sizeof(chan->filename), "%s/%s", (char *)ast_config_AST_LOG_DIR, channel);
166 chan->fileptr = fopen(chan->filename, "a");
167 if (!chan->fileptr) {
168 /* Can't log here, since we're called with a lock */
169 fprintf(stderr, "Logger Warning: Unable to open log file '%s': %s\n", chan->filename, strerror(errno));
172 chan->logmask = make_components(components, lineno);
177 static void init_logger_chain(void)
179 struct logchannel *chan, *cur;
180 struct ast_config *cfg;
181 struct ast_variable *var;
183 /* delete our list of log channels */
184 ast_mutex_lock(&loglock);
192 ast_mutex_unlock(&loglock);
197 cfg = ast_load("logger.conf");
199 /* If no config file, we're fine */
203 ast_mutex_lock(&loglock);
204 var = ast_variable_browse(cfg, "logfiles");
206 chan = make_logchannel(var->name, var->value, var->lineno);
208 chan->next = logchannels;
215 ast_mutex_unlock(&loglock);
218 int reload_logger(int rotate)
220 char old[AST_CONFIG_MAX_PATH];
221 char new[AST_CONFIG_MAX_PATH];
222 struct logchannel *f;
227 ast_mutex_lock(&loglock);
236 mkdir((char *)ast_config_AST_LOG_DIR, 0755);
237 snprintf(old, sizeof(old), "%s/%s", (char *)ast_config_AST_LOG_DIR, EVENTLOG);
241 snprintf(new, sizeof(new), "%s/%s.%d", (char *)ast_config_AST_LOG_DIR, EVENTLOG,x);
242 myf = fopen((char *)new, "r");
251 fprintf(stderr, "Unable to rename file '%s' to '%s'\n", old, new);
254 eventlog = fopen(old, "a");
258 if (f->fileptr && (f->fileptr != stdout) && (f->fileptr != stderr)) {
262 strncpy(old, f->filename, sizeof(old));
265 snprintf(new, sizeof(new), "%s.%d", f->filename, x);
266 myf = fopen((char *)new, "r");
276 fprintf(stderr, "Unable to rename file '%s' to '%s'\n", old, new);
282 ast_mutex_unlock(&loglock);
286 ast_log(LOG_EVENT, "Restarted Asterisk Event Logger\n");
288 ast_verbose("Asterisk Event Logger restarted\n");
291 ast_log(LOG_ERROR, "Unable to create event log: %s\n", strerror(errno));
296 static int handle_logger_reload(int fd, int argc, char *argv[])
300 ast_cli(fd, "Failed to reloadthe logger\n");
301 return RESULT_FAILURE;
304 return RESULT_SUCCESS;
307 static int handle_logger_rotate(int fd, int argc, char *argv[])
311 ast_cli(fd, "Failed to reloadthe logger\n");
312 return RESULT_FAILURE;
315 return RESULT_SUCCESS;
319 void (*verboser)(const char *string, int opos, int replacelast, int complete);
324 static char logger_reload_help[] =
325 "Usage: logger reload\n"
326 " Reloads the logger subsystem state. Use after restarting syslogd(8)\n";
328 static char logger_rotate_help[] =
329 "Usage: logger reload\n"
330 " Rotates and Reopens the log files.\n";
332 static struct ast_cli_entry reload_logger_cli =
333 { { "logger", "reload", NULL },
334 handle_logger_reload, "Reopens the log files",
335 logger_reload_help };
337 static struct ast_cli_entry rotate_logger_cli =
338 { { "logger", "rotate", NULL },
339 handle_logger_rotate, "Reopens the log files",
340 logger_rotate_help };
342 static int handle_SIGXFSZ(int sig) {
344 ast_log(LOG_EVENT,"Rotated Logs Per SIGXFSZ\n");
346 ast_verbose("Rotated Logs Per SIGXFSZ\n");
350 int init_logger(void)
354 /* auto rotate if sig SIGXFSZ comes a-knockin */
355 (void) signal(SIGXFSZ,(void *) handle_SIGXFSZ);
357 /* register the relaod logger cli command */
358 ast_cli_register(&reload_logger_cli);
359 ast_cli_register(&rotate_logger_cli);
361 /* create the eventlog */
362 mkdir((char *)ast_config_AST_LOG_DIR, 0755);
363 snprintf(tmp, sizeof(tmp), "%s/%s", (char *)ast_config_AST_LOG_DIR, EVENTLOG);
364 eventlog = fopen((char *)tmp, "a");
367 ast_log(LOG_EVENT, "Started Asterisk Event Logger\n");
369 ast_verbose("Asterisk Event Logger Started %s\n",(char *)tmp);
372 ast_log(LOG_ERROR, "Unable to create event log: %s\n", strerror(errno));
376 /* create log channels */
381 static void ast_log_vsyslog(int level, const char *file, int line, const char *function, const char *fmt, va_list args) {
384 if(level >= SYSLOG_NLEVELS) {
385 /* we are locked here, so cannot ast_log() */
386 fprintf(stderr, "ast_log_vsyslog called with bogus level: %d\n", level);
389 if(file && line && function) {
390 snprintf(buf, sizeof(buf), "%s[%ld]: %s:%d in %s: ",
391 levels[level], (long)pthread_self(), file, line, function);
393 snprintf(buf, sizeof(buf), "VERBOSE[%ld]: ", (long)pthread_self());
396 vsnprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), fmt, args);
397 syslog(syslog_level_map[level], buf);
401 * send log messages to syslog and/or the console
403 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
405 struct logchannel *chan;
413 if (!option_verbose && !option_debug && (level == __LOG_DEBUG)) {
417 /* begin critical section */
418 ast_mutex_lock(&loglock);
420 if (level == __LOG_EVENT) {
423 /* Log events into the event log file, with a different format */
424 strftime(date, sizeof(date), "%b %e %T", &tm);
427 fprintf(eventlog, "%s asterisk[%d]: ", date, getpid());
428 vfprintf(eventlog, fmt, ap);
432 ast_mutex_unlock(&loglock);
439 if (chan->syslog && (chan->logmask & (1 << level))) {
441 ast_log_vsyslog(level, file, line, function, fmt, ap);
443 } else if ((chan->logmask & (1 << level)) && (chan->console || chan->fileptr)) {
445 char tmp1[80], tmp2[80], tmp3[80], tmp4[80];
448 localtime_r(&t, &tm);
450 strftime(date, sizeof(date), "%b %e %T", &tm);
452 sprintf(linestr, "%d", line);
453 snprintf(buf, sizeof(buf), "%s %s[%ld]: %s:%s %s: ",
455 term_color(tmp1, levels[level], colors[level], 0, sizeof(tmp1)),
456 (long)pthread_self(),
457 term_color(tmp2, file, COLOR_BRWHITE, 0, sizeof(tmp2)),
458 term_color(tmp3, linestr, COLOR_BRWHITE, 0, sizeof(tmp3)),
459 term_color(tmp4, function, COLOR_BRWHITE, 0, sizeof(tmp4)));
462 ast_console_puts(buf);
464 fprintf(chan->fileptr, buf);
466 vsnprintf(buf, sizeof(buf), fmt, ap);
469 ast_console_puts(buf);
471 fprintf(chan->fileptr, buf);
478 ast_mutex_unlock(&loglock);
479 /* end critical section */
482 extern void ast_verbose(const char *fmt, ...)
484 static char stuff[4096];
485 static int pos = 0, opos;
486 static int replacelast = 0, complete;
491 ast_mutex_lock(&msglist_lock);
492 vsnprintf(stuff + pos, sizeof(stuff) - pos, fmt, ap);
495 if (fmt[strlen(fmt)-1] == '\n')
500 if (msgcnt < MAX_MSG_QUEUE) {
501 /* Allocate new structure */
502 m = malloc(sizeof(struct msglist));
505 /* Recycle the oldest entry */
511 m->msg = strdup(stuff);
521 ast_log(LOG_ERROR, "Out of memory\n");
529 v->verboser(stuff, opos, replacelast, complete);
533 fprintf(stdout, stuff + opos); */
535 if (fmt[strlen(fmt)-1] != '\n')
538 replacelast = pos = 0;
542 if(stuff[strlen(stuff)-1] == '\n')
543 stuff[strlen(stuff)-1] = '\0';
544 ast_log_vsyslog(0, NULL, 0, NULL, fmt, ap);
547 ast_mutex_unlock(&msglist_lock);
550 int ast_verbose_dmesg(void (*v)(const char *string, int opos, int replacelast, int complete))
554 ast_mutex_lock(&msglist_lock);
556 /* Send all the existing entries that we have queued (i.e. they're likely to have missed) */
560 ast_mutex_unlock(&msglist_lock);
564 int ast_register_verbose(void (*v)(const char *string, int opos, int replacelast, int complete))
568 /* XXX Should be more flexible here, taking > 1 verboser XXX */
569 if ((tmp = malloc(sizeof (struct verb)))) {
571 ast_mutex_lock(&msglist_lock);
572 tmp->next = verboser;
576 /* Send all the existing entries that we have queued (i.e. they're likely to have missed) */
580 ast_mutex_unlock(&msglist_lock);
586 int ast_unregister_verbose(void (*v)(const char *string, int opos, int replacelast, int complete))
589 struct verb *tmp, *tmpl=NULL;
590 ast_mutex_lock(&msglist_lock);
593 if (tmp->verboser == v) {
595 tmpl->next = tmp->next;
597 verboser = tmp->next;
606 ast_mutex_unlock(&msglist_lock);