By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,948 Members | 865 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,948 IT Pros & Developers. It's quick & easy.

Default parameters in macros... Is it possible?

P: n/a
Hi all,

I am upgrading a kind of small logging framework. Until now, the
interface for logging messages takes only a format string, and a
variable argument list (in a similiar way as printf()). The interface
to the code that uses the logging facilites is through macros, i.e. one
ogf the macros looks like this:

#define LOG(fmtString, ...) GlobalLogger.LogToFile(fmtString"\n",
##__VA_ARGS__)

Now I am introducing message types, filtering etc, and need to add one
more parameter to the macros. Of course I can include that parameter in
the variable argument list, but it is good the varaible argument list
to follow the format string. So my question is: Is there a way to
implement the notion of default parameter in the macros, so the new
macros are compatible with the old code that uses them. Any ideas?

Regards
Alex

Apr 13 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
avasilev wrote:
Hi all,

I am upgrading a kind of small logging framework. Until now, the
interface for logging messages takes only a format string, and a
variable argument list (in a similiar way as printf()). The interface
to the code that uses the logging facilites is through macros, i.e. one
ogf the macros looks like this:

#define LOG(fmtString, ...) GlobalLogger.LogToFile(fmtString"\n",
##__VA_ARGS__)

Now I am introducing message types, filtering etc, and need to add one
more parameter to the macros. Of course I can include that parameter in
the variable argument list, but it is good the varaible argument list
to follow the format string. So my question is: Is there a way to
implement the notion of default parameter in the macros, so the new
macros are compatible with the old code that uses them. Any ideas?

Regards
Alex


Currently, variable argument macros are not part of the C++ standard.
It is part of the C standard, and some compilers like GNU will let you
use variable argument macros in C++ code.

Check out the following logging library:
http://axter.com/ezlogger

The above library uses a temporary object to store some of the
parameters, and then uses the temporary object's methods to call a
variable argument function.
See following link for macro syntax:
http://axter.com/ezlogger/ezlogger__macros_8hpp.htm

Example:
#define EZDBGONLYLOGGERPRINT axter::ezlogger<>(__FILE__, __LINE__,
__FUNCTION__).cprint

To remove above logging from a release version of the code, you can use
the following method:
#define EZDBGONLYLOGGERPRINT if (1);else printf

Most compilers will compile away the above line complete from the
release version of the code.

This method is more portable then using variable argument macro.
----------------------------------------------------------------------------------------
David Maisonave
http://axter.com

Author of Axter's policy based smart pointers
(http://axter.com/smartptr)
Top ten member of C++ Expert Exchange:
http://www.experts-exchange.com/Cplusplus
----------------------------------------------------------------------------------------

Apr 13 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.