Let's say I want to have several types of output messages in my code. One of them is DEBUG
, which is printed only, when the code is compiled in Debug mode.
Usually I'd have to write something like
#ifdef DEBUG
std::cout << "Debug message" << std::endl;
#endif
which is pretty cumbersome and annoying to use in many places.
Is it a good practice to define a macro for the code snippet, so you'd use it this way?
MSG_DEBUG("Debug message")
Or is there any other, more elegant way how to deal with it without macros? I'm interested about possible solutions both in C and C++, as I'm using both languages in different projects.
constexpr if
in C++, for instance.STDERR
. Also, why not make it depend onNDEBUG
likeassert()
does instead? Then you could define it like#define DEBUG_MSG(MSG) assert(std::cerr << MSG)
, which tests the stream-state too.