chaining statements without a semicolon

J

jcktmp

I currently have a macro for logging with a definition similar to the
following:

#define LOG shouldLog() && logStream

so that statements of this type may be written:

LOG << "some logging << endl;

I would like to add a static variable to this macro, so that the macro
expands to:

static LogObject x; x.shouldLog() && logStream

The problem is that with control statements that optionally use braces
(if, while, etc.), the multi statement approach doesn't work. Is there
any way to rewrite this (without adding a brace to the log macro and
creating an END macro) so that everything can be done in LOG?

Thanks,
J
 
T

tony_in_da_uk

#define LOG shouldLog() && logStream

I would like to add a static variable to this macro, so that the macro
expands to:

static LogObject x; x.shouldLog() && logStream

The problem is that with control statements that optionally use braces
(if, while, etc.), the multi statement approach doesn't work. Is there
any way to rewrite this (without adding a brace to the log macro and
creating an END macro) so that everything can be done in LOG?

How would that work? If you have two LOG lines in the same scope,
then you declare two variables "x", which won't compile....

Tony
 
J

James Kanze

I currently have a macro for logging with a definition similar to the
following:
#define LOG shouldLog() && logStream
so that statements of this type may be written:
LOG << "some logging << endl;
I would like to add a static variable to this macro, so that the macro
expands to:
static LogObject x; x.shouldLog() && logStream
The problem is that with control statements that optionally use braces
(if, while, etc.), the multi statement approach doesn't work. Is there
any way to rewrite this (without adding a brace to the log macro and
creating an END macro) so that everything can be done in LOG?

That's not going to work anyway; you'd need a separate name
for the static variable for each invocation of the macro.
(Of course, something like "static LogObject x ## __LINE_ ;"
might be a solution.) And of course, executing the
constructor the first time you hit the statement in control
flow is going to take time.

What problem are you trying to solve? I've used a static
log object per file in the past, requiring the user to
invoke a macro at global scope to declare it, and referring
to it in the LOG macro. In practice, for small
applications, it isn't necessary; you just use one global
object. And for large applications, the macro which
declares the local object requires an argument, specifying
the subsystem, so you can configure the log differently for
different subsystems.
 
J

Joe Greer

(e-mail address removed) wrote in 57g2000hsv.googlegroups.com:
I currently have a macro for logging with a definition similar to the
following:

#define LOG shouldLog() && logStream

so that statements of this type may be written:

LOG << "some logging << endl;

I would like to add a static variable to this macro, so that the macro
expands to:

static LogObject x; x.shouldLog() && logStream

The problem is that with control statements that optionally use braces
(if, while, etc.), the multi statement approach doesn't work. Is there
any way to rewrite this (without adding a brace to the log macro and
creating an END macro) so that everything can be done in LOG?

Thanks,
J

That won't work, but you can use the singleton pattern. That is:

#define LOG LogObject::Instance().ShouldLog && LogObject::Instance
().logStream

would work.

Depending upon how you implement your Instance() method, you could have
one per file or one per application.

joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top