razael1 wrote:
>I am putting debugging messages into my program by putting blocks that
look like this:
#ifdef DEBUG
errlog << "Here is some information";
#endif
All these #ifdef blocks make the code bulky and harder to read, so I'd
like to do something where I put:
errMsg("Here is some information");
One way to do this is via macro abuse. There are other ways, with different
trade-offs. I would not use the following for the log of a server program.
That should be a complete subsystem.
Debugging traces must be absurdly easy to write, because you must belt them
out while in the throws of debugging, without their details distracting you.
So try this:
#ifndef DEBUG_
# ifdef _DEBUG // this helper makes irritations go away when we are not
debugging
# define DEBUG_(Q_) Q_
# else
# define DEBUG_(Q_)
# endif
#endif
That uses a platform that defines _DEBUG. You might also switch based on the
Standard !defined(NDEBUG ).
#ifdef _DEBUG
# define SOURCE__ __FILE__ "(" << __LINE__ << "): "
# define SPEW(x) (std::cout << SOURCE__ "TRACE - " #x ": " << x <<
std::endl)
#endif
That outputs a line in the same format as a Visual Studio error message
(following the coding practice "assertion failures are syntax errors").
At debug time, you can write this:
SPEW(bigObject << " shouldn't be " << stringFoo);
The operator<< will put anything into the trace, not just strings. Anything
with an operator<<.
The SPEW() disappears when you compile for release. This is a feature. It's
to remind you to take the SPEW() out, and not rely on it for too long.
If you need to commit your code with a SPEW() in it, use
DEBUG_(SPEW(wha tever)).
My question is how this would affect the performance of the program.
Your code, which I snipped, would be slow, not because the inside of the
function could go away, but because the calling site of the function could
assemble a string from parts, expensively.
In release mode, you could instead #define errMsg(x) as nothing.
Also, is there an elegant way to get rid of the "unused parameter"
warning.
error=error just inside the function. Or this:
void errMsg(string DEBUG_(error))
My evil ugly DEBUG_() macro allows you write code that is either slick, or
trick. Avoid the latter, because when debugging code differs too far from
production code, you could catch "debugitis" , which is bugs caused by
debugging too much.
/The C++ Programming Language, 3rd Ed/ by Bjarne S. contains an alternative
that uses a template, and is closer to a log system than a debugging trace
system. Also study that. I didn't, so it might improve my macro abuse.
--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!