[asterisk-commits] russell: branch file/logqueue r42011 - /team/file/logqueue/main/logger.c

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Mon Sep 4 19:23:42 MST 2006


Author: russell
Date: Mon Sep  4 21:23:41 2006
New Revision: 42011

URL: http://svn.digium.com/view/asterisk?rev=42011&view=rev
Log:
combine the allocation of the logmsg struct and the space for the message itself
into a single allocation

Modified:
    team/file/logqueue/main/logger.c

Modified: team/file/logqueue/main/logger.c
URL: http://svn.digium.com/view/asterisk/team/file/logqueue/main/logger.c?rev=42011&r1=42010&r2=42011&view=diff
==============================================================================
--- team/file/logqueue/main/logger.c (original)
+++ team/file/logqueue/main/logger.c Mon Sep  4 21:23:41 2006
@@ -124,8 +124,8 @@
 	const char *file;
 	int line;
 	const char *function;
-	char *str;
 	AST_LIST_ENTRY(logmsg) list;
+	char str[0];
 };
 
 static AST_LIST_HEAD_STATIC(logmsgs, logmsg);
@@ -750,7 +750,6 @@
 				logger_print_verbose(msg);
 
 			/* Free the data since we are done */
-			free(msg->str);
 			free(msg);
 		}
 	}
@@ -874,9 +873,21 @@
 	if ((level == __LOG_DEBUG) && !ast_strlen_zero(debug_filename) && strcasecmp(debug_filename, file))
 		return;
 
+	/* Build string */
+	va_start(ap, fmt);
+	res = ast_dynamic_str_thread_set_va(&buf, 0, &verbose_buf, 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))))
-		return;
+	if (!(logmsg = ast_calloc(1, sizeof(*logmsg) + res + 1)))
+		return;
+	
+	/* Copy string over */
+	strcpy(logmsg->str, ast_strdup(buf->str));
 
 	/* Set type to be normal */
 	logmsg->type = LOGMSG_NORMAL;
@@ -891,20 +902,6 @@
 	logmsg->file = file;
 	logmsg->line = line;
 	logmsg->function = function;
-
-	/* Build string */
-	va_start(ap, fmt);
-	res = ast_dynamic_str_thread_set_va(&buf, 0, &verbose_buf, fmt, ap);
-	va_end(ap);
-
-	/* If the build failed, then abort and free this structure */
-	if (res == AST_DYNSTR_BUILD_FAILED) {
-		free(logmsg);
-		return;
-	}
-
-	/* Copy string over */
-	logmsg->str = ast_strdup(buf->str);
 
 	/* Add it to the list at the tail end, and prod the logging thread */
 	AST_LIST_LOCK(&logmsgs);
@@ -954,9 +951,23 @@
 	if (!(buf = ast_dynamic_str_thread_get(&verbose_buf, VERBOSE_BUF_INIT_SIZE)))
 		return;
 
-	if (!(logmsg = ast_calloc(1, sizeof(*logmsg))))
-		return;
-
+	/* Build string */
+	va_start(ap, fmt);
+	res = ast_dynamic_str_thread_set_va(&buf, 0, &verbose_buf, 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;
+	
 	if (ast_opt_timestamp) {
 		time_t t;
 		struct tm tm;
@@ -971,24 +982,6 @@
 		fmt = datefmt;
 	}
 
-	/* Set type */
-	logmsg->type = LOGMSG_VERBOSE;
-	
-	/* Build string */
-	va_start(ap, fmt);
-	res = ast_dynamic_str_thread_set_va(&buf, 0, &verbose_buf, fmt, ap);
-	va_end(ap);
-
-	/* If the build failed then we can drop this allocated message */
-	if (res == AST_DYNSTR_BUILD_FAILED) {
-		free(logmsg);
-		return;
-	}
-
-	/* Duplicate it in the logging message */
-	logmsg->str = ast_strdup(buf->str);
-	ast_log(LOG_VERBOSE, "%s", buf->str);
-
 	/* Add to the list and poke the thread */
 	AST_LIST_LOCK(&logmsgs);
 	AST_LIST_INSERT_TAIL(&logmsgs, logmsg, list);



More information about the asterisk-commits mailing list