[asterisk-commits] russell: branch russell/ast_verbose_threadstorage r38272 - in /team/russell/a...

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Wed Jul 26 16:18:18 MST 2006


Author: russell
Date: Wed Jul 26 18:18:18 2006
New Revision: 38272

URL: http://svn.digium.com/view/asterisk?rev=38272&view=rev
Log:
merge refactoring of ast_verbose handling, including the use of thread local
storage as well as the removal of some dead code and some other overly
complicated and unnecessary stuff

Modified:
    team/russell/ast_verbose_threadstorage/asterisk.c
    team/russell/ast_verbose_threadstorage/include/asterisk/logger.h
    team/russell/ast_verbose_threadstorage/logger.c

Modified: team/russell/ast_verbose_threadstorage/asterisk.c
URL: http://svn.digium.com/view/asterisk/team/russell/ast_verbose_threadstorage/asterisk.c?rev=38272&r1=38271&r2=38272&view=diff
==============================================================================
--- team/russell/ast_verbose_threadstorage/asterisk.c (original)
+++ team/russell/ast_verbose_threadstorage/asterisk.c Wed Jul 26 18:18:18 2006
@@ -746,23 +746,9 @@
 	ast_network_puts(string);
 }
 
-static void network_verboser(const char *s, int pos, int replace, int complete)
-	/* ARGUSED */
-{
-	if (replace) {
-		char *t;
-		if ((t = alloca(strlen(s) + 2))) {
-			sprintf(t, "\r%s", s);
-			if (complete)
-				ast_network_puts_mutable(t);
-		} else {
-			ast_log(LOG_ERROR, "Out of memory\n");
-			ast_network_puts_mutable(s);
-		}
-	} else {
-		if (complete)
-			ast_network_puts_mutable(s);
-	}
+static void network_verboser(const char *s)
+{
+	ast_network_puts_mutable(s);
 }
 
 static pthread_t lthread;
@@ -1203,29 +1189,25 @@
 	return NULL;
 }
 
-static void console_verboser(const char *s, int pos, int replace, int complete)
+static void console_verboser(const char *s)
 {
 	char tmp[80];
 	const char *c = NULL;
-	/* Return to the beginning of the line */
-	if (!pos) {
-		fprintf(stdout, "\r");
-		if ((c = fix_header(tmp, sizeof(tmp), s, VERBOSE_PREFIX_4)) ||
-			(c = fix_header(tmp, sizeof(tmp), s, VERBOSE_PREFIX_3)) ||
-			(c = fix_header(tmp, sizeof(tmp), s, VERBOSE_PREFIX_2)) ||
-			(c = fix_header(tmp, sizeof(tmp), s, VERBOSE_PREFIX_1)))
-			fputs(tmp, stdout);
-	}
-	if (c)
-		fputs(c + pos,stdout);
-	else
-		fputs(s + pos,stdout);
+
+	if ((c = fix_header(tmp, sizeof(tmp), s, VERBOSE_PREFIX_4)) ||
+	    (c = fix_header(tmp, sizeof(tmp), s, VERBOSE_PREFIX_3)) ||
+	    (c = fix_header(tmp, sizeof(tmp), s, VERBOSE_PREFIX_2)) ||
+	    (c = fix_header(tmp, sizeof(tmp), s, VERBOSE_PREFIX_1))) {
+		fputs(tmp, stdout);
+		fputs(c, stdout);
+	} else
+		fputs(s, stdout);
+
 	fflush(stdout);
-	if (complete) {
-		/* Wake up a poll()ing console */
-		if (ast_opt_console && consolethread != AST_PTHREADT_NULL)
-			pthread_kill(consolethread, SIGURG);
-	}
+	
+	/* Wake up a poll()ing console */
+	if (ast_opt_console && consolethread != AST_PTHREADT_NULL)
+		pthread_kill(consolethread, SIGURG);
 }
 
 static int ast_all_zeros(char *s)

Modified: team/russell/ast_verbose_threadstorage/include/asterisk/logger.h
URL: http://svn.digium.com/view/asterisk/team/russell/ast_verbose_threadstorage/include/asterisk/logger.h?rev=38272&r1=38271&r2=38272&view=diff
==============================================================================
--- team/russell/ast_verbose_threadstorage/include/asterisk/logger.h (original)
+++ team/russell/ast_verbose_threadstorage/include/asterisk/logger.h Wed Jul 26 18:18:18 2006
@@ -77,9 +77,9 @@
 void ast_verbose(const char *fmt, ...)
 	__attribute__ ((format (printf, 1, 2)));
 
-int ast_register_verbose(void (*verboser)(const char *string, int opos, int replacelast, int complete));
-int ast_unregister_verbose(void (*verboser)(const char *string, int opos, int replacelast, int complete));
-int ast_verbose_dmesg(void (*verboser)(const char *string, int opos, int replacelast, int complete));
+int ast_register_verbose(void (*verboser)(const char *string));
+int ast_unregister_verbose(void (*verboser)(const char *string));
+
 void ast_console_puts(const char *string);
 
 void ast_console_puts_mutable(const char *string);

