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


Groups > comp.lang.c > #15813 > unrolled thread

macro

Started bydspfun <dspfun@hotmail.com>
First post2012-01-15 05:29 -0800
Last post2012-01-16 12:01 +0000
Articles 20 on this page of 44 — 19 participants

Back to article view | Back to comp.lang.c


Contents

  macro dspfun <dspfun@hotmail.com> - 2012-01-15 05:29 -0800
    Re: macro Dr Nick <3-nospam@temporary-address.org.uk> - 2012-01-15 13:44 +0000
      Re: macro dspfun <dspfun@hotmail.com> - 2012-01-15 05:58 -0800
        Re: macro "Joachim Schmitz" <nospam.jojo@schmitz-digital.de> - 2012-01-15 15:34 +0100
        Re: macro "christian.bau" <christian.bau@cbau.wanadoo.co.uk> - 2012-01-15 06:37 -0800
          Re: macro dspfun <dspfun@hotmail.com> - 2012-01-15 07:28 -0800
            Re: macro Philip Lantz <prl@canterey.us> - 2012-01-15 17:30 -0800
              Re: macro Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-01-16 00:18 -0800
                Re: macro Hans Vlems <hvlems@freenet.de> - 2012-01-16 00:52 -0800
                  Re: macro Ian Collins <ian-news@hotmail.com> - 2012-01-16 22:08 +1300
                  Re: macro Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-01-16 03:04 -0800
                  Re: macro Markus Wichmann <nullplan@gmx.net> - 2012-01-16 12:42 +0100
          Re: macro jgk@panix.com (Joe keane) - 2012-01-16 14:48 +0000
            Re: macro Keith Thompson <kst-u@mib.org> - 2012-01-16 12:01 -0800
              Re: macro Philip Lantz <prl@canterey.us> - 2012-01-17 01:02 -0800
                Re: macro Keith Thompson <kst-u@mib.org> - 2012-01-17 13:05 -0800
                  Re: macro Ian Collins <ian-news@hotmail.com> - 2012-01-18 13:12 +1300
                    Re: macro dspfun <dspfun@hotmail.com> - 2012-01-18 01:05 -0800
                      Re: macro Ian Collins <ian-news@hotmail.com> - 2012-01-18 22:57 +1300
                        Re: macro "osmium" <r124c4u102@comcast.net> - 2012-01-18 06:22 -0600
                        Re: macro dspfun <dspfun@hotmail.com> - 2012-01-18 07:50 -0800
                          Re: macro Ian Collins <ian-news@hotmail.com> - 2012-01-20 14:09 +1300
                        Re: macro blp@cs.stanford.edu (Ben Pfaff) - 2012-01-18 11:28 -0800
                        Re: macro Kaz Kylheku <kaz@kylheku.com> - 2012-01-18 21:09 +0000
                          Re: macro Ian Collins <ian-news@hotmail.com> - 2012-01-20 15:08 +1300
                            [OT] Re: macro Ian Collins <ian-news@hotmail.com> - 2012-01-20 15:58 +1300
                              Re: [OT] Re: macro Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-01-20 12:22 +0000
                                Re: [OT] Re: macro Ian Collins <ian-news@hotmail.com> - 2012-01-21 07:58 +1300
                            Re: macro James Kuyper <jameskuyper@verizon.net> - 2012-01-19 22:47 -0500
                              Re: macro Ian Collins <ian-news@hotmail.com> - 2012-01-20 17:06 +1300
                                Re: macro James Kuyper <jameskuyper@verizon.net> - 2012-01-20 00:12 -0500
                                  Re: macro Ian Collins <ian-news@hotmail.com> - 2012-01-20 18:28 +1300
                            Re: macro Keith Thompson <kst-u@mib.org> - 2012-01-20 03:21 -0800
                          Re: macro Shao Miller <sha0.miller@gmail.com> - 2012-01-20 10:50 -0500
                            Re: macro Shao Miller <sha0.miller@gmail.com> - 2012-01-20 11:46 -0500
                        Re: macro Keith Thompson <kst-u@mib.org> - 2012-01-18 14:24 -0800
                        Re: macro jgk@panix.com (Joe keane) - 2012-01-18 22:36 +0000
                          Re: macro Kaz Kylheku <kaz@kylheku.com> - 2012-01-18 22:50 +0000
              Re: macro jgk@panix.com (Joe keane) - 2012-01-17 22:08 +0000
    Re: macro Keith Thompson <kst-u@mib.org> - 2012-01-15 08:00 -0800
    Re: macro NEON China BLUE <chine.bleu@yahoo.com> - 2012-01-15 08:15 -0800
    Re: macro Ian Collins <ian-news@hotmail.com> - 2012-01-16 09:04 +1300
      Re: macro Philip Lantz <prl@canterey.us> - 2012-01-15 17:36 -0800
    Re: macro Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-01-16 12:01 +0000

Page 1 of 3  [1] 2 3  Next page →


#15813 — macro

Fromdspfun <dspfun@hotmail.com>
Date2012-01-15 05:29 -0800
Subjectmacro
Message-ID<89018f82-9f45-4e72-a0a2-8424e91752b6@g41g2000yqa.googlegroups.com>
Hi all!

