[PATCH 2 of 3] chg: add util function abortmsge to print error with errno

Yuya Nishihara yuya at tcha.org
Tue Apr 5 11:26:19 EDT 2016


On Tue, 5 Apr 2016 15:21:17 +0100, Jun Wu wrote:
> # HG changeset patch
> # User Jun Wu <quark at fb.com>
> # Date 1459865259 -3600
> #      Tue Apr 05 15:07:39 2016 +0100
> # Node ID 6b037b1f423c08785c7c1abe3751f3be1fe550ed
> # Parent  704d3febf4b7ea1063c2c3a58788de13fea31e79
> chg: add util function abortmsge to print error with errno
> 
> It's common to abort with the errno information. Let's make it a utility
> function.
> 
> diff --git a/contrib/chg/util.c b/contrib/chg/util.c
> --- a/contrib/chg/util.c
> +++ b/contrib/chg/util.c
> @@ -7,6 +7,7 @@
>   * GNU General Public License version 2 or any later version.
>   */
>  
> +#include <errno.h>
>  #include <signal.h>
>  #include <stdarg.h>
>  #include <stdio.h>
> @@ -27,13 +28,15 @@
>  	fprintf(fp, "\033[%sm", code);
>  }
>  
> -void abortmsg(const char *fmt, ...)
> +void abortmsge(const char *fmt, ...)
>  {
>  	va_list args;
>  	va_start(args, fmt);
>  	fsetcolor(stderr, "1;31");
>  	fputs("chg: abort: ", stderr);
>  	vfprintf(stderr, fmt, args);
> +	if (errno != 0)
> +		fprintf(stderr, " (errno = %d, %s)", errno, strerror(errno));
>  	fsetcolor(stderr, "");
>  	fputc('\n', stderr);
>  	va_end(args);
> diff --git a/contrib/chg/util.h b/contrib/chg/util.h
> --- a/contrib/chg/util.h
> +++ b/contrib/chg/util.h
> @@ -16,7 +16,8 @@
>  #define PRINTF_FORMAT_
>  #endif
>  
> -void abortmsg(const char *fmt, ...) PRINTF_FORMAT_;
> +void abortmsge(const char *fmt, ...) PRINTF_FORMAT_;
> +#define abortmsg(...) { errno = 0; abortmsge(__VA_ARGS__); }

Could you implement it more cleanly?

 - I know abortmsg() never return, but clearing errno here makes me feel bad.
 - No need to use a preprocessor.
 - One-char suffix "e" is hard to distinguish. How about aborterr() or
   aborterrno()?


More information about the Mercurial-devel mailing list