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

Simon Farnsworth simonfar at fb.com
Tue Apr 5 12:05:13 EDT 2016


On 05/04/2016 16:52, Jun Wu wrote:
> On 04/05/2016 04:26 PM, Yuya Nishihara wrote:
>> Could you implement it more cleanly?
>
> Umm... I think it's a bit tricky. See comments below.
>
>> - I know abortmsg() never return, but clearing errno here makes me
>> feel bad.
>> - No need to use a preprocessor.
>
> Sorry but I'm trying to avoid code duplication. I cannot think of a
> better way
> than this.
>
> Options I have thought:
> - Add a boolean parameter to abortmsg() to control whether to print errno.
>    Feels strange, inconsistent with other printf-alike functions
> - Check if fmt ends with "(errno)". Feels stupid since it's doing things
>    at runtime while could be done in compile time.
> - Extract common logic to abortmsghead(), abortmsgtail() etc. The va_list
>    part cannot be de-duplicated cleanly without preprocessor.
>

I'd do this the way the printf family does it (with lots of wrappers 
around vfprintf): have a utility function abortmsgerrno_va(int errno, 
const char *fmt, va_list args) which doesn't call va_start or va_end, 
just va_arg as needed.

Then, you have two wrapper functions:

void abortmsg(const char *fmt, va_list args)
{
     va_list args;
     va_start(args, fmt);
     abortmsgerrno_va(0, fmt, args);
     va_end(args);
}

and

void abortmsgerrno(int errno, const char *fmt, va_list args)
{
     va_list args;
     va_start(args, fmt);
     abortmsgerrno_va(errno, fmt, args);
     va_end(args);
}


>> - One-char suffix "e" is hard to distinguish. How about aborterr() or
>> aborterrno()?
>
> If we choose the long version, how about just abortmsgerrno ?
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel at mercurial-scm.org
> https://urldefense.proofpoint.com/v2/url?u=https-3A__www.mercurial-2Dscm.org_mailman_listinfo_mercurial-2Ddevel&d=CwIGaQ&c=5VD0RTtNlTh3ycd41b3MUw&r=mEgSWILcY4c4W3zjApBQLA&m=xsbccjdU7KkGKxsUdmi45QF_Sep4BK_aXX8SllhfO98&s=nrp9et1Kg2gQIMUzYjddmSNVBPw6-6meivyKDywJmdg&e=
>

-- 
Simon Farnsworth


More information about the Mercurial-devel mailing list