Patchwork [1,of,3,V2] chg: add util function abortmsgerrno to print error with errno

login
register
mail settings
Submitter Jun Wu
Date April 5, 2016, 4:47 p.m.
Message ID <ff4f30f94e7d54bd36ed.1459874874@x1c>
Download mbox | patch
Permalink /patch/14381/
State Superseded
Commit 57a78a64de44f1f841e997d1caa90deb9c319802
Delegated to: Yuya Nishihara
Headers show

Comments

Jun Wu - April 5, 2016, 4:47 p.m.
# HG changeset patch
# User Jun Wu <quark@fb.com>
# Date 1459873539 -3600
#      Tue Apr 05 17:25:39 2016 +0100
# Node ID ff4f30f94e7d54bd36ed4a8a06faa99a0ac44298
# Parent  1e97bcbb87767e1a96909900595a0b6a7791407b
chg: add util function abortmsgerrno to print error with errno

It's common to abortmsg with the errno information. Let's make a utility
function for it.
timeless - April 5, 2016, 6:04 p.m.
I'm not sure about chg, but for hg, adding a public symbol like this should
be flagged as API.
On Apr 5, 2016 12:50 PM, "Jun Wu" <quark@fb.com> wrote:

> # HG changeset patch
> # User Jun Wu <quark@fb.com>
> # Date 1459873539 -3600
> #      Tue Apr 05 17:25:39 2016 +0100
> # Node ID ff4f30f94e7d54bd36ed4a8a06faa99a0ac44298
> # Parent  1e97bcbb87767e1a96909900595a0b6a7791407b
> chg: add util function abortmsgerrno to print error with errno
>
> It's common to abortmsg with the errno information. Let's make a utility
> function for it.
>
> 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,18 +28,33 @@
>         fprintf(fp, "\033[%sm", code);
>  }
>
> +static void vabortmsgerrno(int no, const char *fmt, va_list args)
> +{
> +       fsetcolor(stderr, "1;31");
> +       fputs("chg: abort: ", stderr);
> +       vfprintf(stderr, fmt, args);
> +       if (no != 0)
> +               fprintf(stderr, " (errno = %d, %s)", no, strerror(no));
> +       fsetcolor(stderr, "");
> +       fputc('\n', stderr);
> +       exit(255);
> +}
> +
>  void abortmsg(const char *fmt, ...)
>  {
>         va_list args;
>         va_start(args, fmt);
> -       fsetcolor(stderr, "1;31");
> -       fputs("chg: abort: ", stderr);
> -       vfprintf(stderr, fmt, args);
> -       fsetcolor(stderr, "");
> -       fputc('\n', stderr);
> +       vabortmsgerrno(0, fmt, args);
>         va_end(args);
> +}
>
> -       exit(255);
> +void abortmsgerrno(const char *fmt, ...)
> +{
> +       int no = errno;
> +       va_list args;
> +       va_start(args, fmt);
> +       vabortmsgerrno(no, fmt, args);
> +       va_end(args);
>  }
>
>  static int debugmsgenabled = 0;
> diff --git a/contrib/chg/util.h b/contrib/chg/util.h
> --- a/contrib/chg/util.h
> +++ b/contrib/chg/util.h
> @@ -17,6 +17,7 @@
>  #endif
>
>  void abortmsg(const char *fmt, ...) PRINTF_FORMAT_;
> +void abortmsgerrno(const char *fmt, ...) PRINTF_FORMAT_;
>
>  void enablecolor(void);
>  void enabledebugmsg(void);
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
>
Yuya Nishihara - April 6, 2016, 2:57 p.m.
On Tue, 5 Apr 2016 17:47:54 +0100, Jun Wu wrote:
> # HG changeset patch
> # User Jun Wu <quark@fb.com>
> # Date 1459873539 -3600
> #      Tue Apr 05 17:25:39 2016 +0100
> # Node ID ff4f30f94e7d54bd36ed4a8a06faa99a0ac44298
> # Parent  1e97bcbb87767e1a96909900595a0b6a7791407b
> chg: add util function abortmsgerrno to print error with errno

Queued these per review by Simon, thanks.

chg is stand-alone executable, so there is no API nor ABI.

Patch

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,18 +28,33 @@ 
 	fprintf(fp, "\033[%sm", code);
 }
 
+static void vabortmsgerrno(int no, const char *fmt, va_list args)
+{
+	fsetcolor(stderr, "1;31");
+	fputs("chg: abort: ", stderr);
+	vfprintf(stderr, fmt, args);
+	if (no != 0)
+		fprintf(stderr, " (errno = %d, %s)", no, strerror(no));
+	fsetcolor(stderr, "");
+	fputc('\n', stderr);
+	exit(255);
+}
+
 void abortmsg(const char *fmt, ...)
 {
 	va_list args;
 	va_start(args, fmt);
-	fsetcolor(stderr, "1;31");
-	fputs("chg: abort: ", stderr);
-	vfprintf(stderr, fmt, args);
-	fsetcolor(stderr, "");
-	fputc('\n', stderr);
+	vabortmsgerrno(0, fmt, args);
 	va_end(args);
+}
 
-	exit(255);
+void abortmsgerrno(const char *fmt, ...)
+{
+	int no = errno;
+	va_list args;
+	va_start(args, fmt);
+	vabortmsgerrno(no, fmt, args);
+	va_end(args);
 }
 
 static int debugmsgenabled = 0;
diff --git a/contrib/chg/util.h b/contrib/chg/util.h
--- a/contrib/chg/util.h
+++ b/contrib/chg/util.h
@@ -17,6 +17,7 @@ 
 #endif
 
 void abortmsg(const char *fmt, ...) PRINTF_FORMAT_;
+void abortmsgerrno(const char *fmt, ...) PRINTF_FORMAT_;
 
 void enablecolor(void);
 void enabledebugmsg(void);