[asterisk-commits] russell: branch russell/ast_verbose_threadstorage r38413 - /team/russell/ast_...

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Fri Jul 28 01:36:36 MST 2006


Author: russell
Date: Fri Jul 28 03:36:34 2006
New Revision: 38413

URL: http://svn.digium.com/view/asterisk?rev=38413&view=rev
Log:
convert ast_log to the new interface

Modified:
    team/russell/ast_verbose_threadstorage/logger.c

Modified: team/russell/ast_verbose_threadstorage/logger.c
URL: http://svn.digium.com/view/asterisk/team/russell/ast_verbose_threadstorage/logger.c?rev=38413&r1=38412&r2=38413&view=diff
==============================================================================
--- team/russell/ast_verbose_threadstorage/logger.c (original)
+++ team/russell/ast_verbose_threadstorage/logger.c Fri Jul 28 03:36:34 2006
@@ -135,12 +135,9 @@
 };
 
 AST_THREADSTORAGE(verbose_buf, verbose_buf_init);
-
 #define VERBOSE_BUF_INIT_SIZE   128
 
-static pthread_once_t log_buf_once = PTHREAD_ONCE_INIT;
-static pthread_key_t log_buf_key;
-
+AST_THREADSTORAGE(log_buf, log_buf_init);
 #define LOG_BUF_INIT_SIZE       128
 
 static int make_components(char *s, int lineno)
@@ -675,34 +672,21 @@
 	syslog(syslog_level_map[level], "%s", buf);
 }
 
-static void log_buf_key_create(void)
-{
-	pthread_key_create(&log_buf_key, FREE);
-}
-
 /*!
  * \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, ...)
 {
 	struct logchannel *chan;
+	struct ast_dynamic_str *buf;
 	time_t t;
 	struct tm tm;
 	char date[256];
-	struct {
-		size_t len;
-		char str[0];
-	} *buf;
 
 	va_list ap;
 
-	pthread_once(&log_buf_once, log_buf_key_create);
-	if (!(buf = pthread_getspecific(log_buf_key))) {
-		if (!(buf = ast_malloc(LOG_BUF_INIT_SIZE + sizeof(*buf))))
-			return;
-		buf->len = LOG_BUF_INIT_SIZE;
-		pthread_setspecific(log_buf_key, buf);
-	}
+	if (!(buf = ast_dynamic_str_threadget(&log_buf, LOG_BUF_INIT_SIZE)))
+		return;
 
 	/* 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
@@ -757,7 +741,8 @@
 					int res;
 					sprintf(linestr, "%d", line);
 
-					res = snprintf(buf->str, buf->len, ast_opt_timestamp ? "[%s] %s[%ld]: %s:%s %s: " : "%s %s[%ld]: %s:%s %s: ",
+					ast_dynamic_str_threadbuild(&buf, BUFSIZ, &log_buf,
+						ast_opt_timestamp ? "[%s] %s[%ld]: %s:%s %s: " : "%s %s[%ld]: %s:%s %s: ",
 						date,
 						term_color(tmp1, levels[level], colors[level], 0, sizeof(tmp1)),
 						(long)GETTID(),
@@ -765,55 +750,23 @@
 						term_color(tmp3, linestr, COLOR_BRWHITE, 0, sizeof(tmp3)),
 						term_color(tmp4, function, COLOR_BRWHITE, 0, sizeof(tmp4)));
 
-					while ((res >= buf->len) && (buf->len <= (BUFSIZ / 2))) {
-						if (!(buf = ast_realloc(buf, (buf->len * 2) + sizeof(*buf)))) {
-							AST_LIST_UNLOCK(&logchannels);
-							return;
-						}
-						buf->len *= 2;
-						pthread_setspecific(log_buf_key, buf);
-						res = snprintf(buf->str, buf->len, ast_opt_timestamp ? "[%s] %s[%ld]: %s:%s %s: " : "%s %s[%ld]: %s:%s %s: ",
-							date,
-							term_color(tmp1, levels[level], colors[level], 0, sizeof(tmp1)),
-							(long)GETTID(),
-							term_color(tmp2, file, COLOR_BRWHITE, 0, sizeof(tmp2)),
-							term_color(tmp3, linestr, COLOR_BRWHITE, 0, sizeof(tmp3)),
-							term_color(tmp4, function, COLOR_BRWHITE, 0, sizeof(tmp4)));
-					}
 					ast_console_puts_mutable(buf->str);
 					
 					va_start(ap, fmt);
-					res = vsnprintf(buf->str, buf->len, fmt, ap);
-					while ((res >= buf->len) && (buf->len <= (BUFSIZ / 2))) {
-						if (!(buf = ast_realloc(buf, (buf->len * 2) + sizeof(*buf)))) {
-							AST_LIST_UNLOCK(&logchannels);
-							va_end(ap);
-							return;
-						}
-						buf->len *= 2;
-						pthread_setspecific(log_buf_key, buf);
+					while ((res = ast_dynamic_str_threadbuild_va(&buf, BUFSIZ, &log_buf, fmt, ap)) == AST_DYNSTR_BUILD_RETRY) {
 						va_end(ap);
 						va_start(ap, fmt);
-						res = vsnprintf(buf->str, buf->len, fmt, ap);
 					}
 					va_end(ap);
-					ast_console_puts_mutable(buf->str);
+					if (res != AST_DYNSTR_BUILD_FAILED)
+						ast_console_puts_mutable(buf->str);
 				}
 			/* File channels */
 			} else if ((chan->logmask & (1 << level)) && (chan->fileptr)) {
 				int res;
-				res = snprintf(buf->str, buf->len, ast_opt_timestamp ? "[%s] %s[%ld]: " : "%s %s[%ld] %s: ", date,
-					levels[level], (long)GETTID(), file);
-				while ((res >= buf->len) && (buf->len <= (BUFSIZ / 2))) {
-					if (!(buf = ast_realloc(buf, (buf->len * 2) + sizeof(*buf)))) {
-						AST_LIST_UNLOCK(&logchannels);
-						return;
-					}
-					buf->len *= 2;
-					pthread_setspecific(log_buf_key, buf);
-					res = snprintf(buf->str, buf->len, ast_opt_timestamp ? "[%s] %s[%ld]: " : "%s %s[%ld] %s: ", date,
-						levels[level], (long)GETTID(), file);
-				}
+				ast_dynamic_str_threadbuild(&buf, BUFSIZ, &log_buf, 
+					ast_opt_timestamp ? "[%s] %s[%ld]: " : "%s %s[%ld] %s: ",
+					date, levels[level], (long)GETTID(), file);
 				res = fprintf(chan->fileptr, "%s", buf->str);
 				if (res <= 0 && !ast_strlen_zero(buf->str)) {	/* Error, no characters printed */
 					fprintf(stderr,"**** Asterisk Logging Error: ***********\n");
@@ -824,25 +777,19 @@
 					manager_event(EVENT_FLAG_SYSTEM, "LogChannel", "Channel: %s\r\nEnabled: No\r\nReason: %d - %s\r\n", chan->filename, errno, strerror(errno));
 					chan->disabled = 1;	
 				} else {
+					int res;
 					/* No error message, continue printing */
 					va_start(ap, fmt);
-					vsnprintf(buf->str, buf->len, fmt, ap);
-					while ((res >= buf->len) && (buf->len <= (BUFSIZ / 2))) {
-						if (!(buf = ast_realloc(buf, (buf->len * 2) + sizeof(*buf)))) {
-							AST_LIST_UNLOCK(&logchannels);
-							va_end(ap);
-							return;
-						}
-						buf->len *= 2;
-						pthread_setspecific(log_buf_key, buf);
+					while ((res = ast_dynamic_str_threadbuild_va(&buf, BUFSIZ, &log_buf, fmt, ap)) == AST_DYNSTR_BUILD_RETRY) {
 						va_end(ap);
 						va_start(ap, fmt);
-						res = vsnprintf(buf->str, buf->len, fmt, ap);
 					}
 					va_end(ap);
-					term_strip(buf->str, buf->str, buf->len);
-					fputs(buf->str, chan->fileptr);
-					fflush(chan->fileptr);
+					if (res != AST_DYNSTR_BUILD_FAILED) {
+						term_strip(buf->str, buf->str, buf->len);
+						fputs(buf->str, chan->fileptr);
+						fflush(chan->fileptr);
+					}
 				}
 			}
 		}
@@ -854,21 +801,13 @@
 		if (level != __LOG_VERBOSE) {
 			int res;
 			va_start(ap, fmt);
-			res = vsnprintf(buf->str, buf->len, fmt, ap);
-			while ((res >= buf->len) && (buf->len <= (BUFSIZ / 2))) {
-				if (!(buf = ast_realloc(buf, (buf->len * 2) + sizeof(*buf)))) {
-					AST_LIST_UNLOCK(&logchannels);
-					va_end(ap);
-					return;
-				}
-				buf->len *= 2;
-				pthread_setspecific(log_buf_key, buf);
+			while ((res = ast_dynamic_str_threadbuild_va(&buf, BUFSIZ, &log_buf, fmt, ap)) == AST_DYNSTR_BUILD_RETRY) {
 				va_end(ap);
 				va_start(ap, fmt);
-				res = vsnprintf(buf->str, buf->len, fmt, ap);
 			}
 			va_end(ap);
-			fputs(buf->str, stdout);
+			if (res != AST_DYNSTR_BUILD_FAILED)
+				fputs(buf->str, stdout);
 		}
 	}
 
@@ -928,6 +867,9 @@
 	}
 	va_end(ap);
 
+	if (res == AST_DYNSTR_BUILD_FAILED)
+		return;
+
 	AST_LIST_LOCK(&verbosers);
 	AST_LIST_TRAVERSE(&verbosers, v, list)
 		v->verboser(buf->str);



More information about the asterisk-commits mailing list