473,386 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

help needed in macro syntax

Hi,
I am trying to stub out debug log messages in my application, if
the logging subsystem is not enabled.. For e.g a invocation

#define LOGMSG !Logger::Enabled() ? false : Logger::LogMsg

so that
LOGMSG("Log Me\n");

will be a NO-OP if the Logger::Enabled() returns false, else the
message will be logged appropriately by the function call to
Logger::LogMsg().

My compilation fails with errors (g++ 3.3 compiler). I have seen
similar macro definition in another working program. Should I be
passing some compile options to g++ compiler to get this working?

#define LOGMSG !Logger::Enabled() ? false : Logger::LogMsg

Thank you in advance for help.
--Raghu
Jul 22 '05 #1
6 2233
"Raghuveer Pallikonda" <pa******@rediffmail.com> wrote in message
news:81**************************@posting.google.c om...
Hi,
I am trying to stub out debug log messages in my application, if
the logging subsystem is not enabled.. For e.g a invocation

#define LOGMSG !Logger::Enabled() ? false : Logger::LogMsg

so that
LOGMSG("Log Me\n");

will be a NO-OP if the Logger::Enabled() returns false, else the
message will be logged appropriately by the function call to
Logger::LogMsg().

My compilation fails with errors (g++ 3.3 compiler). I have seen
similar macro definition in another working program. Should I be
passing some compile options to g++ compiler to get this working?

#define LOGMSG !Logger::Enabled() ? false : Logger::LogMsg

Thank you in advance for help.


Why are macros necessary? Can't you just add that call to Logger::Enabled()
to Logger::LogMsg()?

If you need the macro, then wouldn't you want something like this?

#define LOGMSG(x) if (Logger::Enabled()) Logger::LogMsg((x));

I'm not a heavy macro user, so that may not be the best way to do it. Is
that ternary operator necessary? BTW, if you want better help, you should
post complete code that demonstrates the problem, and include the compiler's
messages in your post.

--
David Hilsee
Jul 22 '05 #2

"Raghuveer Pallikonda" <pa******@rediffmail.com> wrote in message
news:81**************************@posting.google.c om...
Hi,
I am trying to stub out debug log messages in my application, if
the logging subsystem is not enabled.. For e.g a invocation

#define LOGMSG !Logger::Enabled() ? false : Logger::LogMsg

so that
LOGMSG("Log Me\n");


So where are your macro parameters?

-Mike
Jul 22 '05 #3
Raghuveer Pallikonda wrote:
My compilation fails with errors (g++ 3.3 compiler).


What errors do you get? How is Logger::LogMsg defined?
Does your compiler complain because it cannot determine the correct
return type for the ?: operator? (e.g. the return type of Logger::LogMsg
is void?)

--
Regards,
Tobias
Jul 22 '05 #4
pa******@rediffmail.com (Raghuveer Pallikonda) wrote in message news:<81**************************@posting.google. com>...
Hi,
I am trying to stub out debug log messages in my application, if
the logging subsystem is not enabled.. For e.g a invocation

#define LOGMSG !Logger::Enabled() ? false : Logger::LogMsg

so that
LOGMSG("Log Me\n");


Hmm...how about:

#define LOGMSG(MSG) (Logger::Enabled() && Logger::LogMsg(MSG))

I'm feeling too lazy at the moment to put together enough code to test
it with, but if at least if I'm understanding your intent correctly, I
believe it should work.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #5
I apologize for posting in haste. The OP syntax compiles fine. I am
still a bit puzzled as to why the OP syntax works ( example code
below)

#include <iostream>

#define IDIAG !LogEnabled() ? false : printf

bool LogEnabled() { return false; }

int main() {
IDIAG("this is to be logged\n");
return 0;
}
and not this

#include <iostream>

#define IDIAG false

