From ebeb60900fbab569ed14f710a0a1abb1637ec792 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Fri, 25 Feb 2011 23:08:53 -0600 Subject: strbuf: add strbuf_vaddf In a variable-args function, the code for writing into a strbuf is non-trivial. We ended up cutting and pasting it in several places because there was no vprintf-style function for strbufs (which in turn was held up by a lack of va_copy). Now that we have a fallback va_copy, we can add strbuf_vaddf, the strbuf equivalent of vsprintf. And we can clean up the cut and paste mess. Signed-off-by: Jeff King Improved-by: Christian Couder Signed-off-by: Jonathan Nieder Signed-off-by: Junio C Hamano --- trace.c | 32 ++++++-------------------------- 1 file changed, 6 insertions(+), 26 deletions(-) (limited to 'trace.c') diff --git a/trace.c b/trace.c index 35d388dce..eda3f6d72 100644 --- a/trace.c +++ b/trace.c @@ -64,28 +64,18 @@ static const char err_msg[] = "Could not trace into fd given by " void trace_printf(const char *fmt, ...) { - struct strbuf buf; + struct strbuf buf = STRBUF_INIT; va_list ap; - int fd, len, need_close = 0; + int fd, need_close = 0; fd = get_trace_fd(&need_close); if (!fd) return; set_try_to_free_routine(NULL); /* is never reset */ - strbuf_init(&buf, 64); va_start(ap, fmt); - len = vsnprintf(buf.buf, strbuf_avail(&buf), fmt, ap); + strbuf_vaddf(&buf, fmt, ap); va_end(ap); - if (len >= strbuf_avail(&buf)) { - strbuf_grow(&buf, len - strbuf_avail(&buf) + 128); - va_start(ap, fmt); - len = vsnprintf(buf.buf, strbuf_avail(&buf), fmt, ap); - va_end(ap); - if (len >= strbuf_avail(&buf)) - die("broken vsnprintf"); - } - strbuf_setlen(&buf, len); write_or_whine_pipe(fd, buf.buf, buf.len, err_msg); strbuf_release(&buf); @@ -96,28 +86,18 @@ void trace_printf(const char *fmt, ...) void trace_argv_printf(const char **argv, const char *fmt, ...) { - struct strbuf buf; + struct strbuf buf = STRBUF_INIT; va_list ap; - int fd, len, need_close = 0; + int fd, need_close = 0; fd = get_trace_fd(&need_close); if (!fd) return; set_try_to_free_routine(NULL); /* is never reset */ - strbuf_init(&buf, 64); va_start(ap, fmt); - len = vsnprintf(buf.buf, strbuf_avail(&buf), fmt, ap); + strbuf_vaddf(&buf, fmt, ap); va_end(ap); - if (len >= strbuf_avail(&buf)) { - strbuf_grow(&buf, len - strbuf_avail(&buf) + 128); - va_start(ap, fmt); - len = vsnprintf(buf.buf, strbuf_avail(&buf), fmt, ap); - va_end(ap); - if (len >= strbuf_avail(&buf)) - die("broken vsnprintf"); - } - strbuf_setlen(&buf, len); sq_quote_argv(&buf, argv, 0); strbuf_addch(&buf, '\n'); -- cgit v1.2.1