Skip to content
Snippets Groups Projects
logger.c 32.2 KiB
Newer Older
  • Learn to ignore specific revisions
  • 			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->file = file;
    	logmsg->line = line;
    	logmsg->function = 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);
    
    void ast_backtrace(void)
    {
    
    #ifdef AST_DEVMODE
    
    	int count=0, i=0;
    	void **addresses;
    	char **strings;
    
    
    	if ((addresses = ast_calloc(MAX_BACKTRACE_FRAMES, sizeof(*addresses)))) {
    
    		count = backtrace(addresses, MAX_BACKTRACE_FRAMES);
    
    		if ((strings = backtrace_symbols(addresses, count))) {
    
    			ast_debug(1, "Got %d backtrace record%c\n", count, count != 1 ? 's' : ' ');
    
    			for (i=0; i < count ; i++) {
    
    #if __WORDSIZE == 32
    				ast_log(LOG_DEBUG, "#%d: [%08X] %s\n", i, (unsigned int)addresses[i], strings[i]);
    #elif __WORDSIZE == 64
    				ast_log(LOG_DEBUG, "#%d: [%016lX] %s\n", i, (unsigned long)addresses[i], strings[i]);
    #endif
    
    			}
    			free(strings);
    		} else {
    
    			ast_debug(1, "Could not allocate memory for backtrace\n");
    
    	ast_log(LOG_WARNING, "Must run configure with '--enable-dev-mode' for stack backtraces.\n");
    
    #else /* ndef linux */
    
    	ast_log(LOG_WARNING, "Inline stack backtraces are only available on the Linux platform.\n");
    
    void ast_verbose(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;
    
    
            if (ast_opt_timestamp) {
    
                    char date[40];
                    char *datefmt;
    
    
    Olle Johansson's avatar
    Olle Johansson committed
    		tv = ast_tvnow();
    
                    ast_localtime(&tv, &tm, NULL);
                    ast_strftime(date, sizeof(date), dateformat, &tm);
    
                    datefmt = alloca(strlen(date) + 3 + strlen(fmt) + 1);
                    sprintf(datefmt, "[%s] %s", date, fmt);
                    fmt = datefmt;
            }
    
    
    	/* 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);
    
    
    	/* 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
    }