int main() {
IDIAG("this is to be logged\n");
return 0;
}
$ g++ -o testfalse testfalse.cc
testfalse.cc: In function `int main()':
testfalse.cc:6: error: `false' cannot be used as a function
My understanding of Macro definitions is that it is a inplace
substitution. So that in both examples above the line IDIAG("this is
to be logged\n"); is actually translated to false("this is to be
logged\n");

I guess my understanding is incorrect.. so would greatly appreciate to
be corrected and enlightened,

Thank you for all the replies and your valuable time.

Regards,
--Raghu

pa******@rediffmail.com (Raghuveer Pallikonda) wrote in message news:<81**************************@posting.google. com>...
Hi,
I am trying to stub out debug log messages in my application, if
the logging subsystem is not enabled.. For e.g a invocation

#define LOGMSG !Logger::Enabled() ? false : Logger::LogMsg

so that
LOGMSG("Log Me\n");

will be a NO-OP if the Logger::Enabled() returns false, else the
message will be logged appropriately by the function call to
Logger::LogMsg().

My compilation fails with errors (g++ 3.3 compiler). I have seen
similar macro definition in another working program. Should I be
passing some compile options to g++ compiler to get this working?

#define LOGMSG !Logger::Enabled() ? false : Logger::LogMsg

Thank you in advance for help.
--Raghu

Jul 22 '05 #6
Raghuveer Pallikonda wrote:
I apologize for posting in haste. The OP syntax compiles fine. I am
still a bit puzzled as to why the OP syntax works ( example code
below)
Well, just substitute the 'IDIAG' token with what the macro will
expand into:

#include <iostream>

#define IDIAG !LogEnabled() ? false : printf

bool LogEnabled() { return false; }

int main() {
IDIAG("this is to be logged\n");
Expands into

!LogEnabled() ? false : printf ("this is to be logged\n");
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ~~~~~~~~~~~~~~~~~~~~~~~~~
This is the actual substitution This was already there

(I put a space between the substitution and the parentheses for more
clarity) which is a statement that contains a single expression with
a ternary operator. Similar to

if (!LogEnabled()) false;
else printf("this is to be logged\n");
return 0;
}
and not this

#include <iostream>

#define IDIAG false

int main() {
IDIAG("this is to be logged\n");
And what this expands into?

false("this is to be logged\n");

Nonsense, isn't it?
return 0;
}
$ g++ -o testfalse testfalse.cc
testfalse.cc: In function `int main()':
testfalse.cc:6: error: `false' cannot be used as a function
My understanding of Macro definitions is that it is a inplace
substitution. So that in both examples above the line IDIAG("this is
to be logged\n"); is actually translated to false("this is to be
logged\n");
Nope.

I guess my understanding is incorrect.. so would greatly appreciate to
be corrected and enlightened,


See above

Victor
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: Andrew Dalke | last post by:
Here's a proposed Q&A for the FAQ based on a couple recent threads. Appropriate comments appreciated X.Y: Why doesn't Python have macros like in Lisp or Scheme? Before answering that, a...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
28
by: stu_gots | last post by:
I have been losing sleep over this puzzle, and I'm convinced my train of thought is heading in the wrong direction. It is difficult to explain my circumstances, so I will present an identical...
6
by: Dos Lil | last post by:
I have a comobox in the main form which has a query attached to it to list the employee numbers.I have written this code to requry the control in the control's after update event Private Sub...
15
by: Buck Rogers | last post by:
Hi guys! Task 1: Write a program which presents a menu with 5 options. The 5th option quits the program. Each option should execute a system command using system(). Below is my humble...
3
by: Dan Trowbridge | last post by:
Hi everyone, In my attempt to port code from VS 6.0 to VS.NET I had some code break along the way, mostly due to not adhereing closely to the C++ standard. This may be another instance but I...
2
by: Erik | last post by:
Hi Everyone, I'm having real problems compiling some source for eVC4++. The errors I am getting are below: It all seems to be centred around winsock. If I move the afsock.h reference to before...
4
by: Elaine | last post by:
Hi I have created a form which contains a subform. To cut a long story short, I have created a macro that moves the focus to the subform (GoToControl) and sets a value (SetValue) in the field...
6
by: Takeadoe | last post by:
Dear NG, Can someone assist me with writing the little code that is needed to run an update table query each time the database is opened? From what I've been able to glean from this group, the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.