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

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Sat Aug 5 04:06:37 MST 2006


Author: russell
Date: Sat Aug  5 06:06:37 2006
New Revision: 39013

URL: http://svn.digium.com/view/asterisk?rev=39013&view=rev
Log:
- some API name changes
- add append functions so that the dynamic strings can be appended to instead of
  just set
- convert manager_event() to use a thread local dynamic string buffer as the
  workspace for preparing the text of an event to remove the length limitation
  on the length of the body of the event

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

Modified: team/russell/ast_verbose_threadstorage/cli.c
URL: http://svn.digium.com/view/asterisk/team/russell/ast_verbose_threadstorage/cli.c?rev=39013&r1=39012&r2=39013&view=diff
==============================================================================
--- team/russell/ast_verbose_threadstorage/cli.c (original)
+++ team/russell/ast_verbose_threadstorage/cli.c Sat Aug  5 06:06:37 2006
@@ -63,14 +63,14 @@
 	struct ast_dynamic_str *buf;
 	va_list ap;
 
-	if (!(buf = ast_dynamic_str_threadget(&ast_cli_buf, AST_CLI_MAXSTRLEN)))
+	if (!(buf = ast_dynamic_str_thread_get(&ast_cli_buf, AST_CLI_MAXSTRLEN)))
 		return;
 
 	va_start(ap, fmt);
-	res = ast_dynamic_str_thread_printf_va(&buf, 0, &ast_cli_buf, fmt, ap);
+	res = ast_dynamic_str_thread_set_va(&buf, 0, &ast_cli_buf, fmt, ap);
 	va_end(ap);
 
-	if (res != AST_DYNSTR_PRINTF_FAILED)
+	if (res != AST_DYNSTR_SET_FAILED)
 		ast_carefulwrite(fd, buf->str, strlen(buf->str), 100);
 }
 

Modified: team/russell/ast_verbose_threadstorage/include/asterisk/threadstorage.h
URL: http://svn.digium.com/view/asterisk/team/russell/ast_verbose_threadstorage/include/asterisk/threadstorage.h?rev=39013&r1=39012&r2=39013&view=diff
==============================================================================
--- team/russell/ast_verbose_threadstorage/include/asterisk/threadstorage.h (original)
+++ team/russell/ast_verbose_threadstorage/include/asterisk/threadstorage.h Sat Aug  5 06:06:37 2006
@@ -58,15 +58,18 @@
  * AST_THREADSTORAGE(my_buf, my_buf_init);
  * \endcode
  */
-#define AST_THREADSTORAGE(name, name_init)         \
-static void name_init(void);                       \
-static struct ast_threadstorage name = {           \
-	.once = PTHREAD_ONCE_INIT,                 \
-	.key_init = name_init,                     \
-};                                                 \
-static void name_init(void)                        \
-{                                                  \
-	pthread_key_create(&(name).key, ast_free); \
+#define AST_THREADSTORAGE(name, name_init) \
+	AST_THREADSTORAGE_CUSTOM(name, name_init, ast_free) 
+
+#define AST_THREADSTORAGE_CUSTOM(name, name_init, cleanup)  \
+static void name_init(void);                                \
+static struct ast_threadstorage name = {                    \
+	.once = PTHREAD_ONCE_INIT,                          \
+	.key_init = name_init,                              \
+};                                                          \
+static void name_init(void)                                 \
+{                                                           \
+	pthread_key_create(&(name).key, cleanup);           \
 }
 
 /*!
@@ -175,14 +178,14 @@
  * {
  *      struct ast_dynamic_str *buf;
  *
- *      if (!(buf = ast_dynamic_str_threadget(&my_str, MY_STR_INIT_SIZE)))
+ *      if (!(buf = ast_dynamic_str_thread_get(&my_str, MY_STR_INIT_SIZE)))
  *           return;
  *      ...
  * }
  * \endcode
  */
 AST_INLINE_API(
-struct ast_dynamic_str *ast_dynamic_str_threadget(struct ast_threadstorage *ts,
+struct ast_dynamic_str *ast_dynamic_str_thread_get(struct ast_threadstorage *ts,
 	size_t init_len),
 {
 	struct ast_dynamic_str *buf;
@@ -198,24 +201,24 @@
 )
 
 /*!
- * \brief Error codes from __ast_dynamic_str_thread_printf_va()
+ * \brief Error codes from __ast_dynamic_str_thread_set_va()
  */
 enum {
 	/*! An error has occured and the contents of the dynamic string
 	 *  are undefined */
-	AST_DYNSTR_PRINTF_FAILED = -1,
+	AST_DYNSTR_SET_FAILED = -1,
 	/*! The buffer size for the dynamic string had to be increased, and
-	 *  __ast_dynamic_str_thread_printf_va() needs to be called again after
+	 *  __ast_dynamic_str_thread_set_va() needs to be called again after
 	 *  a va_end() and va_start().
 	 */
-	AST_DYNSTR_PRINTF_RETRY = -2
+	AST_DYNSTR_SET_RETRY = -2
 };
 
 /*!
- * \brief Build a thread locally stored dynamic string from a va_list
+ * \brief Set a thread locally stored dynamic string from a va_list
  *
  * \arg buf This is the address of a pointer to an ast_dynamic_str which should
- *      have been retrieved using ast_dynamic_str_threadget.  It will need to
+ *      have been retrieved using ast_dynamic_str_thread_get.  It will need to
  *      be updated in the case that the buffer has to be reallocated to
  *      accomodate a longer string than what it currently has space for.
  * \arg max_len This is the maximum length to allow the string buffer to grow
@@ -224,7 +227,7 @@
  *      the AST_THREADSTORAGE macro.  If declared with 
  *      AST_THREADSTORAGE(my_buf, my_buf_init), then this argument would be 
  *      (&my_buf).
- * \arg fmt This is the format string
+ * \arg fmt This is the format string (printf style)
  * \arg ap This is the va_list
  *
  * \return The return value of this function is the same as that of the printf
@@ -240,11 +243,11 @@
  *      struct ast_dynamic_str *buf;
  *      va_list ap;
  *
- *      if (!(buf = ast_dynamic_str_threadget(&my_str, MY_STR_INIT_SIZE)))
+ *      if (!(buf = ast_dynamic_str_thread_get(&my_str, MY_STR_INIT_SIZE)))
  *           return;
  *      ...
  *      va_start(fmt, ap);
- *      ast_dynamic_str_thread_printf_va(&buf, 0, &my_str, fmt, ap);
+ *      ast_dynamic_str_thread_set_va(&buf, 0, &my_str, fmt, ap);
  *      va_end(ap);
  * 
  *      printf("This is the string we just built: %s\n", buf->str);
@@ -252,30 +255,43 @@
  * }
  * \endcode
  */
-#define ast_dynamic_str_thread_printf_va(buf, max_len, ts, fmt, ap)              \
+#define ast_dynamic_str_thread_set_va(buf, max_len, ts, fmt, ap)                 \
 	({                                                                       \
 		int __res;                                                       \
-		while ((__res = __ast_dynamic_str_thread_printf_va(buf, max_len, \
-			ts, fmt, ap)) == AST_DYNSTR_PRINTF_RETRY) {              \
+		while ((__res = __ast_dynamic_str_thread_set_va(buf, max_len,    \
+			ts, 0, fmt, ap)) == AST_DYNSTR_SET_RETRY) {              \
 			va_end(ap);                                              \
 			va_start(ap, fmt);                                       \
 		}                                                                \
 		(__res);                                                         \
 	})
 
-/*!
- * \brief Core functionality of ast_dynamic_str_thread_printf_va
+#define ast_dynamic_str_thread_append_va(buf, max_len, ts, fmt, ap)               \
+	({                                                                       \
+		int __res;                                                       \
+		while ((__res = __ast_dynamic_str_thread_set_va(buf, max_len,    \
+			ts, 1, fmt, ap)) == AST_DYNSTR_SET_RETRY) {              \
+			va_end(ap);                                              \
+			va_start(ap, fmt);                                       \
+		}                                                                \
+		(__res);                                                         \
+	})
+/*!
+ * \brief Core functionality of ast_dynamic_str_thread_set_va
  *
  * The arguments to this function are the same as those described for
- * ast_dynamic_str_thread_printf_va.
- */
-AST_INLINE_API(
-int __ast_dynamic_str_thread_printf_va(struct ast_dynamic_str **buf, size_t max_len,
-	struct ast_threadstorage *ts, const char *fmt, va_list ap),
+ * ast_dynamic_str_thread_set_va except for an addition argument, append.
+ * If append is non-zero, this will append to the current string instead of
+ * writing over it.
+ */
+AST_INLINE_API(
+int __ast_dynamic_str_thread_set_va(struct ast_dynamic_str **buf, size_t max_len,
+	struct ast_threadstorage *ts, int append, const char *fmt, va_list ap),
 {
 	int res;
-
-	res = vsnprintf((*buf)->str, (*buf)->len, fmt, ap);
+	int offset = append ? strlen((*buf)->str) : 0;
+
+	res = vsnprintf((*buf)->str + offset, (*buf)->len - offset, fmt, ap);
 
 	/* Check to see if there was not enough space in the string buffer to prepare
 	 * the string.  Also, if a maximum length is present, make sure the current
@@ -291,14 +307,14 @@
 			(*buf)->len = res + 1;
 
 		if (!(*buf = ast_realloc(*buf, (*buf)->len + sizeof(*(*buf)))))
-			return AST_DYNSTR_PRINTF_FAILED;
+			return AST_DYNSTR_SET_FAILED;
 
 		if (ts)
 			pthread_setspecific(ts->key, *buf);
 
 		/* va_end() and va_start() must be done before calling
 		 * vsnprintf() again. */
-		return AST_DYNSTR_PRINTF_RETRY;
+		return AST_DYNSTR_SET_RETRY;
 	}
 
 	return res;
@@ -306,38 +322,10 @@
 )
 
 /*!
- * \brief Build a dynamic string
- *
- * \arg buf This is the address of a pointer to an ast_dynamic_str.  It will
- *      need to be updated in the case that the buffer has to be reallocated to
- *      accomodate a longer string than what it currently has space for.
- * \arg max_len This is the maximum length to allow the string buffer to grow
- *      to.  If this is set to 0, then there is no maximum length.
- *
- * \return The return value of this function is the same as that of the printf
- *         family of functions.
- */
-AST_INLINE_API(
-int __attribute__ ((format (printf, 3, 4))) ast_dynamic_str_printf(
-	struct ast_dynamic_str **buf, size_t max_len,
-	const char *fmt, ...),
-{
-	int res;
-	va_list ap;
-	
-	va_start(ap, fmt);
-	res = ast_dynamic_str_thread_printf_va(buf, max_len, NULL, fmt, ap);
-	va_end(ap);
-
-	return res;
-}
-)
-
-/*!
- * \brief Build a thread locally stored dynamic string from a va_list
+ * \brief Set a thread locally stored dynamic string using variable arguments
  *
  * \arg buf This is the address of a pointer to an ast_dynamic_str which should
- *      have been retrieved using ast_dynamic_str_threadget.  It will need to
+ *      have been retrieved using ast_dynamic_str_thread_get.  It will need to
  *      be updated in the case that the buffer has to be reallocated to
  *      accomodate a longer string than what it currently has space for.
  * \arg max_len This is the maximum length to allow the string buffer to grow
@@ -346,7 +334,7 @@
  *      the AST_THREADSTORAGE macro.  If declared with 
  *      AST_THREADSTORAGE(my_buf, my_buf_init), then this argument would be 
  *      (&my_buf).
- * \arg fmt This is the format string
+ * \arg fmt This is the format string (printf style)
  *
  * \return The return value of this function is the same as that of the printf
  *         family of functions.
@@ -361,10 +349,10 @@
  *      struct ast_dynamic_str *buf;
  *      va_list ap;
  *
- *      if (!(buf = ast_dynamic_str_threadget(&my_str, MY_STR_INIT_SIZE)))
+ *      if (!(buf = ast_dynamic_str_thread_get(&my_str, MY_STR_INIT_SIZE)))
  *           return;
  *      ...
- *      ast_dynamic_str_thread_printf(&buf, 0, &my_str, "arg1: %d  arg2: %d\n",
+ *      ast_dynamic_str_thread_set(&buf, 0, &my_str, "arg1: %d  arg2: %d\n",
  *           arg1, arg2);
  * 
  *      printf("This is the string we just built: %s\n", buf->str);
@@ -373,7 +361,7 @@
  * \endcode
  */
 AST_INLINE_API(
-int __attribute__ ((format (printf, 4, 5))) ast_dynamic_str_thread_printf(
+int __attribute__ ((format (printf, 4, 5))) ast_dynamic_str_thread_set(
 	struct ast_dynamic_str **buf, size_t max_len, 
 	struct ast_threadstorage *ts, const char *fmt, ...),
 {
@@ -381,11 +369,71 @@
 	va_list ap;
 
 	va_start(ap, fmt);
-	res = ast_dynamic_str_thread_printf_va(buf, max_len, ts, fmt, ap);
+	res = ast_dynamic_str_thread_set_va(buf, max_len, ts, fmt, ap);
 	va_end(ap);
 
 	return res;
 }
 )
 
+AST_INLINE_API(
+int __attribute__ ((format (printf, 4, 5))) ast_dynamic_str_thread_append(
+	struct ast_dynamic_str **buf, size_t max_len, 
+	struct ast_threadstorage *ts, const char *fmt, ...),
+{
+	int res;
+	va_list ap;
+
+	va_start(ap, fmt);
+	res = ast_dynamic_str_thread_append_va(buf, max_len, ts, fmt, ap);
+	va_end(ap);
+
+	return res;
+}
+)
+
+/*!
+ * \brief Set a dynamic string
+ *
+ * \arg buf This is the address of a pointer to an ast_dynamic_str.  It will
+ *      need to be updated in the case that the buffer has to be reallocated to
+ *      accomodate a longer string than what it currently has space for.
+ * \arg max_len This is the maximum length to allow the string buffer to grow
+ *      to.  If this is set to 0, then there is no maximum length.
+ *
+ * \return The return value of this function is the same as that of the printf
+ *         family of functions.
+ */
+AST_INLINE_API(
+int __attribute__ ((format (printf, 3, 4))) ast_dynamic_str_set(
+	struct ast_dynamic_str **buf, size_t max_len,
+	const char *fmt, ...),
+{
+	int res;
+	va_list ap;
+	
+	va_start(ap, fmt);
+	res = ast_dynamic_str_thread_set_va(buf, max_len, NULL, fmt, ap);
+	va_end(ap);
+
+	return res;
+}
+)
+
+AST_INLINE_API(
+int __attribute__ ((format (printf, 3, 4))) ast_dynamic_str_append(
+	struct ast_dynamic_str **buf, size_t max_len,
+	const char *fmt, ...),
+{
+	int res;
+	va_list ap;
+	
+	va_start(ap, fmt);
+	res = ast_dynamic_str_thread_append_va(buf, max_len, NULL, fmt, ap);
+	va_end(ap);
+
+	return res;
+}
+)
+
 #endif /* ASTERISK_THREADSTORAGE_H */

Modified: team/russell/ast_verbose_threadstorage/logger.c
URL: http://svn.digium.com/view/asterisk/team/russell/ast_verbose_threadstorage/logger.c?rev=39013&r1=39012&r2=39013&view=diff
==============================================================================
--- team/russell/ast_verbose_threadstorage/logger.c (original)
+++ team/russell/ast_verbose_threadstorage/logger.c Sat Aug  5 06:06:37 2006
@@ -685,7 +685,7 @@
 
 	va_list ap;
 
-	if (!(buf = ast_dynamic_str_threadget(&log_buf, LOG_BUF_INIT_SIZE)))
+	if (!(buf = ast_dynamic_str_thread_get(&log_buf, LOG_BUF_INIT_SIZE)))
 		return;
 
 	/* don't display LOG_DEBUG messages unless option_verbose _or_ option_debug
@@ -740,7 +740,7 @@
 				if (level != __LOG_VERBOSE) {
 					int res;
 					sprintf(linestr, "%d", line);
-					ast_dynamic_str_thread_printf(&buf, BUFSIZ, &log_buf,
+					ast_dynamic_str_thread_set(&buf, BUFSIZ, &log_buf,
 						"[%s] %s[%ld]: %s:%s %s: ",
 						date,
 						term_color(tmp1, levels[level], colors[level], 0, sizeof(tmp1)),
@@ -752,15 +752,15 @@
 					ast_console_puts_mutable(buf->str);
 					
 					va_start(ap, fmt);
-					res = ast_dynamic_str_thread_printf_va(&buf, BUFSIZ, &log_buf, fmt, ap);
+					res = ast_dynamic_str_thread_set_va(&buf, BUFSIZ, &log_buf, fmt, ap);
 					va_end(ap);
-					if (res != AST_DYNSTR_PRINTF_FAILED)
+					if (res != AST_DYNSTR_SET_FAILED)
 						ast_console_puts_mutable(buf->str);
 				}
 			/* File channels */
 			} else if ((chan->logmask & (1 << level)) && (chan->fileptr)) {
 				int res;
-				ast_dynamic_str_thread_printf(&buf, BUFSIZ, &log_buf, 
+				ast_dynamic_str_thread_set(&buf, BUFSIZ, &log_buf, 
 					"[%s] %s[%ld] %s: ",
 					date, levels[level], (long)GETTID(), file);
 				res = fprintf(chan->fileptr, "%s", buf->str);
@@ -776,9 +776,9 @@
 					int res;
 					/* No error message, continue printing */
 					va_start(ap, fmt);
-					ast_dynamic_str_thread_printf_va(&buf, BUFSIZ, &log_buf, fmt, ap);
+					ast_dynamic_str_thread_set_va(&buf, BUFSIZ, &log_buf, fmt, ap);
 					va_end(ap);
-					if (res != AST_DYNSTR_PRINTF_FAILED) {
+					if (res != AST_DYNSTR_SET_FAILED) {
 						term_strip(buf->str, buf->str, buf->len);
 						fputs(buf->str, chan->fileptr);
 						fflush(chan->fileptr);
@@ -794,9 +794,9 @@
 		if (level != __LOG_VERBOSE) {
 			int res;
 			va_start(ap, fmt);
-			res = ast_dynamic_str_thread_printf_va(&buf, BUFSIZ, &log_buf, fmt, ap);
+			res = ast_dynamic_str_thread_set_va(&buf, BUFSIZ, &log_buf, fmt, ap);
 			va_end(ap);
-			if (res != AST_DYNSTR_PRINTF_FAILED)
+			if (res != AST_DYNSTR_SET_FAILED)
 				fputs(buf->str, stdout);
 		}
 	}
@@ -861,14 +861,14 @@
 		fmt = datefmt;
 	}
 
-	if (!(buf = ast_dynamic_str_threadget(&verbose_buf, VERBOSE_BUF_INIT_SIZE)))
+	if (!(buf = ast_dynamic_str_thread_get(&verbose_buf, VERBOSE_BUF_INIT_SIZE)))
 		return;
 
 	va_start(ap, fmt);
-	res = ast_dynamic_str_thread_printf_va(&buf, 0, &verbose_buf, fmt, ap);
+	res = ast_dynamic_str_thread_set_va(&buf, 0, &verbose_buf, fmt, ap);
 	va_end(ap);
 
-	if (res == AST_DYNSTR_PRINTF_FAILED)
+	if (res == AST_DYNSTR_SET_FAILED)
 		return;
 
 	AST_LIST_LOCK(&verbosers);

Modified: team/russell/ast_verbose_threadstorage/manager.c
URL: http://svn.digium.com/view/asterisk/team/russell/ast_verbose_threadstorage/manager.c?rev=39013&r1=39012&r2=39013&view=diff
==============================================================================
--- team/russell/ast_verbose_threadstorage/manager.c (original)
+++ team/russell/ast_verbose_threadstorage/manager.c Sat Aug  5 06:06:37 2006
@@ -66,6 +66,7 @@
 #include "asterisk/acl.h"
 #include "asterisk/utils.h"
 #include "asterisk/http.h"
+#include "asterisk/threadstorage.h"
 
 struct fast_originate_helper {
 	char tech[AST_MAX_MANHEADER_LEN];
@@ -103,6 +104,9 @@
 static int block_sockets = 0;
 static int num_sessions = 0;
 struct eventqent *master_eventq = NULL;
+
+AST_THREADSTORAGE(manager_event_buf, manager_event_buf_init);
+#define MANAGER_EVENT_BUF_INITSIZE   256
 
 static struct permalias {
 	int num;
@@ -2113,33 +2117,37 @@
 {
 	struct mansession *s;
 	char auth[80];
-	char tmp[4096] = "";
-	char *tmp_next = tmp;
-	size_t tmp_left = sizeof(tmp) - 2;
 	va_list ap;
 	struct timeval now;
+	struct ast_dynamic_str *buf;
 
 	/* Abort if there aren't any manager sessions */
 	if (!num_sessions)
 		return 0;
 
-	ast_build_string(&tmp_next, &tmp_left, "Event: %s\r\nPrivilege: %s\r\n",
+	if (!(buf = ast_dynamic_str_thread_get(&manager_event_buf, MANAGER_EVENT_BUF_INITSIZE)))
+		return -1;
+
+	ast_dynamic_str_thread_set(&buf, 0, &manager_event_buf,
+			"Event: %s\r\nPrivilege: %s\r\n",
 			 event, authority_to_str(category, auth, sizeof(auth)));
+
 	if (timestampevents) {
 		now = ast_tvnow();
-		ast_build_string(&tmp_next, &tmp_left, "Timestamp: %ld.%06lu\r\n",
+		ast_dynamic_str_thread_append(&buf, 0, &manager_event_buf,
+				"Timestamp: %ld.%06lu\r\n",
 				 now.tv_sec, (unsigned long) now.tv_usec);
 	}
+
 	va_start(ap, fmt);
-	ast_build_string_va(&tmp_next, &tmp_left, fmt, ap);
+	ast_dynamic_str_thread_append_va(&buf, 0, &manager_event_buf, fmt, ap);
 	va_end(ap);
-	*tmp_next++ = '\r';
-	*tmp_next++ = '\n';
-	*tmp_next = '\0';
+	
+	ast_dynamic_str_thread_append(&buf, 0, &manager_event_buf, "\r\n");	
 	
 	ast_mutex_lock(&sessionlock);
 	/* Append even to master list and wake up any sleeping sessions */
-	append_event(tmp, category);
+	append_event(buf->str, category);
 	for (s = sessions; s; s = s->next) {
 		ast_mutex_lock(&s->__lock);
 		if (s->waiting_thread != AST_PTHREADT_NULL)



More information about the asterisk-commits mailing list