trace: use warning() for printing trace errors
authorJeff King <peff@peff.net>
Fri, 5 Aug 2016 07:56:00 +0000 (03:56 -0400)
committerJunio C Hamano <gitster@pobox.com>
Fri, 5 Aug 2016 16:27:34 +0000 (09:27 -0700)
Right now we just fprintf() straight to stderr, which can
make the output hard to distinguish. It would be helpful to
give it one of our usual prefixes like "error:", "warning:",
etc.

It doesn't make sense to use error() here, as the trace code
is "optional" debugging code. If something goes wrong, we
should warn the user, but saying "error" implies the actual
git operation had a problem. So warning() is the only sane
choice.

Note that this does end up calling warn_routine() to do the
formatting. This is probably a good thing, since they are
clearly trying to hook messages before they make it to
stderr. However, it also means that in theory somebody who
tries to trace from their warn_routine() could cause a loop.
This seems rather unlikely in practice (we've never even
overridden the default warn_builtin routine before, and
recent discussions to do so would just install a noop
routine).

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
trace.c
diff --git a/trace.c b/trace.c
index bdbe1493967c66d986a4d1ed359ad57cd5af4c7c..6a77e4df037cfe48694d5b686bcc291243cb8dd3 100644 (file)
--- a/trace.c
+++ b/trace.c
@@ -61,9 +61,8 @@ static int get_trace_fd(struct trace_key *key)
        else if (is_absolute_path(trace)) {
                int fd = open(trace, O_WRONLY | O_APPEND | O_CREAT, 0666);
                if (fd == -1) {
-                       fprintf(stderr,
-                               "Could not open '%s' for tracing: %s\n"
-                               "Defaulting to tracing on stderr...\n",
+                       warning("Could not open '%s' for tracing: %s\n"
+                               "Defaulting to tracing on stderr...",
                                trace, strerror(errno));
                        key->fd = STDERR_FILENO;
                } else {
@@ -71,10 +70,10 @@ static int get_trace_fd(struct trace_key *key)
                        key->need_close = 1;
                }
        } else {
-               fprintf(stderr, "What does '%s' for %s mean?\n"
+               warning("What does '%s' for %s mean?\n"
                        "If you want to trace into a file, then please set "
                        "%s to an absolute pathname (starting with /).\n"
-                       "Defaulting to tracing on stderr...\n",
+                       "Defaulting to tracing on stderr...",
                        trace, key->key, key->key);
                key->fd = STDERR_FILENO;
        }
@@ -135,7 +134,7 @@ static int prepare_trace_line(const char *file, int line,
 static void trace_write(struct trace_key *key, const void *buf, unsigned len)
 {
        if (write_in_full(get_trace_fd(key), buf, len) < 0)
-               fprintf(stderr, "%s: write error (%s)\n", err_msg, strerror(errno));
+               warning("%s: write error (%s)", err_msg, strerror(errno));
 }
 
 void trace_verbatim(struct trace_key *key, const void *buf, unsigned len)