path: root/usage.c
diff options
authorJeff King <>2015-08-11 18:06:15 (GMT)
committerJunio C Hamano <>2015-08-11 21:24:50 (GMT)
commit3b331e92671469614662830402103848a8004b97 (patch)
treed7ce9a7eb9d1eeb06d40346984ee621a82c6b766 /usage.c
parenta17c56c056d5fea0843b429132904c429a900229 (diff)
vreportf: report to arbitrary filehandles
The vreportf function always goes to stderr, but run-command wants child errors to go to the parent's original stderr. To solve this, commit a5487dd duplicates the stderr fd and installs die and error handlers to direct the output appropriately (which later turned into the vwritef function). This has two downsides, though: - we make multiple calls to write(), which contradicts the "write at once" logic from d048a96 (print warning/error/fatal messages in one shot, 2007-11-09). - the custom handlers basically duplicate the normal handlers. They're only a few lines of code, but we should not have to repeat the magic "exit(128)", for example. We can solve the first by using fdopen() on the duplicated descriptor. We can't pass this to vreportf, but we could introduce a new vreportf_to to handle it. However, to fix the second problem, we instead introduce a new "set_error_handle" function, which lets the normal vreportf calls output to a handle besides stderr. Thus we can get rid of our custom handlers entirely, and just ask the regular handlers to output to our new descriptor. And as vwritef has no more callers, it can just go away. Signed-off-by: Jeff King <> Signed-off-by: Junio C Hamano <>
Diffstat (limited to 'usage.c')
1 files changed, 9 insertions, 13 deletions
diff --git a/usage.c b/usage.c
index ed14645..e4fa6d2 100644
--- a/usage.c
+++ b/usage.c
@@ -6,23 +6,14 @@
#include "git-compat-util.h"
#include "cache.h"
+static FILE *error_handle;
void vreportf(const char *prefix, const char *err, va_list params)
char msg[4096];
+ FILE *fh = error_handle ? error_handle : stderr;
vsnprintf(msg, sizeof(msg), err, params);
- fprintf(stderr, "%s%s\n", prefix, msg);
-void vwritef(int fd, const char *prefix, const char *err, va_list params)
- char msg[4096];
- int len = vsnprintf(msg, sizeof(msg), err, params);
- if (len > sizeof(msg))
- len = sizeof(msg);
- write_in_full(fd, prefix, strlen(prefix));
- write_in_full(fd, msg, len);
- write_in_full(fd, "\n", 1);
+ fprintf(fh, "%s%s\n", prefix, msg);
static NORETURN void usage_builtin(const char *err, va_list params)
@@ -76,6 +67,11 @@ void set_die_is_recursing_routine(int (*routine)(void))
die_is_recursing = routine;
+void set_error_handle(FILE *fh)
+ error_handle = fh;
void NORETURN usagef(const char *err, ...)
va_list params;