Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #26350

Re: Problem with printf-like logging framework and inline hexdumps.

From Jef Driesen <jefdriesen@hotmail.com.invalid>
Newsgroups comp.lang.c
Subject Re: Problem with printf-like logging framework and inline hexdumps.
Date 2012-09-14 18:31 +0200
Organization Aioe.org NNTP Server
Message-ID <k2vm54$ugp$1@speranza.aioe.org> (permalink)
References <k2t0k8$cjf$1@speranza.aioe.org> <0.b131ab98d00632aa0f8a.20120913212435BST.87fw6liscc.fsf@bsb.me.uk> <k2tk9f$1k4$1@speranza.aioe.org> <0.0d6e7c2b5d0bb3a4be5a.20120913235432BST.87sjalh6tz.fsf@bsb.me.uk>

Show all headers | View raw


On 14-09-12 00:54, Ben Bacarisse wrote:
> Jef Driesen <jefdriesen@hotmail.com.invalid> writes:
>
>> On 13-09-12 22:24, Ben Bacarisse wrote:
> <snip>
>>> Rolling your own format processing is not that hard.  I'd process the
>>> format in chunks that bracket your own format codes.  Here's an
>>> example:
>>>
>>>     void logit(const char *fmt, ...)
>>>     {
>>>          va_list al;
>>>          va_start(al, fmt);
>>>          const char *fp;
>>>          while ((fp = strstr(fmt, "%H")) != NULL) {
>>>               char tmp[fp - fmt + 1];
>>>               memcpy(tmp, fmt, sizeof tmp - 1);
>>>               tmp[sizeof tmp - 1] = 0;
>>>               vprintf(tmp, al);
>>>
>>>               printf("<%d>", va_arg(al, int));
>>>
>>>               fmt = fp + 2;
>>>          }
>>>          vprintf(fmt, al);
>>>          va_end(al);
>>>     }
>>>
>>> Here, a format of %H is treated as if it meant "<%d>" but you can do
>>> whatever you like at that point (in fact, my use-case called a function
>>> keyed from the the format letter).
>>>
>>> Obviously you'll need a cleverer function to match a custom format.  %H
>>> should not match after a %, for example, and your formats are likely to
>>> have counts in them.
>>>
>>> I don't think it even breaks any of the rules about handling va_lists.
>>
>> I also thought of something like this, but the manpages for the vprintf
>> functions says this on my linux system:
>>
>> These functions do not call the va_end macro.  Because they invoke the va_arg
>> macro, the value of ap is undefined after the call. See stdarg(3).
>
> Ah, yes.  The above is from some old code when things were a little
> wilder...
>
> I don't think all is lost.   I think there is a fiddly dance you can do
> with va_end and va_copy to get the job done:
>
>    void logit(const char *fmt, ...)
>    {
>         va_list al;
>         va_start(al, fmt);
>         const char *fp;
>         while ((fp = strstr(fmt, "%H")) != NULL) {
>              char tmp[fp - fmt + 1];
>              memcpy(tmp, fmt, sizeof tmp - 1);
>              tmp[sizeof tmp - 1] = 0;
>
>              va_list al_copy;
>              va_copy(al_copy, al);
>
>              vprintf(tmp, al_copy);
>
>              va_end(al_copy);
>              va_end(al);
>              va_copy(al, al_copy);
>
>              printf("<%d>", va_arg(al, int));
>              fmt = fp + 2;
>         }
>         vprintf(fmt, al);
>         va_end(al);
>    }
>
> This passes a cursory reading of the va_* macro restrictions in the
> standard but I'd welcome some more input...

I think this version has the same problem as the original. After
calling the va_end macro, the va_list variable becomes undefined
according to the manpages on my linux system:

"After the call va_end(ap) the variable ap is undefined."

So I guess that if this version works, then the original would have
worked too.

Jef

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Problem with printf-like logging framework and inline hexdumps. Jef Driesen <jefdriesen@hotmail.com.invalid> - 2012-09-13 18:11 +0200
  Re: Problem with printf-like logging framework and inline hexdumps. "Heinrich Wolf" <invalid@invalid.invalid> - 2012-09-13 18:55 +0200
    Re: Problem with printf-like logging framework and inline hexdumps. Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-09-20 17:19 +0000
  Re: Problem with printf-like logging framework and inline hexdumps. Barry Schwarz <schwarzb@dqel.com> - 2012-09-13 11:26 -0700
    Re: Problem with printf-like logging framework and inline hexdumps. Jef Driesen <jefdriesen@hotmail.com.invalid> - 2012-09-14 00:06 +0200
  Re: Problem with printf-like logging framework and inline hexdumps. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-09-13 21:24 +0100
    Re: Problem with printf-like logging framework and inline hexdumps. Jef Driesen <jefdriesen@hotmail.com.invalid> - 2012-09-13 23:47 +0200
      Re: Problem with printf-like logging framework and inline hexdumps. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-09-13 23:54 +0100
        Re: Problem with printf-like logging framework and inline hexdumps. Jef Driesen <jefdriesen@hotmail.com.invalid> - 2012-09-14 18:31 +0200
          Re: Problem with printf-like logging framework and inline hexdumps. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-09-14 22:02 +0100
            Re: Problem with printf-like logging framework and inline hexdumps. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-09-14 22:49 +0100
              Re: Problem with printf-like logging framework and inline hexdumps. Jef Driesen <jefdriesen@hotmail.com.invalid> - 2012-09-17 07:09 +0200
              Re: Problem with printf-like logging framework and inline hexdumps. Kaz Kylheku <kaz@kylheku.com> - 2012-09-17 06:39 +0000
                Re: Problem with printf-like logging framework and inline hexdumps. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-09-17 13:30 +0100
      Re: Problem with printf-like logging framework and inline hexdumps. Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-09-14 03:29 -0700
        Re: Problem with printf-like logging framework and inline hexdumps. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-09-14 12:01 +0100
  Re: Problem with printf-like logging framework and inline hexdumps. Jef Driesen <jefdriesen@hotmail.com.invalid> - 2012-09-14 22:50 +0200

csiph-web