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/logger.h>
21 #include <asterisk/options.h>
22 #include <asterisk/channel.h>
23 #include <asterisk/config.h>
24 #include <asterisk/term.h>
25 #include <asterisk/cli.h>
35 #define MAX_MSG_QUEUE 200
37 static ast_mutex_t msglist_lock = AST_MUTEX_INITIALIZER;
38 static ast_mutex_t loglock = AST_MUTEX_INITIALIZER;
40 static struct msglist {
43 } *list = NULL, *last = NULL;
49 int facility; /* syslog */
53 static struct logfile *logfiles = NULL;
55 static int msgcnt = 0;
57 static FILE *eventlog = NULL;
59 static char *levels[] = {
67 static int colors[] = {
77 static int make_components(char *s, int lineno)
83 w = strsep(&stringp, ",");
85 while(*w && (*w < 33))
87 if (!strcasecmp(w, "debug"))
89 else if (!strcasecmp(w, "notice"))
91 else if (!strcasecmp(w, "warning"))
93 else if (!strcasecmp(w, "error"))
96 fprintf(stderr, "Logfile Warning: Unknown keyword '%s' at line %d of logger.conf\n", w, lineno);
98 w = strsep(&stringp, ",");
103 static struct logfile *make_logfile(char *fn, char *components, int lineno)
109 f = malloc(sizeof(struct logfile));
111 memset(f, 0, sizeof(struct logfile));
112 strncpy(f->fn, fn, sizeof(f->fn) - 1);
113 if (!strcasecmp(fn, "ignore")) {
115 } else if (!strcasecmp(fn, "console")) {
117 } else if (!strcasecmp(fn, "syslog")) {
119 f->facility = LOG_LOCAL0;
122 strncpy(tmp, fn, sizeof(tmp) - 1);
124 snprintf(tmp, sizeof(tmp), "%s/%s", (char *)ast_config_AST_LOG_DIR, fn);
125 f->f = fopen(tmp, "a");
127 /* Can't log here, since we're called with a lock */
128 fprintf(stderr, "Logger Warning: Unable to open log file '%s': %s\n", tmp, strerror(errno));
131 f->logflags = make_components(components, lineno);
137 static void init_logger_chain(void)
139 struct logfile *f, *cur;
140 struct ast_config *cfg;
141 struct ast_variable *var;
143 ast_mutex_lock(&loglock);
145 /* Free anything that is here */
149 if (f->f && (f->f != stdout) && (f->f != stderr))
157 ast_mutex_unlock(&loglock);
158 cfg = ast_load("logger.conf");
159 ast_mutex_lock(&loglock);
161 /* If no config file, we're fine */
163 ast_mutex_unlock(&loglock);
166 var = ast_variable_browse(cfg, "logfiles");
168 f = make_logfile(var->name, var->value, var->lineno);
176 /* Gotta have at least one. We'll make a NULL one */
177 logfiles = make_logfile("ignore", "", -1);
180 ast_mutex_unlock(&loglock);
185 int reload_logger(int rotate)
187 char old[AST_CONFIG_MAX_PATH];
188 char new[AST_CONFIG_MAX_PATH];
194 ast_mutex_lock(&loglock);
203 mkdir((char *)ast_config_AST_LOG_DIR, 0755);
204 snprintf(old, sizeof(old), "%s/%s", (char *)ast_config_AST_LOG_DIR, EVENTLOG);
208 snprintf(new, sizeof(new), "%s/%s.%d", (char *)ast_config_AST_LOG_DIR, EVENTLOG,x);
209 myf = fopen((char *)new, "r");
218 fprintf(stderr, "Unable to rename file '%s' to '%s'\n", old, new);
221 eventlog = fopen(old, "a");
225 if (f->f && (f->f != stdout) && (f->f != stderr)) {
229 snprintf(old, sizeof(old), "%s/%s", (char *)ast_config_AST_LOG_DIR,f->fn);
232 snprintf(new, sizeof(new), "%s/%s.%d", (char *)ast_config_AST_LOG_DIR,f->fn,x);
233 myf = fopen((char *)new, "r");
242 fprintf(stderr, "Unable to rename file '%s' to '%s'\n", old, new);
253 ast_mutex_unlock(&loglock);
257 ast_log(LOG_EVENT, "Restarted Asterisk Event Logger\n");
259 ast_verbose("Asterisk Event Logger restarted\n");
262 ast_log(LOG_ERROR, "Unable to create event log: %s\n", strerror(errno));
267 static int handle_logger_reload(int fd, int argc, char *argv[])
271 ast_cli(fd, "Failed to reloadthe logger\n");
272 return RESULT_FAILURE;
275 return RESULT_SUCCESS;
285 static int handle_logger_rotate(int fd, int argc, char *argv[])
289 ast_cli(fd, "Failed to reloadthe logger\n");
290 return RESULT_FAILURE;
293 return RESULT_SUCCESS;
297 void (*verboser)(const char *string, int opos, int replacelast, int complete);
302 static char logger_reload_help[] =
303 "Usage: logger reload\n"
304 " Reopens the log files. Use after a rotating the log files\n";
307 static char logger_rotate_help[] =
308 "Usage: logger reload\n"
309 " Rotates and Reopens the log files.\n";
312 static struct ast_cli_entry reload_logger_cli =
313 { { "logger", "reload", NULL },
314 handle_logger_reload, "Reopens the log files",
315 logger_reload_help };
318 static struct ast_cli_entry rotate_logger_cli =
319 { { "logger", "rotate", NULL },
320 handle_logger_rotate, "Reopens the log files",
321 logger_rotate_help };
325 static int handle_SIGXFSZ(int sig) {
327 ast_log(LOG_EVENT,"Rotated Logs Per SIGXFSZ\n");
329 ast_verbose("Rotated Logs Per SIGXFSZ\n");
334 int init_logger(void)
336 char tmp[AST_CONFIG_MAX_PATH];
340 /* auto rotate if sig SIGXFSZ comes a-knockin */
341 (void) signal(SIGXFSZ,(void *) handle_SIGXFSZ);
343 /* register the relaod logger cli command */
344 ast_cli_register(&reload_logger_cli);
345 ast_cli_register(&rotate_logger_cli);
347 mkdir((char *)ast_config_AST_LOG_DIR, 0755);
348 snprintf(tmp, sizeof(tmp), "%s/%s", (char *)ast_config_AST_LOG_DIR, EVENTLOG);
349 eventlog = fopen((char *)tmp, "a");
352 ast_log(LOG_EVENT, "Started Asterisk Event Logger\n");
354 ast_verbose("Asterisk Event Logger Started %s\n",(char *)tmp);
357 ast_log(LOG_ERROR, "Unable to create event log: %s\n", strerror(errno));
363 extern void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
376 if (!option_verbose && !option_debug && (!level)) {
379 ast_mutex_lock(&loglock);
380 if (level == 1 /* Event */) {
384 /* Log events into the event log file, with a different format */
385 strftime(date, sizeof(date), "%b %e %T", &tm);
386 fprintf(eventlog, "%s asterisk[%d]: ", date, getpid());
388 vfprintf(eventlog, fmt, ap);
392 /** Cannot use ast_log() from locked section of ast_log()!
393 ast_log(LOG_WARNING, "Unable to retrieve local time?\n"); **/
394 fprintf(stderr, "ast_log: Unable to retrieve local time for %ld?\n", (long)t);
399 if (f->logflags & (1 << level) && f->facility) {
402 strftime(date, sizeof(date), "%b %e %T", &tm);
404 openlog("asterisk_pbx",LOG_PID,f->facility);
405 syslog(LOG_INFO|f->facility,"%s %s[%ld]: File %s, Line %d (%s): ",date,
406 levels[level], (long)pthread_self(), file, line, function);
410 else if (f->logflags & (1 << level) && f->f) {
411 if ((f->f != stdout) && (f->f != stderr)) {
414 strftime(date, sizeof(date), "%b %e %T", &tm);
415 fprintf(f->f, "%s %s[%ld]: File %s, Line %d (%s): ", date, levels[level], (long)pthread_self(), file, line, function);
417 sprintf(linestr, "%d", line);
418 fprintf(f->f, "%s[%ld]: File %s, Line %s (%s): ",
419 term_color(tmp, levels[level], colors[level], 0, sizeof(tmp)),
420 (long)pthread_self(),
421 term_color(tmp2, file, COLOR_BRWHITE, 0, sizeof(tmp2)),
422 term_color(tmp3, linestr, COLOR_BRWHITE, 0, sizeof(tmp3)),
423 term_color(tmp4, function, COLOR_BRWHITE, 0, sizeof(tmp4)));
426 vfprintf(f->f, fmt, ap);
433 fprintf(stdout, "%s[%ld]: File %s, Line %d (%s): ", levels[level], (long)pthread_self(), file, line, function);
435 vfprintf(stdout, fmt, ap);
440 ast_mutex_unlock(&loglock);
443 extern void ast_verbose(const char *fmt, ...)
445 static char stuff[4096];
446 static int pos = 0, opos;
447 static int replacelast = 0, complete;
452 ast_mutex_lock(&msglist_lock);
453 vsnprintf(stuff + pos, sizeof(stuff) - pos, fmt, ap);
456 if (fmt[strlen(fmt)-1] == '\n')
461 if (msgcnt < MAX_MSG_QUEUE) {
462 /* Allocate new structure */
463 m = malloc(sizeof(struct msglist));
466 /* Recycle the oldest entry */
472 m->msg = strdup(stuff);
482 ast_log(LOG_ERROR, "Out of memory\n");
490 v->verboser(stuff, opos, replacelast, complete);
494 fprintf(stdout, stuff + opos); */
496 if (fmt[strlen(fmt)-1] != '\n')
499 replacelast = pos = 0;
501 ast_mutex_unlock(&msglist_lock);
504 int ast_verbose_dmesg(void (*v)(const char *string, int opos, int replacelast, int complete))
508 ast_mutex_lock(&msglist_lock);
510 /* Send all the existing entries that we have queued (i.e. they're likely to have missed) */
514 ast_mutex_unlock(&msglist_lock);
518 int ast_register_verbose(void (*v)(const char *string, int opos, int replacelast, int complete))
522 /* XXX Should be more flexible here, taking > 1 verboser XXX */
523 if ((tmp = malloc(sizeof (struct verb)))) {
525 ast_mutex_lock(&msglist_lock);
526 tmp->next = verboser;
530 /* Send all the existing entries that we have queued (i.e. they're likely to have missed) */
534 ast_mutex_unlock(&msglist_lock);
540 int ast_unregister_verbose(void (*v)(const char *string, int opos, int replacelast, int complete))
543 struct verb *tmp, *tmpl=NULL;
544 ast_mutex_lock(&msglist_lock);
547 if (tmp->verboser == v) {
549 tmpl->next = tmp->next;
551 verboser = tmp->next;
560 ast_mutex_unlock(&msglist_lock);