Can you see anything wrong or "bad" with the following macro?

#define LOG(level, msg) \
openlog (__FUNCTION__, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_USER);
\
syslog (level, "%s", msg);\
closelog ();

I have problems using the macro with ternary operators. I believe it
should have parantheses around it all, but I cannot get it right.

Brs,
Markus

[toc] | [next] | [standalone]


#15815

FromDr Nick <3-nospam@temporary-address.org.uk>
Date2012-01-15 13:44 +0000
Message-ID<87boq5ozau.fsf@temporary-address.org.uk>
In reply to#15813
dspfun <dspfun@hotmail.com> writes:

> Hi all!
>
> Can you see anything wrong or "bad" with the following macro?
>
> #define LOG(level, msg) \
> openlog (__FUNCTION__, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_USER);
> \
> syslog (level, "%s", msg);\
> closelog ();
>
> I have problems using the macro with ternary operators. I believe it
> should have parantheses around it all, but I cannot get it right.

Do you mean using it inside operators - like:

b==c ? LOG(10,"hello") : LOG(2,"goodbye");

If so, then you will have problems as the macro is three statements while
the ternary operator expects a single expression on each side.

If you do mean that - why are you doing it?  The macro returns no value
so you'd be better with an if.  If you don't mean that, what do you mean
(hint - give an example of where it's not working).

If you do want to use it with an if you need to be careful to wrap it in
{}s.  You might want to use the do{ ... } while(0) trick.
-- 
Online waterways route planner            | http://canalplan.eu
Plan trips, see photos, check facilities  | http://canalplan.org.uk

[toc] | [prev] | [next] | [standalone]


#15816

Fromdspfun <dspfun@hotmail.com>
Date2012-01-15 05:58 -0800
Message-ID<8645ca39-0a48-4efb-af29-86f84eb04cd5@h3g2000yqe.googlegroups.com>
In reply to#15815
On 15 Jan, 14:44, Dr Nick <3-nos...@temporary-address.org.uk> wrote:
> dspfun <dsp...@hotmail.com> writes:
> > Hi all!
>
> > Can you see anything wrong or "bad" with the following macro?
>
> > #define LOG(level, msg) \
> > openlog (__FUNCTION__, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_USER);
> > \
> > syslog (level, "%s", msg);\
> > closelog ();
>
> > I have problems using the macro with ternary operators. I believe it
> > should have parantheses around it all, but I cannot get it right.
>
> Do you mean using it inside operators - like:
>
> b==c ? LOG(10,"hello") : LOG(2,"goodbye");
>
> If so, then you will have problems as the macro is three statements while
> the ternary operator expects a single expression on each side.
>
> If you do mean that - why are you doing it?  The macro returns no value
> so you'd be better with an if.  If you don't mean that, what do you mean
> (hint - give an example of where it's not working).
>
> If you do want to use it with an if you need to be careful to wrap it in
> {}s.  You might want to use the do{ ... } while(0) trick.
> --
> Online waterways route planner            |http://canalplan.eu
> Plan trips, see photos, check facilities  |http://canalplan.org.uk

Hi, thanks for your quick reply!

Here is the function/code where it is used (slightly modified, but
still is valid):

static void
foo(Boolean internal)
internal ? TRACE_IF(8, STR("Doing internal stuff\n")) : TRACE_IF(8,
STR("Not internal stuff\n"));
..snip...


TRACE_IF is defined as:

#define TRACE_IF(GROUP,MSG) LOG(LOG_INFO, MSG)


LOG is defined as:
#define LOG(level,msg) openlog (__FUNCTION__, LOG_CONS | LOG_PID |
LOG_NDELAY, LOG_USER); syslog (level, "%s", msg);closelog ();


Resulting in:

internal ? openlog (__FUNCTION__, 0x02 | 0x01 | 0x08, (1<<3)); syslog
(6, "%s", STR("Doing internal stuff\n"));closelog (); : openlog
(__FUNCTION__, 0x02 | 0x01 | 0x08, (1<<3)); syslog (6, "%s", STR("Not
internal stuff\n"));closelog ();;


Which gives the compile error:
foo.c:737: error: expected ':' before ';' token

What would be the "proper" way to define the macro LOG above?

Brs,
Markus

[toc] | [prev] | [next] | [standalone]


#15817

From"Joachim Schmitz" <nospam.jojo@schmitz-digital.de>
Date2012-01-15 15:34 +0100
Message-ID<jeuo5f$93f$1@online.de>
In reply to#15816
dspfun wrote:
> On 15 Jan, 14:44, Dr Nick <3-nos...@temporary-address.org.uk> wrote:
>> dspfun <dsp...@hotmail.com> writes:
>>> Hi all!
>> 
>>> Can you see anything wrong or "bad" with the following macro?
>> 
>>> #define LOG(level, msg) \
>>> openlog (__FUNCTION__, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_USER);
>>> \
>>> syslog (level, "%s", msg);\
>>> closelog ();
>> 
>>> I have problems using the macro with ternary operators. I believe it
>>> should have parantheses around it all, but I cannot get it right.
>> 
>> Do you mean using it inside operators - like:
>> 
>> b==c ? LOG(10,"hello") : LOG(2,"goodbye");
>> 
>> If so, then you will have problems as the macro is three statements
>> while the ternary operator expects a single expression on each side.
>> 
>> If you do mean that - why are you doing it? The macro returns no
>> value so you'd be better with an if. If you don't mean that, what do
>> you mean (hint - give an example of where it's not working).
>> 
>> If you do want to use it with an if you need to be careful to wrap
>> it in {}s. You might want to use the do{ ... } while(0) trick.
>> --
>> Online waterways route planner |http://canalplan.eu
>> Plan trips, see photos, check facilities |http://canalplan.org.uk
> 
> Hi, thanks for your quick reply!
> 
> Here is the function/code where it is used (slightly modified, but
> still is valid):
> 
> static void
> foo(Boolean internal)
> internal ? TRACE_IF(8, STR("Doing internal stuff\n")) : TRACE_IF(8,
> STR("Not internal stuff\n"));
> ..snip...
> 
> 
> TRACE_IF is defined as:
> 
> #define TRACE_IF(GROUP,MSG) LOG(LOG_INFO, MSG)
> 
> 
> LOG is defined as:
> #define LOG(level,msg) openlog (__FUNCTION__, LOG_CONS | LOG_PID |
> LOG_NDELAY, LOG_USER); syslog (level, "%s", msg);closelog ();
> 
> 
> Resulting in:
> 
> internal ? openlog (__FUNCTION__, 0x02 | 0x01 | 0x08, (1<<3)); syslog
> (6, "%s", STR("Doing internal stuff\n"));closelog (); : openlog
> (__FUNCTION__, 0x02 | 0x01 | 0x08, (1<<3)); syslog (6, "%s", STR("Not
> internal stuff\n"));closelog ();;
> 
> 
> Which gives the compile error:
> foo.c:737: error: expected ':' before ';' token
> 
> What would be the "proper" way to define the macro LOG above?

Try this:

#define LOG(level,msg) do {\
 openlog (__FUNCTION__, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_USER);\
 syslog (level, "%s", msg);\
 closelog (); } while (0)

Bye, Jojo

[toc] | [prev] | [next] | [standalone]


#15818

From"christian.bau" <christian.bau@cbau.wanadoo.co.uk>
Date2012-01-15 06:37 -0800
Message-ID<92fa9a4a-692b-4ffd-a733-97a0b417a669@f1g2000yqi.googlegroups.com>
In reply to#15816
On Jan 15, 1:58 pm, dspfun <dsp...@hotmail.com> wrote:
> On 15 Jan, 14:44, Dr Nick <3-nos...@temporary-address.org.uk> wrote:
>
>
>
>
>
>
>
>
>
> > dspfun <dsp...@hotmail.com> writes:
> > > Hi all!
>
> > > Can you see anything wrong or "bad" with the following macro?
>
> > > #define LOG(level, msg) \
> > > openlog (__FUNCTION__, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_USER);
> > > \
> > > syslog (level, "%s", msg);\
> > > closelog ();
>
> > > I have problems using the macro with ternary operators. I believe it
> > > should have parantheses around it all, but I cannot get it right.
>
> > Do you mean using it inside operators - like:
>
> > b==c ? LOG(10,"hello") : LOG(2,"goodbye");
>
> > If so, then you will have problems as the macro is three statements while
> > the ternary operator expects a single expression on each side.
>
> > If you do mean that - why are you doing it?  The macro returns no value
> > so you'd be better with an if.  If you don't mean that, what do you mean
> > (hint - give an example of where it's not working).
>
> > If you do want to use it with an if you need to be careful to wrap it in
> > {}s.  You might want to use the do{ ... } while(0) trick.
> > --
> > Online waterways route planner            |http://canalplan.eu
> > Plan trips, see photos, check facilities  |http://canalplan.org.uk
>
> Hi, thanks for your quick reply!
>
> Here is the function/code where it is used (slightly modified, but
> still is valid):
>
> static void
> foo(Boolean internal)
> internal ? TRACE_IF(8, STR("Doing internal stuff\n")) : TRACE_IF(8,
> STR("Not internal stuff\n"));
> ..snip...
>
> TRACE_IF is defined as:
>
> #define TRACE_IF(GROUP,MSG) LOG(LOG_INFO, MSG)
>
> LOG is defined as:
> #define LOG(level,msg) openlog (__FUNCTION__, LOG_CONS | LOG_PID |
> LOG_NDELAY, LOG_USER); syslog (level, "%s", msg);closelog ();
>
> Resulting in:
>
> internal ? openlog (__FUNCTION__, 0x02 | 0x01 | 0x08, (1<<3)); syslog
> (6, "%s", STR("Doing internal stuff\n"));closelog (); : openlog
> (__FUNCTION__, 0x02 | 0x01 | 0x08, (1<<3)); syslog (6, "%s", STR("Not
> internal stuff\n"));closelog ();;

To answer your original question:

What's wrong with the macro is that it is three statements, and you
use it in a context where only an expression is required.
Solution: Don't do that.

You probably want to use the well-known trick do create a macro that
is a single statement without semicolon like:

#define LOG (level, msg) do { whatever (); } while (0)

and get rid of that rubbish where you use a ternary operator to save
an if statement, which is just rubbish and demonstrates that someone
tries to look clever without actually being clever. Or change the
#define so that the macro is an actual expression.

[toc] | [prev] | [next] | [standalone]


#15819

Fromdspfun <dspfun@hotmail.com>
Date2012-01-15 07:28 -0800
Message-ID<9c5c53ae-3142-45e2-9071-f6cacfbf3dec@d10g2000vbh.googlegroups.com>
In reply to#15818
On 15 Jan, 15:37, "christian.bau" <christian....@cbau.wanadoo.co.uk>
wrote:
> On Jan 15, 1:58 pm, dspfun <dsp...@hotmail.com> wrote:
>
>
>
>
>
>
>
>
>
> > On 15 Jan, 14:44, Dr Nick <3-nos...@temporary-address.org.uk> wrote:
>
> > > dspfun <dsp...@hotmail.com> writes:
> > > > Hi all!
>
> > > > Can you see anything wrong or "bad" with the following macro?
>
> > > > #define LOG(level, msg) \
> > > > openlog (__FUNCTION__, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_USER);
> > > > \
> > > > syslog (level, "%s", msg);\
> > > > closelog ();
>
> > > > I have problems using the macro with ternary operators. I believe it
> > > > should have parantheses around it all, but I cannot get it right.
>
> > > Do you mean using it inside operators - like:
>
> > > b==c ? LOG(10,"hello") : LOG(2,"goodbye");
>
> > > If so, then you will have problems as the macro is three statements while
> > > the ternary operator expects a single expression on each side.
>
> > > If you do mean that - why are you doing it?  The macro returns no value
> > > so you'd be better with an if.  If you don't mean that, what do you mean
> > > (hint - give an example of where it's not working).
>
> > > If you do want to use it with an if you need to be careful to wrap it in
> > > {}s.  You might want to use the do{ ... } while(0) trick.
> > > --
> > > Online waterways route planner            |http://canalplan.eu
> > > Plan trips, see photos, check facilities  |http://canalplan.org.uk
>
> > Hi, thanks for your quick reply!
>
> > Here is the function/code where it is used (slightly modified, but
> > still is valid):
>
> > static void
> > foo(Boolean internal)
> > internal ? TRACE_IF(8, STR("Doing internal stuff\n")) : TRACE_IF(8,
> > STR("Not internal stuff\n"));
> > ..snip...
>
> > TRACE_IF is defined as:
>
> > #define TRACE_IF(GROUP,MSG) LOG(LOG_INFO, MSG)
>
> > LOG is defined as:
> > #define LOG(level,msg) openlog (__FUNCTION__, LOG_CONS | LOG_PID |
> > LOG_NDELAY, LOG_USER); syslog (level, "%s", msg);closelog ();
>
> > Resulting in:
>
> > internal ? openlog (__FUNCTION__, 0x02 | 0x01 | 0x08, (1<<3)); syslog
> > (6, "%s", STR("Doing internal stuff\n"));closelog (); : openlog
> > (__FUNCTION__, 0x02 | 0x01 | 0x08, (1<<3)); syslog (6, "%s", STR("Not
> > internal stuff\n"));closelog ();;
>
> To answer your original question:
>
> What's wrong with the macro is that it is three statements, and you
> use it in a context where only an expression is required.
> Solution: Don't do that.
>
> You probably want to use the well-known trick do create a macro that
> is a single statement without semicolon like:
>
> #define LOG (level, msg) do { whatever (); } while (0)
>
> and get rid of that rubbish where you use a ternary operator to save
> an if statement, which is just rubbish and demonstrates that someone
> tries to look clever without actually being clever. Or change the
> #define so that the macro is an actual expression.

Thanks for all your replies!

Brs,
Markus

[toc] | [prev] | [next] | [standalone]


#15837

FromPhilip Lantz <prl@canterey.us>
Date2012-01-15 17:30 -0800
Message-ID<MPG.297d1da7c8b5ca81989684@news.eternal-september.org>
In reply to#15819
dspfun wrote:
> On 15 Jan, 15:37, "christian.bau" wrote:
> > On Jan 15, 1:58 pm, dspfun wrote:
> > > On 15 Jan, 14:44, Dr Nick wrote:
> > > > dspfun <dsp...@hotmail.com> writes:
> > > > > Can you see anything wrong or "bad" with the following macro?
> > > > > #define LOG(level, msg) \
> > > > > openlog (__FUNCTION__, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_USER);
> > > > > \
> > > > > syslog (level, "%s", msg);\
> > > > > closelog ();
> >
> > > > > I have problems using the macro with ternary operators. I believe it
> > > > > should have parantheses around it all, but I cannot get it right.
> >
> > > > Do you mean using it inside operators - like:
> >
> > > > b==c ? LOG(10,"hello") : LOG(2,"goodbye");
> >
> > > > If so, then you will have problems as the macro is three statements while
> > > > the ternary operator expects a single expression on each side.
> >
> > > > If you do want to use it with an if you need to be careful to wrap it in
> > > > {}s.  You might want to use the do{ ... } while(0) trick.
> >
> > > Here is the function/code where it is used (slightly modified, but
> > > still is valid):
> >
> > > static void
> > > foo(Boolean internal)
> > > internal ? TRACE_IF(8, STR("Doing internal stuff\n")) : TRACE_IF(8,
> > > STR("Not internal stuff\n"));
> > > ..snip...
> >
> > > TRACE_IF is defined as:
> >
> > > #define TRACE_IF(GROUP,MSG) LOG(LOG_INFO, MSG)
> >
> > > LOG is defined as:
> > > #define LOG(level,msg) openlog (__FUNCTION__, LOG_CONS | LOG_PID |
> > > LOG_NDELAY, LOG_USER); syslog (level, "%s", msg);closelog ();
> >
> > > Resulting in:
> >
> > > internal ? openlog (__FUNCTION__, 0x02 | 0x01 | 0x08, (1<<3)); syslog
> > > (6, "%s", STR("Doing internal stuff\n"));closelog (); : openlog
> > > (__FUNCTION__, 0x02 | 0x01 | 0x08, (1<<3)); syslog (6, "%s", STR("Not
> > > internal stuff\n"));closelog ();;
> >
> > To answer your original question:
> >
> > What's wrong with the macro is that it is three statements, and you
> > use it in a context where only an expression is required.
> > Solution: Don't do that.
> >
> > You probably want to use the well-known trick do create a macro that
> > is a single statement without semicolon like:
> >
> > #define LOG (level, msg) do { whatever (); } while (0)
> >
> > and get rid of that rubbish where you use a ternary operator to save
> > an if statement, which is just rubbish and demonstrates that someone
> > tries to look clever without actually being clever. Or change the
> > #define so that the macro is an actual expression.

I respectfully disagree with the suggestion to put do ... while (0) 
around this macro. In my experience, a macro should always be written as 
an expression if it can be. In the example from the OP, there is no 
reason whatsoever to write it as a statement--all that is required to 
write it as an expression is to substitute commas for the semicolons and 
enclose the whole thing in parentheses.

However, I do agree with the advice to avoid the ternary operator and 
use an if statement. (Unless, of course, the ternary operator is within 
another macro, in which case refer to the above paragraph.)

The do ... while (0) idiom is great when the macro cannot be written as 
an expression.

And one final thing: are you *sure* you want to open and close the log 
for every trace message?

Philip

[toc] | [prev] | [next] | [standalone]


#15840

FromNick Keighley <nick_keighley_nospam@hotmail.com>
Date2012-01-16 00:18 -0800
Message-ID<1560fffc-f76b-4c7f-a4ab-c52be69897a6@f33g2000yqh.googlegroups.com>
In reply to#15837
On Jan 16, 1:30 am, Philip Lantz <p...@canterey.us> wrote:

> And one final thing: are you *sure* you want to open and close the log
> for every trace message?

1. you can read the logfile on a Windows system while the system is
running
2. if the system crashes you get all the trace messages

you could bundle trace messages and write them out, say, every second

[toc] | [prev] | [next] | [standalone]


#15841

FromHans Vlems <hvlems@freenet.de>
Date2012-01-16 00:52 -0800
Message-ID<03fa4fde-db29-43ca-895c-ec666cf3595d@f33g2000yqh.googlegroups.com>
In reply to#15840
On 16 jan, 09:18, Nick Keighley <nick_keighley_nos...@hotmail.com>
wrote:
> On Jan 16, 1:30 am, Philip Lantz <p...@canterey.us> wrote:
>
> > And one final thing: are you *sure* you want to open and close the log
> > for every trace message?
>
> 1. you can read the logfile on a Windows system while the system is
> running
> 2. if the system crashes you get all the trace messages
>
> you could bundle trace messages and write them out, say, every second

Wouldn't a call to fflush() do the same thing?
Has fflush() possibly nasty side effects perhaps?
Hans

[toc] | [prev] | [next] | [standalone]


#15842

FromIan Collins <ian-news@hotmail.com>
Date2012-01-16 22:08 +1300
Message-ID<9ni7njFlpvU1@mid.individual.net>
In reply to#15841
On 01/16/12 09:52 PM, Hans Vlems wrote:
> On 16 jan, 09:18, Nick Keighley<nick_keighley_nos...@hotmail.com>
> wrote:
>> On Jan 16, 1:30 am, Philip Lantz<p...@canterey.us>  wrote:
>>
>>> And one final thing: are you *sure* you want to open and close the log
>>> for every trace message?
>>
>> 1. you can read the logfile on a Windows system while the system is
>> running
>> 2. if the system crashes you get all the trace messages
>>
>> you could bundle trace messages and write them out, say, every second
>
> Wouldn't a call to fflush() do the same thing?
> Has fflush() possibly nasty side effects perhaps?
> Hans

On Unix systems, the system log isn't a file, it's a service.

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#15843

FromNick Keighley <nick_keighley_nospam@hotmail.com>
Date2012-01-16 03:04 -0800
Message-ID<2a97f26d-d403-4f1b-849d-3da5c1fc9691@g27g2000yqa.googlegroups.com>
In reply to#15841
On Jan 16, 8:52 am, Hans Vlems <hvl...@freenet.de> wrote:
> On 16 jan, 09:18, Nick Keighley <nick_keighley_nos...@hotmail.com>
> wrote:
>
> > On Jan 16, 1:30 am, Philip Lantz <p...@canterey.us> wrote:
>
> > > And one final thing: are you *sure* you want to open and close the log
> > > for every trace message?
>
> > 1. you can read the logfile on a Windows system while the system is
> > running
> > 2. if the system crashes you get all the trace messages
>
> > you could bundle trace messages and write them out, say, every second
>
> Wouldn't a call to fflush() do the same thing?
> Has fflush() possibly nasty side effects perhaps?

I don't think you can read the file  though

[toc] | [prev] | [next] | [standalone]


#15845

FromMarkus Wichmann <nullplan@gmx.net>
Date2012-01-16 12:42 +0100
Message-ID<52fdu8-nk1.ln1@voyager.wichi.de.vu>
In reply to#15841
On 16.01.2012 09:52, Hans Vlems wrote:
> On 16 jan, 09:18, Nick Keighley <nick_keighley_nos...@hotmail.com>
> wrote:
>> On Jan 16, 1:30 am, Philip Lantz <p...@canterey.us> wrote:
>>
>>> And one final thing: are you *sure* you want to open and close the log
>>> for every trace message?
>>
>> 1. you can read the logfile on a Windows system while the system is
>> running
>> 2. if the system crashes you get all the trace messages
>>
>> you could bundle trace messages and write them out, say, every second
> 
> Wouldn't a call to fflush() do the same thing?
> Has fflush() possibly nasty side effects perhaps?
> Hans

Well, fflush() needs a FILE*. However, openlog(), closelog() and
syslog() don't return a FILE*. They don't even return an FD.

Of course one could write subroutines to talk to /dev/log, but that
would be mostly redundant.

What bothers me here is that a field commonly used for the program name
is used for the function name.

Ciao,
Markus

[toc] | [prev] | [next] | [standalone]


#15848

Fromjgk@panix.com (Joe keane)
Date2012-01-16 14:48 +0000
Message-ID<jf1dcn$ign$1@reader1.panix.com>
In reply to#15818
In article <92fa9a4a-692b-4ffd-a733-97a0b417a669@f1g2000yqi.googlegroups.com>,
christian.bau <christian.bau@cbau.wanadoo.co.uk> wrote:
>You probably want to use the well-known trick do create a macro that
>is a single statement without semicolon like:
>
>#define LOG (level, msg) do { whatever (); } while (0)
>
>and get rid of that rubbish where you use a ternary operator to save
>an if statement, which is just rubbish and demonstrates that someone
>tries to look clever without actually being clever.

Silly.

Of *course* you want a macro to be expression if that's easily done.

e.g.

	#define JK_GET_Z(JK, Z) \
	(((JK)->a & 0x1) == 0 ? \
	((Z) = (JK)->b >> 2 & 0x1, 0) : JK_GET_XZ(JK, Z))

You could write

	#define JK_GET_Z(JK, Z) \
	do { \
	  if (((JK->a) & 0x1) == 0) \
	    (Z) = (JK)->b >> 2 & 0x1; \
	  else
	    JK_GET_XZ(JK, Z); \
	} while (0)

but *why*?

	#define JK_GET_XZ(JK, Z) \
	(((JK)->a & 0x3) == 0 ? \
	((Z) = (JK)->c >> 3 & 0x1, 0) : jk_get_xz((JK), &(Z)))

You could write

	#define JK_GET_XZ(JK, Z) \
	do { \
	  int err;
	  if (((JK)->a & 0x3) == 0) { \
	    (Z) = (JK)->c >> 3 & 0x1;
	    err = 0; \
	  } else \
	    err = jk_get_xz((JK), &(Z)); \
	  __??(err); \
	} while (0)

but *why*?

[of course the first of the first and the second of the second doesn't
work at all; the first of the second works with the second of the first]

I written plenty of macros.

	#define PQ_FRACK(PQ) \
	(PQ_IS_X(PQ) ? PQ_FRACK_X(PQ) : \
	PQ_IS_Y(PQ) ? PQ_FRACK_Y(PQ) : pq_frack_other(PQ))

	#define PQ_FRACK_Y(PQ) \
	(((PQ)->p & PQ_SBZ) == 0 ? ((PQ)->q |= PQ_FRB, 0) : ERR_DANG)

[toc] | [prev] | [next] | [standalone]


#15851

FromKeith Thompson <kst-u@mib.org>
Date2012-01-16 12:01 -0800
Message-ID<lnhazv1kon.fsf@nuthaus.mib.org>
In reply to#15848
jgk@panix.com (Joe keane) writes:
> In article <92fa9a4a-692b-4ffd-a733-97a0b417a669@f1g2000yqi.googlegroups.com>,
> christian.bau <christian.bau@cbau.wanadoo.co.uk> wrote:
>>You probably want to use the well-known trick do create a macro that
>>is a single statement without semicolon like:
>>
>>#define LOG (level, msg) do { whatever (); } while (0)
>>
>>and get rid of that rubbish where you use a ternary operator to save
>>an if statement, which is just rubbish and demonstrates that someone
>>tries to look clever without actually being clever.
>
> Silly.
>
> Of *course* you want a macro to be expression if that's easily done.
>
> e.g.
>
> 	#define JK_GET_Z(JK, Z) \
> 	(((JK)->a & 0x1) == 0 ? \
> 	((Z) = (JK)->b >> 2 & 0x1, 0) : JK_GET_XZ(JK, Z))
>
> You could write
>
> 	#define JK_GET_Z(JK, Z) \
> 	do { \
> 	  if (((JK->a) & 0x1) == 0) \
> 	    (Z) = (JK)->b >> 2 & 0x1; \
> 	  else
> 	    JK_GET_XZ(JK, Z); \
> 	} while (0)
>
> but *why*?

If you were writing this other than as a macro, presumably you'd use the
if/else from rather than using the conditional operator, simply because
it's more readable that way.  (Well, I almost certainly would.)

Yes, if you write the macro as an expression, you can use it in more
contexts.  But what if you happen to know that you're *never* going to
need to use it other than in a statement context?  Why not write the
macro definition in a more readable and maintainable form, when the only
cost is the loss of some flexibility that you're never going to use
anyway?  Unless it's a visible part of a general-purpose library, if you
find later that it needs to be an expression you can always go back and
change it.

[...]

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#15881

FromPhilip Lantz <prl@canterey.us>
Date2012-01-17 01:02 -0800
Message-ID<MPG.297ed91372934fba989686@news.eternal-september.org>
In reply to#15851
Keith Thompson wrote:
> Joe keane writes:
> > christian.bau <christian.bau@cbau.wanadoo.co.uk> wrote:
> >>You probably want to use the well-known trick do create a macro that
> >>is a single statement without semicolon like:
> >>
> >>#define LOG (level, msg) do { whatever (); } while (0)
> >
> > Silly.
> >
> > Of *course* you want a macro to be expression if that's easily done.
> >
> 
> Yes, if you write the macro as an expression, you can use it in more
> contexts.  But what if you happen to know that you're *never* going to
> need to use it other than in a statement context?  Why not write the
> macro definition in a more readable and maintainable form, when the only
> cost is the loss of some flexibility that you're never going to use
> anyway?

I think one of the main reasons for me is that using do ... while (0) in 
a macro is a hack, and I try to avoid using hacks except where 
necessary, and it is (in my experience) almost never necessary.

Philip

[toc] | [prev] | [next] | [standalone]


#15892

FromKeith Thompson <kst-u@mib.org>
Date2012-01-17 13:05 -0800
Message-ID<lnaa5myr89.fsf@nuthaus.mib.org>
In reply to#15881
Philip Lantz <prl@canterey.us> writes:
> Keith Thompson wrote:
>> Joe keane writes:
>> > christian.bau <christian.bau@cbau.wanadoo.co.uk> wrote:
>> >>You probably want to use the well-known trick do create a macro that
>> >>is a single statement without semicolon like:
>> >>
>> >>#define LOG (level, msg) do { whatever (); } while (0)
>> >
>> > Silly.
>> >
>> > Of *course* you want a macro to be expression if that's easily done.
>> 
>> Yes, if you write the macro as an expression, you can use it in more
>> contexts.  But what if you happen to know that you're *never* going to
>> need to use it other than in a statement context?  Why not write the
>> macro definition in a more readable and maintainable form, when the only
>> cost is the loss of some flexibility that you're never going to use
>> anyway?
>
> I think one of the main reasons for me is that using do ... while (0) in 
> a macro is a hack, and I try to avoid using hacks except where 
> necessary, and it is (in my experience) almost never necessary.

Fair enough.

But one could argue that translating if/else to ?: and ";" to "," for
the sole purpose of making a chunk of code usable as a macro is equally
hackish.

Sure, "do...while(0)" is a hack -- but it's also an idiom that's widely
used and recognized by C programmers.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#15896

FromIan Collins <ian-news@hotmail.com>
Date2012-01-18 13:12 +1300
Message-ID<9nmh3nFaaoU1@mid.individual.net>
In reply to#15892
On 01/18/12 10:05 AM, Keith Thompson wrote:
> Philip Lantz<prl@canterey.us>  writes:
>> Keith Thompson wrote:
>>> Joe keane writes:
>>>> christian.bau<christian.bau@cbau.wanadoo.co.uk>  wrote:
>>>>> You probably want to use the well-known trick do create a macro that
>>>>> is a single statement without semicolon like:
>>>>>
>>>>> #define LOG (level, msg) do { whatever (); } while (0)
>>>>
>>>> Silly.
>>>>
>>>> Of *course* you want a macro to be expression if that's easily done.
>>>
>>> Yes, if you write the macro as an expression, you can use it in more
>>> contexts.  But what if you happen to know that you're *never* going to
>>> need to use it other than in a statement context?  Why not write the
>>> macro definition in a more readable and maintainable form, when the only
>>> cost is the loss of some flexibility that you're never going to use
>>> anyway?
>>
>> I think one of the main reasons for me is that using do ... while (0) in
>> a macro is a hack, and I try to avoid using hacks except where
>> necessary, and it is (in my experience) almost never necessary.
>
> Fair enough.
>
> But one could argue that translating if/else to ?: and ";" to "," for
> the sole purpose of making a chunk of code usable as a macro is equally
> hackish.
>
> Sure, "do...while(0)" is a hack -- but it's also an idiom that's widely
> used and recognized by C programmers.

It is also a strong indication that whatever you are attempting to do is 
an inappropriate use of a macro!

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#15901

Fromdspfun <dspfun@hotmail.com>
Date2012-01-18 01:05 -0800
Message-ID<8a04a8ba-eee0-48fd-9cb6-98695875840e@f33g2000yqh.googlegroups.com>
In reply to#15896
On 18 Jan, 01:12, Ian Collins <ian-n...@hotmail.com> wrote:
> On 01/18/12 10:05 AM, Keith Thompson wrote:
>
>
>
>
>
>
>
>
>
> > Philip Lantz<p...@canterey.us>  writes:
> >> Keith Thompson wrote:
> >>> Joe keane writes:
> >>>> christian.bau<christian....@cbau.wanadoo.co.uk>  wrote:
> >>>>> You probably want to use the well-known trick do create a macro that
> >>>>> is a single statement without semicolon like:
>
> >>>>> #define LOG (level, msg) do { whatever (); } while (0)
>
> >>>> Silly.
>
> >>>> Of *course* you want a macro to be expression if that's easily done.
>
> >>> Yes, if you write the macro as an expression, you can use it in more
> >>> contexts.  But what if you happen to know that you're *never* going to
> >>> need to use it other than in a statement context?  Why not write the
> >>> macro definition in a more readable and maintainable form, when the only
> >>> cost is the loss of some flexibility that you're never going to use
> >>> anyway?
>
> >> I think one of the main reasons for me is that using do ... while (0) in
> >> a macro is a hack, and I try to avoid using hacks except where
> >> necessary, and it is (in my experience) almost never necessary.
>
> > Fair enough.
>
> > But one could argue that translating if/else to ?: and ";" to "," for
> > the sole purpose of making a chunk of code usable as a macro is equally
> > hackish.
>
> > Sure, "do...while(0)" is a hack -- but it's also an idiom that's widely
> > used and recognized by C programmers.
>
> It is also a strong indication that whatever you are attempting to do is
> an inappropriate use of a macro!
>
> --
> Ian Collins

What is the reason that this is an indication that the "do...while(0)"
in a macro is an inappropriate use of a macro?

Brs,
Markus

[toc] | [prev] | [next] | [standalone]


#15902

FromIan Collins <ian-news@hotmail.com>
Date2012-01-18 22:57 +1300
Message-ID<9nnjboFaaoU3@mid.individual.net>
In reply to#15901
On 01/18/12 10:05 PM, dspfun wrote:
> On 18 Jan, 01:12, Ian Collins<ian-n...@hotmail.com>  wrote:
>> On 01/18/12 10:05 AM, Keith Thompson wrote:

[what adds these random lines?]

>>> Philip Lantz<p...@canterey.us>    writes:
>>
>>>> I think one of the main reasons for me is that using do ... while (0) in
>>>> a macro is a hack, and I try to avoid using hacks except where
>>>> necessary, and it is (in my experience) almost never necessary.
>>
>>> Fair enough.
>>
>>> But one could argue that translating if/else to ?: and ";" to "," for
>>> the sole purpose of making a chunk of code usable as a macro is equally
>>> hackish.
>>
>>> Sure, "do...while(0)" is a hack -- but it's also an idiom that's widely
>>> used and recognized by C programmers.
>>
>> It is also a strong indication that whatever you are attempting to do is
>> an inappropriate use of a macro!
>>
> What is the reason that this is an indication that the "do...while(0)"
> in a macro is an inappropriate use of a macro?

Because in most cases it gets used where a function or as in your code, 
a macro wrapping a function, is more appropriate.  The rare exceptions 
are where macros are used as poor man's templates.

I guess the use of "do...while(0)" (and function-like macros in general) 
is a hang over from 80s and 90s compiler which didn't inline functions.

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#15904

From"osmium" <r124c4u102@comcast.net>
Date2012-01-18 06:22 -0600
Message-ID<9nnrr9FnusU1@mid.individual.net>
In reply to#15902
"Ian Collins" wrote:

> On 01/18/12 10:05 PM, dspfun wrote:
>> On 18 Jan, 01:12, Ian Collins<ian-n...@hotmail.com>  wrote:
>>> On 01/18/12 10:05 AM, Keith Thompson wrote:
>
> [what adds these random lines?]

That ever annoying PITA, Google groups adds those meaningless lines.

[toc] | [prev] | [next] | [standalone]


Page 1 of 3  [1] 2 3  Next page →

Back to top | Article view | comp.lang.c


csiph-web