Skip to content

Commit 38f460c

Browse files
peffgitster
authored andcommitted
trace: use warning() for printing trace errors
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c0222e7 commit 38f460c

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

trace.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,19 @@ static int get_trace_fd(struct trace_key *key)
6161
else if (is_absolute_path(trace)) {
6262
int fd = open(trace, O_WRONLY | O_APPEND | O_CREAT, 0666);
6363
if (fd == -1) {
64-
fprintf(stderr,
65-
"Could not open '%s' for tracing: %s\n"
66-
"Defaulting to tracing on stderr...\n",
64+
warning("Could not open '%s' for tracing: %s\n"
65+
"Defaulting to tracing on stderr...",
6766
trace, strerror(errno));
6867
key->fd = STDERR_FILENO;
6968
} else {
7069
key->fd = fd;
7170
key->need_close = 1;
7271
}
7372
} else {
74-
fprintf(stderr, "What does '%s' for %s mean?\n"
73+
warning("What does '%s' for %s mean?\n"
7574
"If you want to trace into a file, then please set "
7675
"%s to an absolute pathname (starting with /).\n"
77-
"Defaulting to tracing on stderr...\n",
76+
"Defaulting to tracing on stderr...",
7877
trace, key->key, key->key);
7978
key->fd = STDERR_FILENO;
8079
}
@@ -135,7 +134,7 @@ static int prepare_trace_line(const char *file, int line,
135134
static void trace_write(struct trace_key *key, const void *buf, unsigned len)
136135
{
137136
if (write_in_full(get_trace_fd(key), buf, len) < 0)
138-
fprintf(stderr, "%s: write error (%s)\n", err_msg, strerror(errno));
137+
warning("%s: write error (%s)", err_msg, strerror(errno));
139138
}
140139

141140
void trace_verbatim(struct trace_key *key, const void *buf, unsigned len)

0 commit comments

Comments
 (0)