Skip to content
Snippets Groups Projects
logger.c 34.7 KiB
Newer Older
  • Learn to ignore specific revisions
  • 
    	/* auto rotate if sig SIGXFSZ comes a-knockin */
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    	(void) signal(SIGXFSZ, (void *) handle_SIGXFSZ);
    
    	/* start logger thread */
    	ast_cond_init(&logcond, NULL);
    	if (ast_pthread_create(&logthread, NULL, logger_thread, NULL) < 0) {
    		ast_cond_destroy(&logcond);
    		return -1;
    	}
    
    
    	/* register the logger cli commands */
    	ast_cli_register_multiple(cli_logger, sizeof(cli_logger) / sizeof(struct ast_cli_entry));
    
    	/* create log channels */
    
    
    	/* create the eventlog */
    	if (logfiles.event_log) {
    
    		snprintf(tmp, sizeof(tmp), "%s/%s", ast_config_AST_LOG_DIR, EVENTLOG);
    		eventlog = fopen(tmp, "a");
    
    		if (eventlog) {
    			ast_log(LOG_EVENT, "Started Asterisk Event Logger\n");
    
    			ast_verb(1, "Asterisk Event Logger Started %s\n", tmp);
    
    			ast_log(LOG_ERROR, "Unable to create event log: %s\n", strerror(errno));
    
    	if (logfiles.queue_log) {
    
    		snprintf(tmp, sizeof(tmp), "%s/%s", ast_config_AST_LOG_DIR, queue_log_name);
    
    		qlog = fopen(tmp, "a");
    		ast_queue_log("NONE", "NONE", "NONE", "QUEUESTART", "%s", "");
    	}
    	return res;
    
    	struct logchannel *f = NULL;
    
    	/* Stop logger thread */
    	AST_LIST_LOCK(&logmsgs);
    	close_logger_thread = 1;
    	ast_cond_signal(&logcond);
    	AST_LIST_UNLOCK(&logmsgs);
    
    	if (logthread != AST_PTHREADT_NULL)
    		pthread_join(logthread, NULL);
    
    
    
    	if (eventlog) {
    		fclose(eventlog);
    		eventlog = NULL;
    	}
    
    	if (qlog) {
    		fclose(qlog);
    		qlog = NULL;
    	}
    
    
    	AST_RWLIST_TRAVERSE(&logchannels, f, list) {
    
    		if (f->fileptr && (f->fileptr != stdout) && (f->fileptr != stderr)) {
    			fclose(f->fileptr);
    			f->fileptr = NULL;
    
    /*!
     * \brief send log messages to syslog and/or the console
    
     */
    void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    
    	struct logmsg *logmsg = NULL;
    	struct ast_str *buf = NULL;
    
    	struct ast_tm tm;
    	struct timeval tv = ast_tvnow();
    
    	va_list ap;
    
    	if (!(buf = ast_str_thread_get(&log_buf, LOG_BUF_INIT_SIZE)))
    
    	if (AST_RWLIST_EMPTY(&logchannels)) {
    
    		/*
    		 * we don't have the logger chain configured yet,
    		 * so just log to stdout
    
    		if (level != __LOG_VERBOSE) {
    			int res;
    			va_start(ap, fmt);
    
    			res = ast_str_set_va(&buf, BUFSIZ, fmt, ap); /* XXX BUFSIZ ? */
    
    			if (res != AST_DYNSTR_BUILD_FAILED) {
    				term_filter_escapes(buf->str);
    
    	/* don't display LOG_DEBUG messages unless option_verbose _or_ option_debug
    	   are non-zero; LOG_DEBUG messages can still be displayed if option_debug
    	   is zero, if option_verbose is non-zero (this allows for 'level zero'
    	   LOG_DEBUG messages to be displayed, if the logmask on any channel
    	   allows it)
    	*/
    
    	if (!option_verbose && !option_debug && (level == __LOG_DEBUG))
    
    		return;
    
    	/* Ignore anything that never gets logged anywhere */
    	if (!(global_logmask & (1 << level)))
    		return;
    
    	/* Build string */
    	va_start(ap, fmt);
    	res = ast_str_set_va(&buf, BUFSIZ, fmt, ap);
    	va_end(ap);
    
    	/* If the build failed, then abort and free this structure */
    	if (res == AST_DYNSTR_BUILD_FAILED)
    		return;
    
    	/* Create a new logging message */
    	if (!(logmsg = ast_calloc(1, sizeof(*logmsg) + res + 1)))
    
    		return;
    
    	/* Copy string over */
    	strcpy(logmsg->str, buf->str);
    
    	/* Set type to be normal */
    	logmsg->type = LOGMSG_NORMAL;
    
    	ast_localtime(&tv, &tm, NULL);
    	ast_strftime(logmsg->date, sizeof(logmsg->date), dateformat, &tm);
    
    
    	/* Copy over data */
    	logmsg->level = level;
    	logmsg->line = line;
    
    	ast_copy_string(logmsg->file, file, sizeof(logmsg->file));
    	ast_copy_string(logmsg->function, function, sizeof(logmsg->function));
    
    
    	/* If the logger thread is active, append it to the tail end of the list - otherwise skip that step */
    	if (logthread != AST_PTHREADT_NULL) {
    		AST_LIST_LOCK(&logmsgs);
    		AST_LIST_INSERT_TAIL(&logmsgs, logmsg, list);
    		ast_cond_signal(&logcond);
    		AST_LIST_UNLOCK(&logmsgs);
    	} else {
    		logger_print_normal(logmsg);
    
    #ifdef HAVE_BKTR
    
    struct ast_bt *ast_bt_create(void) 
    {
    	struct ast_bt *bt = ast_calloc(1, sizeof(*bt));
    	if (!bt) {
    		ast_log(LOG_ERROR, "Unable to allocate memory for backtrace structure!\n");
    		return NULL;
    	}
    
    	bt->alloced = 1;
    
    	ast_bt_get_addresses(bt);
    
    	return bt;
    }
    
    int ast_bt_get_addresses(struct ast_bt *bt)
    {
    	bt->num_frames = backtrace(bt->addresses, AST_MAX_BT_FRAMES);
    
    	return 0;
    }
    
    void *ast_bt_destroy(struct ast_bt *bt)
    {
    	if (bt->alloced) {
    		ast_free(bt);
    	}
    
    	return NULL;
    }
    
    #endif /* HAVE_BKTR */
    
    
    void ast_backtrace(void)
    {
    
    	struct ast_bt *backtrace;
    	int i = 0;
    
    	if (!(backtrace = ast_bt_create())) {
    		ast_log(LOG_WARNING, "Unable to allocate space for backtrace structure\n");
    		return;
    	}
    
    	if ((strings = backtrace_symbols(backtrace->addresses, backtrace->num_frames))) {
    		ast_debug(1, "Got %d backtrace record%c\n", backtrace->num_frames, backtrace->num_frames != 1 ? 's' : ' ');
    		for (i = 0; i < backtrace->num_frames; i++) {
    
    #if __WORDSIZE == 32
    
    			ast_log(LOG_DEBUG, "#%d: [%08X] %s\n", i, (unsigned int)backtrace->addresses[i], strings[i]);
    
    #elif __WORDSIZE == 64
    
    			ast_log(LOG_DEBUG, "#%d: [%016lX] %s\n", i, (unsigned long)backtrace->addresses[i], strings[i]);
    
    		free(strings);
    	} else {
    		ast_debug(1, "Could not allocate memory for backtrace\n");
    
    	ast_bt_destroy(backtrace);
    
    	ast_log(LOG_WARNING, "Must run configure with '--with-execinfo' for stack backtraces.\n");
    
    void __ast_verbose(const char *file, int line, const char *func, const char *fmt, ...)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    
    	struct logmsg *logmsg = NULL;
    	struct ast_str *buf = NULL;
    	int res = 0;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	va_list ap;
    
    	if (!(buf = ast_str_thread_get(&verbose_buf, VERBOSE_BUF_INIT_SIZE)))
    		return;
    
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    	if (ast_opt_timestamp) {
    		struct timeval tv;
    		struct ast_tm tm;
    		char date[40];
    		char *datefmt;
    
    Olle Johansson's avatar
    Olle Johansson committed
    		tv = ast_tvnow();
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    		ast_localtime(&tv, &tm, NULL);
    		ast_strftime(date, sizeof(date), dateformat, &tm);
    		datefmt = alloca(strlen(date) + 3 + strlen(fmt) + 1);
    
    		sprintf(datefmt, "%c[%s] %s", 127, date, fmt);
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    		fmt = datefmt;
    
    	} else {
    		char *tmp = alloca(strlen(fmt) + 2);
    		sprintf(tmp, "%c%s", 127, fmt);
    		fmt = tmp;
    
    	/* Build string */
    	va_start(ap, fmt);
    	res = ast_str_set_va(&buf, 0, fmt, ap);
    	va_end(ap);
    
    	/* If the build failed then we can drop this allocated message */
    	if (res == AST_DYNSTR_BUILD_FAILED)
    		return;
    
    	if (!(logmsg = ast_calloc(1, sizeof(*logmsg) + res + 1)))
    		return;
    
    	strcpy(logmsg->str, buf->str);
    
    
    	ast_log(__LOG_VERBOSE, file, line, func, "%s", logmsg->str + 1);
    
    	/* Set type */
    	logmsg->type = LOGMSG_VERBOSE;
    	
    	/* Add to the list and poke the thread if possible */
    	if (logthread != AST_PTHREADT_NULL) {
    		AST_LIST_LOCK(&logmsgs);
    		AST_LIST_INSERT_TAIL(&logmsgs, logmsg, list);
    		ast_cond_signal(&logcond);
    		AST_LIST_UNLOCK(&logmsgs);
    	} else {
    		logger_print_verbose(logmsg);
    
    int ast_register_verbose(void (*v)(const char *string)) 
    {
    	struct verb *verb;
    
    	if (!(verb = ast_malloc(sizeof(*verb))))
    		return -1;
    
    	verb->verboser = v;
    
    	AST_RWLIST_WRLOCK(&verbosers);
    	AST_RWLIST_INSERT_HEAD(&verbosers, verb, list);
    	AST_RWLIST_UNLOCK(&verbosers);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	return 0;
    }
    
    int ast_unregister_verbose(void (*v)(const char *string))
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    
    	struct verb *cur;
    
    	AST_RWLIST_WRLOCK(&verbosers);
    	AST_RWLIST_TRAVERSE_SAFE_BEGIN(&verbosers, cur, list) {
    
    		if (cur->verboser == v) {
    
    			AST_RWLIST_REMOVE_CURRENT(list);
    
    	AST_RWLIST_TRAVERSE_SAFE_END;
    
    Mark Spencer's avatar
    Mark Spencer committed
    }