From: Francois Gouget Subject: [3/6] testbot/testagentd: Add a function to simplify formatting a message. Message-Id: Date: Mon, 14 Apr 2014 15:45:36 +0200 (CEST) --- This splits off an aspect of vset_status_msg() and makes it reusable but does not yet use of it elsewhere. This patch is independent from all other patches in the series, except 4/6 which depends on it. testbot/src/testagentd/testagentd.c | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/testbot/src/testagentd/testagentd.c b/testbot/src/testagentd/testagentd.c index 230683a..d5d8dfc 100644 --- a/testbot/src/testagentd/testagentd.c +++ b/testbot/src/testagentd/testagentd.c @@ -129,31 +129,38 @@ const char* status_names[] = {"ok:", "error:", "fatal:"}; /* If true, then the current connection is in a broken state */ static int broken = 0; -/* This is a message which indicates the reason for the status */ -static char* status_msg = NULL; -static unsigned status_size = 0; -static void vset_status_msg(const char* format, va_list valist) + +static char* vformat_msg(char** buf, unsigned* size, const char* format, va_list valist) { - int len; + unsigned len; va_list args; len = 1; do { - if (len >= status_size) + if (len >= *size) { /* len does not count the trailing '\0'. So add 1 and round up * to the next 16 bytes multiple. */ - status_size = (len + 1 + 0xf) & ~0xf; - status_msg = realloc(status_msg, status_size); + *size = (len + 1 + 0xf) & ~0xf; + *buf = realloc(*buf, *size); } va_copy(args, valist); - len = vsnprintf(status_msg, status_size, format, args); + len = vsnprintf(*buf, *size, format, args); va_end(args); if (len < 0) - len = status_size * 1.1; + len = *size * 1.1; } - while (len >= status_size); + while (len >= *size); + return *buf; +} + +/* This is a message which indicates the reason for the status */ +static char* status_msg = NULL; +static unsigned status_size = 0; +static void vset_status_msg(const char* format, va_list valist) +{ + vformat_msg(&status_msg, &status_size, format, valist); if (opt_debug || status != ST_OK) fprintf(stderr, "%s%s: %s\n", status_names[status], rpc_name(rpcid), status_msg); } -- 1.9.1