Re: [Dev-C++] #define a "function" with variable number of parameters
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2005-12-19 13:39:10
|
You do not want to use a solution where your source code contains
mismatched parentheses. Syntax highlighting, and in some cases
auto-indent, not to mention lint etc will dislike such code something
grievously...
/Per W
On Mon, 19 Dec 2005, Minds Work wrote:
> Hi Daneil,
>
> My goal is only to write less code, but i like your ideia, so I will use
> something like this:
> #define LOG_INFO_(log) if(log->isInfoEnabled()) log->(__FILE__, __LINE__,
> #define LOG_INFO LOG_INFO_(LOG)
>
> #define LOG_DEBUG_(log) if(log->isDebugEnabled()) log->debug(__FILE__,
> __LINE__,
> #define LOG_DEBUG LOG_DEBUG_(LOG)
>
> And use as:
>
> LOG_DEBUG "x =3D %d", 2);
>
> LOG_INFO "%s", "this is a log message");
>
> PS: I already use a log library :)
>
>
> On 12/18/05, Daniel K. O. <dan...@ya...> wrote:
> >
> > Per Westermark wrote:
> >
> > >Given the billions of lines of existing code without use of __VA_ARGS_=
_,
> > >it really isn't such an important function that it is worth it to igno=
re
> > >the current language standard.
> > >
> > >
> > Agreed. I was just trying to answer the original question, but I also
> > doubt that it's really needed.
> > As Minds Work didn't specify what problem he's trying to solve, I assum=
e
> > that the example posted could be easily solved with Per's first sugesti=
on.
> >
> > Something like this:
> >
> >
> > -----
> >
> > #include <cstdarg>
> > #include <cstdio>
> >
> > #define LOG log_prefix(__FILE__, __LINE__); log
> >
> > void log_prefix(const char *file, int line)
> > {
> > printf("%s:%d:\t", file, line);
> > }
> >
> >
> > // here is Per's code
> > void log(const char *msg,...) {
> > va_list ap;
> >
> > va_start(ap,msg);
> > vprintf(msg,ap);
> > va_end(ap);
> > }
> >
> > int main() {
> > LOG("test %u %u\n",1,2);
> > return 0;
> > }
> >
> > -----
> >
> >
> > PS: There's also the C++ way to solve it, using ostreams and operator<<=
,
> > that obviously won't need a variable number of parameters.
> > PS2: if the problem is really just logging, there are tons of C++
> > libraries that do just this - conveniently. Google a bit, maybe you can
> > use one of them for your needs.
> >
> >
> > ---
> > Daniel K. O.
> >
> >
> >
> >
> >
> >
> > _______________________________________________________
> > Yahoo! doce lar. Fa=E7a do Yahoo! sua homepage.
> > http://br.yahoo.com/homepageset.html
> >
> >
> >
>
|