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 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
> > > >
> > > >
> > > >
> > >
> >
> >
>
|