@matheusbsbs / matheus-bagliano / commits / 06796607ef
Fork 0

trace: refactor to support multiple env variables

Right now you turn all tracing off and on with GIT_TRACE. To support new types of tracing without forcing the user to see all of them, we will soon support turning each tracing area on with GIT_TRACE_*. This patch lays the groundwork by providing an interface which does not assume GIT_TRACE. However, we still maintain the trace_printf interface so that existing callers do not need to be refactored. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Feb 24, 2011 at 09:28 UTC 06796607ef557e8913f1797cca3c98ce4844c36c
2 files changed +19 -11
cache.h
+1 -1
@@ -1067,7 +1067,7 @@ extern void alloc_report(void);
1067 /* trace.c */
1068 __attribute__((format (printf, 1, 2)))
1069 extern void trace_printf(const char *format, ...);
1070 -extern void trace_vprintf(const char *format, va_list ap);
1070 +extern void trace_vprintf(const char *key, const char *format, va_list ap);
1071 __attribute__((format (printf, 2, 3)))
1072 extern void trace_argv_printf(const char **argv, const char *format, ...);
1073 extern void trace_repo_setup(const char *prefix);
trace.c
+18 -10
@@ -25,10 +25,10 @@
25 #include "cache.h"
26 #include "quote.h"
27
28 -/* Get a trace file descriptor from GIT_TRACE env variable. */
29 -static int get_trace_fd(int *need_close)
28 +/* Get a trace file descriptor from "key" env variable. */
29 +static int get_trace_fd(const char *key, int *need_close)
30 {
31 - char *trace = getenv("GIT_TRACE");
31 + char *trace = getenv(key);
32
33 if (!trace || !strcmp(trace, "") ||
34 !strcmp(trace, "0") || !strcasecmp(trace, "false"))
@@ -50,10 +50,10 @@ static int get_trace_fd(int *need_close)
50 return fd;
51 }
52
53 - fprintf(stderr, "What does '%s' for GIT_TRACE mean?\n", trace);
53 + fprintf(stderr, "What does '%s' for %s mean?\n", trace, key);
54 fprintf(stderr, "If you want to trace into a file, "
55 - "then please set GIT_TRACE to an absolute pathname "
56 - "(starting with /).\n");
55 + "then please set %s to an absolute pathname "
56 + "(starting with /).\n", key);
57 fprintf(stderr, "Defaulting to tracing on stderr...\n");
58
59 return STDERR_FILENO;
@@ -62,12 +62,12 @@ static int get_trace_fd(int *need_close)
62 static const char err_msg[] = "Could not trace into fd given by "
63 "GIT_TRACE environment variable";
64
65 -void trace_vprintf(const char *fmt, va_list ap)
65 +void trace_vprintf(const char *key, const char *fmt, va_list ap)
66 {
67 struct strbuf buf = STRBUF_INIT;
68 int fd, need_close = 0;
69
70 - fd = get_trace_fd(&need_close);
70 + fd = get_trace_fd(key, &need_close);
71 if (!fd)
72 return;
73
@@ -80,11 +80,19 @@ void trace_vprintf(const char *fmt, va_list ap)
80 close(fd);
81 }
82
83 +void trace_printf_key(const char *key, const char *fmt, ...)
84 +{
85 + va_list ap;
86 + va_start(ap, fmt);
87 + trace_vprintf(key, fmt, ap);
88 + va_end(ap);
89 +}
90 +
91 void trace_printf(const char *fmt, ...)
92 {
93 va_list ap;
94 va_start(ap, fmt);
87 - trace_vprintf(fmt, ap);
95 + trace_vprintf("GIT_TRACE", fmt, ap);
96 va_end(ap);
97 }
98
@@ -94,7 +102,7 @@ void trace_argv_printf(const char **argv, const char *fmt, ...)
102 va_list ap;
103 int fd, need_close = 0;
104
97 - fd = get_trace_fd(&need_close);
105 + fd = get_trace_fd("GIT_TRACE", &need_close);
106 if (!fd)
107 return;
108