Modified: team/russell/ast_verbose_threadstorage/logger.c
URL: http://svn.digium.com/view/asterisk/team/russell/ast_verbose_threadstorage/logger.c?rev=38272&r1=38271&r2=38272&view=diff
==============================================================================
--- team/russell/ast_verbose_threadstorage/logger.c (original)
+++ team/russell/ast_verbose_threadstorage/logger.c Wed Jul 26 18:18:18 2006
@@ -84,7 +84,6 @@
 
 static char dateformat[256] = "%b %e %T";		/* Original Asterisk Format */
 
-AST_MUTEX_DEFINE_STATIC(msglist_lock);
 AST_MUTEX_DEFINE_STATIC(loglock);
 static int filesize_reload_needed = 0;
 static int global_logmask = -1;
@@ -93,11 +92,6 @@
 	unsigned int queue_log:1;
 	unsigned int event_log:1;
 } logfiles = { 1, 1 };
-
-static struct msglist {
-	char *msg;
-	struct msglist *next;
-} *list = NULL, *last = NULL;
 
 static char hostname[MAXHOSTNAMELEN];
 
@@ -119,8 +113,6 @@
 
 static struct logchannel *logchannels = NULL;
 
-static int msgcnt = 0;
-
 static FILE *eventlog = NULL;
 static FILE *qlog = NULL;
 
@@ -143,6 +135,20 @@
 	COLOR_GREEN,
 	COLOR_BRGREEN
 };
+
+static pthread_once_t verbose_buf_once = PTHREAD_ONCE_INIT;
+static pthread_key_t verbose_buf_key;
+
+#define VERBOSE_BUF_INIT_SIZE	128
+
+#ifdef __AST_DEBUG_MALLOC
+static void FREE(void *ptr)
+{
+	free(ptr);
+}
+#else
+#define FREE free
+#endif
 
 static int make_components(char *s, int lineno)
 {
@@ -383,7 +389,6 @@
 	FILE *myf;
 	int x, res = 0;
 
-	ast_mutex_lock(&msglist_lock);	/* to avoid deadlock */
 	ast_mutex_lock(&loglock);
 	if (eventlog) 
 		fclose(eventlog);
@@ -490,7 +495,6 @@
 		}
 	}
 	ast_mutex_unlock(&loglock);
-	ast_mutex_unlock(&msglist_lock);
 
 	return res;
 }
@@ -554,11 +558,12 @@
 	return RESULT_SUCCESS;
 }
 
-static struct verb {
-	void (*verboser)(const char *string, int opos, int replacelast, int complete);
-	struct verb *next;
-} *verboser = NULL;
-
+struct verb {
+	void (*verboser)(const char *string);
+	AST_LIST_ENTRY(verb) list;
+};
+
+static AST_LIST_HEAD_STATIC(verbosers, verb);
 
 static char logger_reload_help[] =
 "Usage: logger reload\n"
@@ -637,21 +642,6 @@
 
 void close_logger(void)
 {
-	struct msglist *m, *tmp;
-
-	ast_mutex_lock(&msglist_lock);
-	m = list;
-	while(m) {
-		if (m->msg) {
-			free(m->msg);
-		}
-		tmp = m->next;
-		free(m);
-		m = tmp;
-	}
-	list = last = NULL;
-	msgcnt = 0;
-	ast_mutex_unlock(&msglist_lock);
 	return;
 }
 
@@ -838,154 +828,80 @@
 #endif
 }
 
