I want to write a macro that produces debug output and has a variable
number of arguments, so that I can use like this:
int i;
char *s;
DBG("simple message\n");
DBG("message with an int argument, value is %d\n", i);
DBG("two arguments: int %d, strings %s\n", i, s);
The output should be a fixed prefix, say "DEBUG ", followed by the
function name the macros is used in, and then by the string passed to
the macro, including further arguments.
The best solution I have found so far, is
#define DBG(fmt, ...) printf("DEBUG %s:" fmt, __func__, __VA_ARGS__)
This works as intended as long as there is at least one argument
following the format strings, but it expands to
printf("DEBUG %s:" fmt, __func__,) when there is no argument and that
causes a syntax error.
The draft of C99 I have here, disallows to give no arguments for the
ellipsis ... as stated in 6.10.3:
[#4] If the identifier-list in the macro definition does not end
with an ellipsis, the number of arguments, including those
arguments consisting of no preprocessing tokens, in an invocation
of a function-like macro shall agree with the number of parameters
in the macro definition. Otherwise, there shall be more arguments
in the invocation than there are parameters in the macro
definition (excluding the ...). There shall exist a )
preprocessing token that terminates the invocation.
Is there a better way to write the DBG() macro to avoid this?
urs 15 14278
Urs Thuermann <ur*@isnogud.es cape.de> writes: I want to write a macro that produces debug output and has a variable number of arguments, so that I can use like this:
int i; char *s;
DBG("simple message\n"); DBG("message with an int argument, value is %d\n", i); DBG("two arguments: int %d, strings %s\n", i, s);
The output should be a fixed prefix, say "DEBUG ", followed by the function name the macros is used in, and then by the string passed to the macro, including further arguments.
The best solution I have found so far, is
#define DBG(fmt, ...) printf("DEBUG %s:" fmt, __func__, __VA_ARGS__)
<SNIP> Is there a better way to write the DBG() macro to avoid this?
There was a related thread a couple of weeks ago:
<http://groups.google.c o.uk/group/comp.lang.c/browse_frm/thread/ac0bda529a91d75 4/949239c624c9fe7 1?lnk=st&q=logg ing+group%3Acom p.lang.c&rnum=3 &hl=en#949239c6 24c9fe71>
This basically used a *function* instead of a macro. The logging
function accepts variadic arguments, and these can then be modified
and passed on to a varargs form of printf (e.g. vfprintf).
--
John Devereux
On 2006-03-27, John Devereux <jd******@THISd evereux.me.uk> wrote: Urs Thuermann <ur*@isnogud.es cape.de> writes:
I want to write a macro that produces debug output and has a variable number of arguments, so that I can use like this:
int i; char *s;
DBG("simple message\n"); DBG("message with an int argument, value is %d\n", i); DBG("two arguments: int %d, strings %s\n", i, s);
The output should be a fixed prefix, say "DEBUG ", followed by the function name the macros is used in, and then by the string passed to the macro, including further arguments.
The best solution I have found so far, is
#define DBG(fmt, ...) printf("DEBUG %s:" fmt, __func__, __VA_ARGS__) <SNIP> Is there a better way to write the DBG() macro to avoid this?
There was a related thread a couple of weeks ago:
<http://groups.google.c o.uk/group/comp.lang.c/browse_frm/thread/ac0bda529a91d75 4/949239c624c9fe7 1?lnk=st&q=logg ing+group%3Acom p.lang.c&rnum=3 &hl=en#949239c6 24c9fe71>
This basically used a *function* instead of a macro. The logging function accepts variadic arguments, and these can then be modified and passed on to a varargs form of printf (e.g. vfprintf).
One little thing you might consider at an early stage : have an
importance or category enum as the first/second parameter : very
useful for filtering/enabling/disabling runtime logging for different
subsets in a non trivial system. You might even use the function name
and have settings to enable/disable logging for the particular function.
> This works as intended as long as there is at least one argument following the format strings, but it expands to printf("DEBUG %s:" fmt, __func__,) when there is no argument and that causes a syntax error.
#define macroFunc(a,b,. ..) ( Func(a, b, ##__VA_ARGS__) )
John Devereux wrote: Urs Thuermann <ur*@isnogud.es cape.de> writes:
I want to write a macro that produces debug output and has a variable number of arguments, so that I can use like this:
int i; char *s;
DBG("simple message\n"); DBG("message with an int argument, value is %d\n", i); DBG("two arguments: int %d, strings %s\n", i, s);
The output should be a fixed prefix, say "DEBUG ", followed by the function name the macros is used in, and then by the string passed to the macro, including further arguments.
The best solution I have found so far, is
#define DBG(fmt, ...) printf("DEBUG %s:" fmt, __func__, __VA_ARGS__) <SNIP> Is there a better way to write the DBG() macro to avoid this?
There was a related thread a couple of weeks ago:
<http://groups.google.c o.uk/group/comp.lang.c/browse_frm/thread/ac0bda529a91d75 4/949239c624c9fe7 1?lnk=st&q=logg ing+group%3Acom p.lang.c&rnum=3 &hl=en#949239c6 24c9fe71>
This basically used a *function* instead of a macro. The logging function accepts variadic arguments, and these can then be modified and passed on to a varargs form of printf (e.g. vfprintf).
See this one too: http://groups.google.co.uk/group/com...d17634c1ed7065
--
==============
Not a pedant
==============
raxip schrieb: This works as intended as long as there is at least one argument following the format strings, but it expands to printf("DEB UG %s:" fmt, __func__,) when there is no argument and that causes a syntax error.
#define macroFunc(a,b,. ..) ( Func(a, b, ##__VA_ARGS__) )
This is "gcc-C".
It is not valid C99, though.
-Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Assumptions are wonderous creatures.
raxip wrote: Assumptions are wonderous creatures.
That's nice. What does it have to do with anything? See my .sig below
for valuable information.
Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
> That's nice. What does it have to do with anything? See my .sig below for valuable information.
Is this better?
raxip wrote: That's nice. What does it have to do with anything? See my .sig below for valuable information.
Is this better?
Somewhat. Where are the attributions (the who said what part)? I know
that Google puts them in if you use the correct Reply.
Brian This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: jois.de.vivre |
last post by:
Hi,
I'm writing a C++ program that uses C style macros for debugging
purposes. I have code that looks something like this:
#define DEBUG(str, ...) Show(__FILE__, __LINE__, str, ## __VA_ARGS__);
void Show(const char* file, int line, const char* display, ...)
{
|
by: Augustus S.F.X Van Dusen |
last post by:
I have recently come across the following construction:
#define P_VAR(output, string, args...) \
fprintf (output, "This is "string"\n", ##args)
which can be invoked as follows:
int x = 1, y = 2 ;
char * str = "String" ;
|
by: Trent Buck |
last post by:
(Note: C99 supports variadic macros, but C89 does not.)
I'm pretty sure what I'm trying to do is impossible, but I'll ask here
in case I'm missing something.
I'm trying to define generic, variadic arithmetic and boolean operators.
For example,
product (2, sum (3, 4), 5);
|
by: Michael B Allen |
last post by:
If I define a variadic macro like say:
#define PRINT(fmt, ...) _myprintf(__FILE__ ": " fmt, __VA_ARGS__)
and I call this like:
PRINT("no args");
the preprocessor generates:
|
by: Thomas Carter |
last post by:
I understand that C99 supports variadic macros. However, is it not the
case that a variadic macro defined as
#define SAMPLE_MACRO(...) Bloody-blah
must take at least one argument? I would be interested to allow for no
arguments at all. Is this possible?
| |
by: Laurent Deniau |
last post by:
I was playing a bit with the preprocessor of gcc (4.1.1). The following
macros expand to:
#define A(...) __VA_ARGS__
#define B(x,...) __VA_ARGS__
A() -nothing, *no warning*
A(x) -x
B() -nothing, *warning ISO C99 requires rest arguments to be used*
|
by: Christof Warlich |
last post by:
Hi,
is there any way to access individual elements in the body
of a variadic macro? I only found __VA_ARGS__, which always
expands to the complete list.
Thanks for any help,
Christof
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |