From owner-freebsd-chat Fri Feb 25 21: 6:39 2000 Delivered-To: freebsd-chat@freebsd.org Received: from cc942873-a.ewndsr1.nj.home.com (cc942873-a.ewndsr1.nj.home.com [24.2.89.207]) by hub.freebsd.org (Postfix) with ESMTP id 526A737BBC9 for ; Fri, 25 Feb 2000 21:06:36 -0800 (PST) (envelope-from cjc@cc942873-a.ewndsr1.nj.home.com) Received: (from cjc@localhost) by cc942873-a.ewndsr1.nj.home.com (8.9.3/8.9.3) id AAA20782; Sat, 26 Feb 2000 00:11:22 -0500 (EST) (envelope-from cjc) Date: Sat, 26 Feb 2000 00:11:22 -0500 From: "Crist J. Clark" To: Marco Molteni Cc: freebsd-chat@FreeBSD.ORG Subject: Re: how to do this C preprocessor trick? Message-ID: <20000226001121.A20702@cc942873-a.ewndsr1.nj.home.com> Reply-To: cjclark@home.com References: <20000225182432.A5017@sofia.csl.sri.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 1.0i In-Reply-To: <20000225182432.A5017@sofia.csl.sri.com>; from molter@csl.sri.com on Fri, Feb 25, 2000 at 06:24:32PM -0800 Sender: owner-freebsd-chat@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.org On Fri, Feb 25, 2000 at 06:24:32PM -0800, Marco Molteni wrote: > Hi all, > > I have a function that takes a variable number of arguments: > > void d_printf(const char *format, ...) > > I would like to make it print automatically the function name > from which it is called, eg instead of doing > > f() { d_printf("f: blabla", x, y, z); } > > doing simply > > f() { d_printf("blabla", x, y, z); } > > To do that, I though of wrapping d_printf() around a macro like > > #define dprintf(x) d_printf(__FUNCTION__, x) > > but whatever combination I use (also with #), the thing is not going to work: > > main.c:231: macro `d_printf' used with too many (4) args > > Is it possible to trick the C preprocessor to do what I want? Yeah, I use the same type of thing to produce error messages. I'm having a little bit of trouble understanding exactly what you are trying to do above, so I'll just show my solution to my problem. I wanted to just be able to do, errmsg(char fmt, ...) But have it print, cmd(file:line)- Error message Where 'cmd' is the name of the program (the tail of argv[0]), 'file' is the C source file name, and 'num' is the line number. char *cmd void _errmsg(char *fmt, ... ) { va_list ap; va_start(ap,fmt); vfprintf(stderr,fmt,ap); va_end(ap); } #define errmsg fprintf(stderr,"%s(%s:%d)- ",cmd,__FILE__,__LINE__); _errmsg Gets me around the varargs in the precompiler by not using _any_ args in the macro. So, errmsg("cannot fine file: %s\n",str); Expands to, fprintf(stderr,"%s(%s:%d)- ",cmd,__FILE__,__LINE__); _errmsg("cannot fine file: %s\n",str); And you know, it works. Big help in debugging big apps. When it's sent bound for users, I make the messages a bit less verbose, but only takes the one change. -- Crist J. Clark cjclark@home.com To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-chat" in the body of the message