Thread: [Dev-C++] #define a "function" with variable number of parameters
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Minds W. <min...@gm...> - 2005-12-18 01:42:13
|
Hi,
I know we can #define "functions" in C/C++, like this :
#define LOG_INFO(msg) if(LOG->isInfoEnabled()) { LOG->info(__FILE__,
__LINE__, msg);}
We use this #define to simplify the LOG code.
But now we change the info method to receive a variable number of parameter=
s
(so the info can used like the printf function), the info signature changed
from:
void info(const* char file, int line, const char* msg);
to:
void info(const* char file, int line, const char* msg, ...);
Is there a way to change the #define LOG_INFO to receive a variable number
of parameters to use the new LOG->info method?
[]s
Freddy
|
|
From: Daniel K. O. <dan...@ya...> - 2005-12-18 05:34:18
|
Minds Work wrote: > Is there a way to change the #define LOG_INFO to receive a variable > number of parameters to use the new LOG->info method? See: http://david.tribble.com/text/cdiffs.htm#C99-pp-vararg --- Daniel K. O. _______________________________________________________ Yahoo! doce lar. Faça do Yahoo! sua homepage. http://br.yahoo.com/homepageset.html |
|
From: Per W. <pw...@ia...> - 2005-12-18 09:43:46
|
Sometimes it's possible to use a #define that doesn't take any parameters:
#include <cstdarg>
#include <cstdio>
#define LOG log
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;
}
/Per W
On Sun, 18 Dec 2005, Daniel K. O. wrote:
> Minds Work wrote:
>
> > Is there a way to change the #define LOG_INFO to receive a variable
> > number of parameters to use the new LOG->info method?
>
>
> See:
> http://david.tribble.com/text/cdiffs.htm#C99-pp-vararg
>
>
> ---
> Daniel K. O.
>
>
>
>
>
>
> _______________________________________________________
> Yahoo! doce lar. Fa=E7a do Yahoo! sua homepage.
> http://br.yahoo.com/homepageset.html
>
>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: Splunk Inc. Do you grep through log fi=
les
> for problems? Stop! Download the new AJAX search engine that makes
> searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
> http://ads.osdn.com/?ad_id=3D7637&alloc_id=3D16865&op=3Dclick
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: Minds W. <min...@gm...> - 2005-12-18 13:09:07
|
Hi Daniel, Thanks for the link. That page said that MACRO functions with variable number of arguments are only available in C99 and in some C++ compilers (but not in my compiler :-( ) So I cant use MACRO Functions with variable number of arguments, but is there a way to se use MACRO FUNCTIONS with default args? (like; void foo(const char* msg, int b =3D 0)) On 12/18/05, Daniel K. O. <dan...@ya...> wrote: > > Minds Work wrote: > > > Is there a way to change the #define LOG_INFO to receive a variable > > number of parameters to use the new LOG->info method? > > > See: > http://david.tribble.com/text/cdiffs.htm#C99-pp-vararg > > > --- > Daniel K. O. > > > > > > > _______________________________________________________ > Yahoo! doce lar. Fa=E7a do Yahoo! sua homepage. > http://br.yahoo.com/homepageset.html > > > |
|
From: Per W. <pw...@ia...> - 2005-12-18 17:59:29
|
No default arguments possible. /Per W On Sun, 18 Dec 2005, Minds Work wrote: > Hi Daniel, > > Thanks for the link. > > That page said that MACRO functions with variable number of arguments are > only available in C99 and in some C++ compilers (but not in my compiler := -( > ) > > So I cant use MACRO Functions with variable number of arguments, but is > there a way to se use MACRO FUNCTIONS with default args? > (like; void foo(const char* msg, int b =3D 0)) > > On 12/18/05, Daniel K. O. <dan...@ya...> wrote: > > > > Minds Work wrote: > > > > > Is there a way to change the #define LOG_INFO to receive a variable > > > number of parameters to use the new LOG->info method? > > > > > > See: > > http://david.tribble.com/text/cdiffs.htm#C99-pp-vararg > > > > > > --- > > Daniel K. O. > > > > > > > > > > > > > > _______________________________________________________ > > Yahoo! doce lar. Fa=E7a do Yahoo! sua homepage. > > http://br.yahoo.com/homepageset.html > > > > > > > |
|
From: Daniel K. O. <dan...@ya...> - 2005-12-18 20:07:29
|
Minds Work wrote: > That page said that MACRO functions with variable number of arguments > are only available in C99 and in some C++ compilers (but not in my > compiler :-( ) Well, if you aren't using Dev-C++/MinGW maybe you'll need some other non-standard extension provided by your compiler vendor. MinGW/GCC accepts __VA_ARGS__ just fine on C++. --- Daniel K. O. _______________________________________________________ Yahoo! doce lar. Faça do Yahoo! sua homepage. http://br.yahoo.com/homepageset.html |
|
From: Per W. <pw...@ia...> - 2005-12-18 21:49:07
|
But then again - do _not ever_ use a vendor-specific extension unless you are _absolutely 100% sure_ that the code need _never ever_ be ported to a different compiler. Until the official standard have incorporated __VA_ARGS__ support (and more than one vendor have implemented it according to the standard), it really should not be used. 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 ignore the current language standard. /Per W On Sun, 18 Dec 2005, Daniel K. O. wrote: > Minds Work wrote: > > > That page said that MACRO functions with variable number of arguments > > are only available in C99 and in some C++ compilers (but not in my > > compiler :-( ) > > > Well, if you aren't using Dev-C++/MinGW maybe you'll need some other > non-standard extension provided by your compiler vendor. > MinGW/GCC accepts __VA_ARGS__ just fine on C++. > > > --- > Daniel K. O. |
|
From: Daniel K. O. <dan...@ya...> - 2005-12-19 03:37:51
|
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 ignore
>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 assume
that the example posted could be easily solved with Per's first sugestion.
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ça do Yahoo! sua homepage.
http://br.yahoo.com/homepageset.html
|
|
From: Minds W. <min...@gm...> - 2005-12-19 12:21:13
|
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 ignore
> >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 assume
> that the example posted could be easily solved with Per's first sugestion=
.
>
> 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
>
>
>
|
|
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
> >
> >
> >
>
|
|
From: Minds W. <min...@gm...> - 2005-12-19 13:45:45
|
I know it is not a beatiful solution...
(but its the one tha will works as we want....)
On 12/19/05, Per Westermark <pw...@ia...> wrote:
>
> 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 us=
e
> > 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
> ignore
> > > >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
> assume
> > > that the example posted could be easily solved with Per's first
> sugestion.
> > >
> > > 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
> > >
> > >
> > >
> >
>
>
|
|
From: Per W. <pw...@ia...> - 2005-12-19 18:41:09
|
You really shouldn't prioritize your coding (typing) quantity in such ways
that you violate the integrity of the source code. There is no way you can
maintain such code in the long run.
Most companies with software development knowledge would give you a
warning: if you want to keep your job, code properly!
By the way, never think that the world allows just a single solution to a
problem. Here is a quick - but ugly - solution that makes sure that the
"call" still looks reasonable.
#include <cstdarg>
#include <cstdio>
struct Log {
const char* m_fname;
int m_line;
bool m_logging;
Log() {
m_logging =3D true;
m_fname =3D NULL;
m_line =3D 0;
}
Log& init(const char* fname,int line) {
m_fname =3D fname;
m_line =3D line;
return *this;
}
void log(const char* msg,...) {
if (m_logging) {
char buf[1000];
va_list ap;
va_start(ap,msg);
vsnprintf(buf,sizeof(buf),msg,ap);
va_end(ap);
printf("My message: %s:%d: %s\n",m_fname,m_line,buf);
}
}
};
#define LOG log.init(__FILE__,__LINE__).log
Log log;
int main() {
LOG("hello %d %d",1,2);
return 0;
}
It is possible to extend the solution to make it thread-safe too, i.e.
make sure that multiple threads will play with individual instances of the
m_fname and m_line members.
/Per W
On Mon, 19 Dec 2005, Minds Work wrote:
> I know it is not a beatiful solution...
> (but its the one tha will works as we want....)
>
>
> On 12/19/05, Per Westermark <pw...@ia...> wrote:
> >
> > 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
> > ignore
> > > > >the current language standard.
> > > > >
> > > > >
> > > > Agreed. I was just trying to answer the original question, but I al=
so
> > > > doubt that it's really needed.
> > > > As Minds Work didn't specify what problem he's trying to solve, I
> > assume
> > > > that the example posted could be easily solved with Per's first
> > sugestion.
> > > >
> > > > 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
> > > >
> > > >
> > > >
> > >
> >
> >
>
|
|
From: Josh M. <mrl...@gm...> - 2005-12-19 22:00:32
|
What compiler are you using that doesn't allow syntax like:
#define error(s,...) { fprintf(stderr,"ERROR %s:%d -
"s"\n",__FILE__,__LINE__, ##__VA_ARGS__); fflush(stderr); }
#define debug(s,...) { fprintf(stdout,s"\n",##__VA_ARGS__); }
On 12/19/05, Minds Work <min...@gm...> wrote:
>
>
> I know it is not a beatiful solution...
> (but its the one tha will works as we want....)
>
>
> On 12/19/05, Per Westermark <pw...@ia...> wrote:
> >
> > 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
> > ignore
> > > > >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
> > assume
> > > > that the example posted could be easily solved with Per's first
> > sugestion.
> > > >
> > > > 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
> > > >
> > > >
> > > >
> > >
> >
> >
>
--
Cheers,
Josh
PGP: http://revvy.box43.net/Josh_Matthews.asc
|
|
From: Per W. <pw...@ia...> - 2005-12-19 23:16:44
|
Probably a lot more non-gcc compilers than you think...
/Per W
On Mon, 19 Dec 2005, Josh Matthews wrote:
> What compiler are you using that doesn't allow syntax like:
>
> #define error(s,...) { fprintf(stderr,"ERROR %s:%d -
> "s"\n",__FILE__,__LINE__, ##__VA_ARGS__); fflush(stderr); }
> #define debug(s,...) { fprintf(stdout,s"\n",##__VA_ARGS__); }
>
> On 12/19/05, Minds Work <min...@gm...> wrote:
> >
> >
> > I know it is not a beatiful solution...
> > (but its the one tha will works as we want....)
> >
> >
> > On 12/19/05, Per Westermark <pw...@ia...> wrote:
> > >
> > > 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 wil=
l
> > > 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 t=
o
> > > ignore
> > > > > >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
> > > assume
> > > > > that the example posted could be easily solved with Per's first
> > > sugestion.
> > > > >
> > > > > 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 y=
ou
> > > 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
> > > > >
> > > > >
> > > > >
> > > >
> > >
> > >
> >
>
>
> --
> Cheers,
> Josh
>
> PGP: http://revvy.box43.net/Josh_Matthews.asc
>
|