+static void verbose_buf_key_create(void)
+{
+	pthread_key_create(&verbose_buf_key, FREE);
+}
+
 void ast_verbose(const char *fmt, ...)
 {
-	static char stuff[4096];
-	static int len = 0;
-	static int replacelast = 0;
-
-	int complete;
-	int olen;
-	struct msglist *m;
 	struct verb *v;
-	
+	int res;
 	va_list ap;
+	struct {
+		size_t len;
+		char str[0];
+	} *buf;
+
+	pthread_once(&verbose_buf_once, verbose_buf_key_create);
+	if (!(buf = pthread_getspecific(verbose_buf_key))) {
+		if (!(buf = ast_malloc(VERBOSE_BUF_INIT_SIZE + sizeof(*buf))))
+			return;
+		buf->len = VERBOSE_BUF_INIT_SIZE;
+		pthread_setspecific(verbose_buf_key, buf);	
+	}
+
 	va_start(ap, fmt);
-
-	if (ast_opt_timestamp) {
-		time_t t;
-		struct tm tm;
-		char date[40];
-		char *datefmt;
-
-		time(&t);
-		localtime_r(&t, &tm);
-		strftime(date, sizeof(date), dateformat, &tm);
-		datefmt = alloca(strlen(date) + 3 + strlen(fmt) + 1);
-	}
-
-	/* this lock is also protecting against multiple threads
-	   being in this function at the same time, so it must be
-	   held before any of the static variables are accessed
-	*/
-	ast_mutex_lock(&msglist_lock);
-
-	/* there is a potential security problem here: if formatting
-	   the current date using 'dateformat' results in a string
-	   containing '%', then the vsnprintf() call below will
-	   probably try to access random memory
-	*/
-	vsnprintf(stuff + len, sizeof(stuff) - len, fmt, ap);
+	res = vsnprintf(buf->str, buf->len, fmt, ap);
+	while (res >= buf->len) {
+		if (!(buf = ast_realloc(buf, (buf->len * 2) + sizeof(*buf)))) {
+			va_end(ap);
+			return;
+		}
+		buf->len *= 2;
+		pthread_setspecific(verbose_buf_key, buf);
+		res = vsnprintf(buf->str, buf->len, fmt, ap);
+	}
 	va_end(ap);
 
-	olen = len;
-	len = strlen(stuff);
-
-	complete = (stuff[len - 1] == '\n') ? 1 : 0;
-
-	/* If we filled up the stuff completely, then log it even without the '\n' */
-	if (len >= sizeof(stuff) - 1) {
-		complete = 1;
-		len = 0;
-	}
-
-	if (complete) {
-		if (msgcnt < MAX_MSG_QUEUE) {
-			/* Allocate new structure */
-			if ((m = ast_malloc(sizeof(*m))))
-				msgcnt++;
-		} else {
-			/* Recycle the oldest entry */
-			m = list;
-			list = list->next;
-			free(m->msg);
-		}
-		if (m) {
-			if ((m->msg = ast_strdup(stuff))) {
-				if (last)
-					last->next = m;
-				else
-					list = m;
-				m->next = NULL;
-				last = m;
-			} else {
-				msgcnt--;
-				free(m);
-			}
-		}
-	}
-
-	for (v = verboser; v; v = v->next)
-		v->verboser(stuff, olen, replacelast, complete);
-
-	ast_log(LOG_VERBOSE, "%s", stuff);
-
-	if (len) {
-		if (!complete)
-			replacelast = 1;
-		else 
-			replacelast = len = 0;
-	}
-
-	ast_mutex_unlock(&msglist_lock);
-}
-
-int ast_verbose_dmesg(void (*v)(const char *string, int opos, int replacelast, int complete))
-{
-	struct msglist *m;
-	ast_mutex_lock(&msglist_lock);
-	m = list;
-	while(m) {
-		/* Send all the existing entries that we have queued (i.e. they're likely to have missed) */
-		v(m->msg, 0, 0, 1);
-		m = m->next;
-	}
-	ast_mutex_unlock(&msglist_lock);
+	AST_LIST_LOCK(&verbosers);
+	AST_LIST_TRAVERSE(&verbosers, v, list)
+		v->verboser(buf->str);
+	AST_LIST_UNLOCK(&verbosers);
+
+	ast_log(LOG_VERBOSE, "%s", buf->str);
+}
+
+int ast_register_verbose(void (*v)(const char *string)) 
+{
+	struct verb *verb;
+
+	if (!(verb = ast_malloc(sizeof(*verb))))
+		return -1;
+
+	verb->verboser = v;
+
+	AST_LIST_LOCK(&verbosers);
+	AST_LIST_INSERT_HEAD(&verbosers, verb, list);
+	AST_LIST_UNLOCK(&verbosers);
+	
 	return 0;
 }
 
-int ast_register_verbose(void (*v)(const char *string, int opos, int replacelast, int complete)) 
-{
-	struct msglist *m;
-	struct verb *tmp;
-	/* XXX Should be more flexible here, taking > 1 verboser XXX */
-	if ((tmp = ast_malloc(sizeof(*tmp)))) {
-		tmp->verboser = v;
-		ast_mutex_lock(&msglist_lock);
-		tmp->next = verboser;
-		verboser = tmp;
-		m = list;
-		while(m) {
-			/* Send all the existing entries that we have queued (i.e. they're likely to have missed) */
-			v(m->msg, 0, 0, 1);
-			m = m->next;
-		}
-		ast_mutex_unlock(&msglist_lock);
-		return 0;
-	}
-	return -1;
-}
-
-int ast_unregister_verbose(void (*v)(const char *string, int opos, int replacelast, int complete))
-{
-	int res = -1;
-	struct verb *tmp, *tmpl=NULL;
-	ast_mutex_lock(&msglist_lock);
-	tmp = verboser;
-	while(tmp) {
-		if (tmp->verboser == v)	{
-			if (tmpl)
-				tmpl->next = tmp->next;
-			else
-				verboser = tmp->next;
-			free(tmp);
+int ast_unregister_verbose(void (*v)(const char *string))
+{
+	struct verb *cur;
+
+	AST_LIST_LOCK(&verbosers);
+	AST_LIST_TRAVERSE_SAFE_BEGIN(&verbosers, cur, list) {
+		if (cur->verboser == v) {
+			AST_LIST_REMOVE_CURRENT(&verbosers, list);
+			free(cur);
 			break;
 		}
-		tmpl = tmp;
-		tmp = tmp->next;
-	}
-	if (tmp)
-		res = 0;
-	ast_mutex_unlock(&msglist_lock);
-	return res;
-}
+	}
+	AST_LIST_TRAVERSE_SAFE_END
+	AST_LIST_UNLOCK(&verbosers);
+	
+	return cur ? 0 : -1;
+}



More information about the asterisk-commits mailing list