core-tutorial.txt: Fix showing the current behaviour.
[gitweb.git] / trace.c
diff --git a/trace.c b/trace.c
index 91548a56eceb56cfcb0521fc0313afe51dff0353..4713f9165c54405d51e81c3e90847120ee907e5d 100644 (file)
--- a/trace.c
+++ b/trace.c
@@ -37,7 +37,7 @@ static int get_trace_fd(int *need_close)
                return STDERR_FILENO;
        if (strlen(trace) == 1 && isdigit(*trace))
                return atoi(trace);
-       if (*trace == '/') {
+       if (is_absolute_path(trace)) {
                int fd = open(trace, O_WRONLY | O_APPEND | O_CREAT, 0666);
                if (fd == -1) {
                        fprintf(stderr,
@@ -64,7 +64,7 @@ static const char err_msg[] = "Could not trace into fd given by "
 
 void trace_printf(const char *fmt, ...)
 {
-       char buf[8192];
+       struct strbuf buf;
        va_list ap;
        int fd, len, need_close = 0;
 
@@ -72,44 +72,55 @@ void trace_printf(const char *fmt, ...)
        if (!fd)
                return;
 
+       strbuf_init(&buf, 64);
        va_start(ap, fmt);
-       len = vsnprintf(buf, sizeof(buf), fmt, ap);
+       len = vsnprintf(buf.buf, strbuf_avail(&buf), fmt, ap);
        va_end(ap);
-       if (len >= sizeof(buf))
-               die("unreasonnable trace length");
-       write_or_whine_pipe(fd, buf, len, err_msg);
+       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);
 
        if (need_close)
                close(fd);
 }
 
-void trace_argv_printf(const char **argv, int count, const char *fmt, ...)
+void trace_argv_printf(const char **argv, const char *fmt, ...)
 {
-       char buf[8192];
+       struct strbuf buf;
        va_list ap;
-       char *argv_str;
-       size_t argv_len;
        int fd, len, need_close = 0;
 
        fd = get_trace_fd(&need_close);
        if (!fd)
                return;
 
+       strbuf_init(&buf, 64);
        va_start(ap, fmt);
-       len = vsnprintf(buf, sizeof(buf), fmt, ap);
+       len = vsnprintf(buf.buf, strbuf_avail(&buf), fmt, ap);
        va_end(ap);
-       if (len >= sizeof(buf))
-               die("unreasonnable trace length");
-
-       /* Get the argv string. */
-       argv_str = sq_quote_argv(argv, count);
-       argv_len = strlen(argv_str);
-
-       write_or_whine_pipe(fd, buf, len, err_msg);
-       write_or_whine_pipe(fd, argv_str, argv_len, err_msg);
-       write_or_whine_pipe(fd, "\n", 1, err_msg);
+       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);
 
-       free(argv_str);
+       sq_quote_argv(&buf, argv, 0);
+       strbuf_addch(&buf, '\n');
+       write_or_whine_pipe(fd, buf.buf, buf.len, err_msg);
+       strbuf_release(&buf);
 
        if (need_close)
                close(fd);