mirror of
https://github.com/git/git.git
synced 2024-11-15 15:03:47 +01:00
f4c3edc0b1
When we call "die(fmt, args...)", we end up in vreportf with two pieces of information: 1. The prefix "fatal: " 2. The original fmt and va_list of args. We format item (2) into a temporary buffer, and then fprintf the prefix and the temporary buffer, along with a newline. This has the unfortunate side effect of truncating any error messages that are longer than 4096 bytes. Instead, let's use separate calls for the prefix and newline, letting us hand the item (2) directly to vfprintf. This is essentially undoing d048a96 (print warning/error/fatal messages in one shot, 2007-11-09), which tried to have the whole output end up in a single `write` call. But we can address this instead by explicitly requesting line-buffering for the output handle, and by making sure that the buffer is empty before we start (so that outputting the prefix does not cause a flush due to hitting the buffer limit). We may still break the output into two writes if the content is larger than our buffer, but there's not much we can do there; depending on the stdio implementation, that might have happened even with a single fprintf call. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
165 lines
3.3 KiB
C
165 lines
3.3 KiB
C
/*
|
|
* GIT - The information manager from hell
|
|
*
|
|
* Copyright (C) Linus Torvalds, 2005
|
|
*/
|
|
#include "git-compat-util.h"
|
|
#include "cache.h"
|
|
|
|
static FILE *error_handle;
|
|
static int tweaked_error_buffering;
|
|
|
|
void vreportf(const char *prefix, const char *err, va_list params)
|
|
{
|
|
FILE *fh = error_handle ? error_handle : stderr;
|
|
|
|
fflush(fh);
|
|
if (!tweaked_error_buffering) {
|
|
setvbuf(fh, NULL, _IOLBF, 0);
|
|
tweaked_error_buffering = 1;
|
|
}
|
|
|
|
fputs(prefix, fh);
|
|
vfprintf(fh, err, params);
|
|
fputc('\n', fh);
|
|
}
|
|
|
|
static NORETURN void usage_builtin(const char *err, va_list params)
|
|
{
|
|
vreportf("usage: ", err, params);
|
|
exit(129);
|
|
}
|
|
|
|
static NORETURN void die_builtin(const char *err, va_list params)
|
|
{
|
|
vreportf("fatal: ", err, params);
|
|
exit(128);
|
|
}
|
|
|
|
static void error_builtin(const char *err, va_list params)
|
|
{
|
|
vreportf("error: ", err, params);
|
|
}
|
|
|
|
static void warn_builtin(const char *warn, va_list params)
|
|
{
|
|
vreportf("warning: ", warn, params);
|
|
}
|
|
|
|
static int die_is_recursing_builtin(void)
|
|
{
|
|
static int dying;
|
|
return dying++;
|
|
}
|
|
|
|
/* If we are in a dlopen()ed .so write to a global variable would segfault
|
|
* (ugh), so keep things static. */
|
|
static NORETURN_PTR void (*usage_routine)(const char *err, va_list params) = usage_builtin;
|
|
static NORETURN_PTR void (*die_routine)(const char *err, va_list params) = die_builtin;
|
|
static void (*error_routine)(const char *err, va_list params) = error_builtin;
|
|
static void (*warn_routine)(const char *err, va_list params) = warn_builtin;
|
|
static int (*die_is_recursing)(void) = die_is_recursing_builtin;
|
|
|
|
void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params))
|
|
{
|
|
die_routine = routine;
|
|
}
|
|
|
|
void set_error_routine(void (*routine)(const char *err, va_list params))
|
|
{
|
|
error_routine = routine;
|
|
}
|
|
|
|
void set_die_is_recursing_routine(int (*routine)(void))
|
|
{
|
|
die_is_recursing = routine;
|
|
}
|
|
|
|
void set_error_handle(FILE *fh)
|
|
{
|
|
error_handle = fh;
|
|
tweaked_error_buffering = 0;
|
|
}
|
|
|
|
void NORETURN usagef(const char *err, ...)
|
|
{
|
|
va_list params;
|
|
|
|
va_start(params, err);
|
|
usage_routine(err, params);
|
|
va_end(params);
|
|
}
|
|
|
|
void NORETURN usage(const char *err)
|
|
{
|
|
usagef("%s", err);
|
|
}
|
|
|
|
void NORETURN die(const char *err, ...)
|
|
{
|
|
va_list params;
|
|
|
|
if (die_is_recursing()) {
|
|
fputs("fatal: recursion detected in die handler\n", stderr);
|
|
exit(128);
|
|
}
|
|
|
|
va_start(params, err);
|
|
die_routine(err, params);
|
|
va_end(params);
|
|
}
|
|
|
|
void NORETURN die_errno(const char *fmt, ...)
|
|
{
|
|
va_list params;
|
|
char fmt_with_err[1024];
|
|
char str_error[256], *err;
|
|
int i, j;
|
|
|
|
if (die_is_recursing()) {
|
|
fputs("fatal: recursion detected in die_errno handler\n",
|
|
stderr);
|
|
exit(128);
|
|
}
|
|
|
|
err = strerror(errno);
|
|
for (i = j = 0; err[i] && j < sizeof(str_error) - 1; ) {
|
|
if ((str_error[j++] = err[i++]) != '%')
|
|
continue;
|
|
if (j < sizeof(str_error) - 1) {
|
|
str_error[j++] = '%';
|
|
} else {
|
|
/* No room to double the '%', so we overwrite it with
|
|
* '\0' below */
|
|
j--;
|
|
break;
|
|
}
|
|
}
|
|
str_error[j] = 0;
|
|
snprintf(fmt_with_err, sizeof(fmt_with_err), "%s: %s", fmt, str_error);
|
|
|
|
va_start(params, fmt);
|
|
die_routine(fmt_with_err, params);
|
|
va_end(params);
|
|
}
|
|
|
|
#undef error
|
|
int error(const char *err, ...)
|
|
{
|
|
va_list params;
|
|
|
|
va_start(params, err);
|
|
error_routine(err, params);
|
|
va_end(params);
|
|
return -1;
|
|
}
|
|
|
|
void warning(const char *warn, ...)
|
|
{
|
|
va_list params;
|
|
|
|
va_start(params, warn);
|
|
warn_routine(warn, params);
|
|
va_end(params);
|
|
}
|