To be able to add a common prefix or suffix to all trace output (e.g. a timestamp or file:line of the caller), factor out common setup and cleanup tasks of the trace* functions.
Some unit-tests use trace output to verify internal state, and variable output such as timestamps and line numbers are not useful there. Disable additional trace output if GIT_TRACE_BARE is set. When adding a common prefix, it makes sense that the output of each trace call starts on a new line. Add '\n' in case the caller forgot. Note that this explicitly limits trace output to line-by-line, it is no longer possible to trace-print just part of a line. Until now, this was just an implicit assumption (trace-printing part of a line worked, but messed up the trace file if multiple threads or processes were involved). Thread-safety / inter-process-safety is also the reason why we need to do the prefixing and suffixing in memory rather than issuing multiple write() calls. Write_or_whine_pipe() / xwrite() is atomic unless the size exceeds MAX_IO_SIZE (8MB, see wrapper.c). In case of trace_strbuf, this costs an additional string copy (which should be irrelevant for performance in light of actual file IO). While we're at it, rename trace_strbuf's 'buf' argument, which suggests that the function is modifying the buffer. Trace_strbuf() currently is the only trace API that can print arbitrary binary data (without barfing on '%' or stopping at '\0'), so 'data' seems more appropriate. Signed-off-by: Karsten Blees <bl...@dcon.de> --- t/t1510-repo-setup.sh | 2 +- t/t5503-tagfollow.sh | 8 ++++---- trace.c | 53 ++++++++++++++++++++++++++++++++++++++++----------- trace.h | 2 +- 4 files changed, 48 insertions(+), 17 deletions(-) diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh index e1b2a99..8db8d68 100755 --- a/t/t1510-repo-setup.sh +++ b/t/t1510-repo-setup.sh @@ -57,7 +57,7 @@ test_repo () { export GIT_WORK_TREE fi && rm -f trace && - GIT_TRACE_SETUP="$(pwd)/trace" git symbolic-ref HEAD >/dev/null && + GIT_TRACE_BARE=1 GIT_TRACE_SETUP="$(pwd)/trace" git symbolic-ref HEAD >/dev/null && grep '^setup: ' trace >result && test_cmp expected result ) diff --git a/t/t5503-tagfollow.sh b/t/t5503-tagfollow.sh index f30c038..dc10143 100755 --- a/t/t5503-tagfollow.sh +++ b/t/t5503-tagfollow.sh @@ -56,7 +56,7 @@ test_expect_success 'fetch A (new commit : 1 connection)' ' rm -f $U && ( cd cloned && - GIT_TRACE_PACKET=$UPATH git fetch && + GIT_TRACE_BARE=1 GIT_TRACE_PACKET=$UPATH git fetch && test $A = $(git rev-parse --verify origin/master) ) && get_needs $U >actual && @@ -86,7 +86,7 @@ test_expect_success 'fetch C, T (new branch, tag : 1 connection)' ' rm -f $U && ( cd cloned && - GIT_TRACE_PACKET=$UPATH git fetch && + GIT_TRACE_BARE=1 GIT_TRACE_PACKET=$UPATH git fetch && test $C = $(git rev-parse --verify origin/cat) && test $T = $(git rev-parse --verify tag1) && test $A = $(git rev-parse --verify tag1^0) @@ -122,7 +122,7 @@ test_expect_success 'fetch B, S (commit and tag : 1 connection)' ' rm -f $U && ( cd cloned && - GIT_TRACE_PACKET=$UPATH git fetch && + GIT_TRACE_BARE=1 GIT_TRACE_PACKET=$UPATH git fetch && test $B = $(git rev-parse --verify origin/master) && test $B = $(git rev-parse --verify tag2^0) && test $S = $(git rev-parse --verify tag2) @@ -146,7 +146,7 @@ test_expect_success 'new clone fetch master and tags' ' cd clone2 && git init && git remote add origin .. && - GIT_TRACE_PACKET=$UPATH git fetch && + GIT_TRACE_BARE=1 GIT_TRACE_PACKET=$UPATH git fetch && test $B = $(git rev-parse --verify origin/master) && test $S = $(git rev-parse --verify tag2) && test $B = $(git rev-parse --verify tag2^0) && diff --git a/trace.c b/trace.c index b7ca51b..9fa406e 100644 --- a/trace.c +++ b/trace.c @@ -77,17 +77,45 @@ static void do_trace_print(const char *key, const struct strbuf *buf) close(fd); } +static int trace_bare = -1; + +static int prepare_trace_line(const char *key, struct strbuf *buf) +{ + if (!trace_want(key)) + return 0; + + set_try_to_free_routine(NULL); /* is never reset */ + + /* unit tests may want to disable additional trace output */ + if (trace_bare < 0) + trace_bare = trace_want("GIT_TRACE_BARE"); + if (trace_bare) + return 1; + + /* add line prefix here */ + + return 1; +} + +static void print_trace_line(const char *key, struct strbuf *buf) +{ + /* append newline if missing */ + if (buf->len && buf->buf[buf->len - 1] != '\n') + strbuf_addch(buf, '\n'); + + do_trace_print(key, buf); + strbuf_release(buf); +} + static void trace_vprintf(const char *key, const char *format, va_list ap) { struct strbuf buf = STRBUF_INIT; - if (!trace_want(key)) + if (!prepare_trace_line(key, &buf)) return; - set_try_to_free_routine(NULL); /* is never reset */ strbuf_vaddf(&buf, format, ap); - do_trace_print(key, &buf); - strbuf_release(&buf); + print_trace_line(key, &buf); } void trace_printf_key(const char *key, const char *format, ...) @@ -106,9 +134,15 @@ void trace_printf(const char *format, ...) va_end(ap); } -void trace_strbuf(const char *key, const struct strbuf *buf) +void trace_strbuf(const char *key, const struct strbuf *data) { - do_trace_print(key, buf); + struct strbuf buf = STRBUF_INIT; + + if (!prepare_trace_line(key, &buf)) + return; + + strbuf_addbuf(&buf, data); + print_trace_line(key, &buf); } void trace_argv_printf(const char **argv, const char *format, ...) @@ -116,18 +150,15 @@ void trace_argv_printf(const char **argv, const char *format, ...) struct strbuf buf = STRBUF_INIT; va_list ap; - if (!trace_want("GIT_TRACE")) + if (!prepare_trace_line("GIT_TRACE", &buf)) return; - set_try_to_free_routine(NULL); /* is never reset */ va_start(ap, format); strbuf_vaddf(&buf, format, ap); va_end(ap); sq_quote_argv(&buf, argv, 0); - strbuf_addch(&buf, '\n'); - do_trace_print("GIT_TRACE", &buf); - strbuf_release(&buf); + print_trace_line("GIT_TRACE", &buf); } static const char *quote_crnl(const char *path) diff --git a/trace.h b/trace.h index 8fea50b..e03db2f 100644 --- a/trace.h +++ b/trace.h @@ -12,6 +12,6 @@ extern void trace_repo_setup(const char *prefix); extern int trace_want(const char *key); __attribute__((format (printf, 2, 3))) extern void trace_printf_key(const char *key, const char *format, ...); -extern void trace_strbuf(const char *key, const struct strbuf *buf); +extern void trace_strbuf(const char *key, const struct strbuf *data); #endif /* TRACE_H */ -- 2.0.0.402.g13b8b25 -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html