Adding debug message

M

mthread

Hi,
I am new to C++, But has good experience in C. In C I have used
macros to enable / disable debug statements in the code. For ex :

#if defined ENABLE_DEBUG
#define Printf1(arg) printf(arg)
#else
#define Printf1(arg)
#endif

int main()
{
Printf1("This is a debug statement\n");
}

By defining/not-defining ENABLE_DEBUG I can enable / disable the debug
statements.


I would like to know what is the kind of standard used in C++ to
enable / disable the debug statements(ie cout statements).
 
A

Alf P. Steinbach

* mthread:
Hi,
I am new to C++, But has good experience in C. In C I have used
macros to enable / disable debug statements in the code. For ex :

#if defined ENABLE_DEBUG
#define Printf1(arg) printf(arg)
#else
#define Printf1(arg)
#endif

int main()
{
Printf1("This is a debug statement\n");
}

By defining/not-defining ENABLE_DEBUG I can enable / disable the debug
statements.


I would like to know what is the kind of standard used in C++ to
enable / disable the debug statements(ie cout statements).

In both C and C++ the standard (only standard) is the macro 'NDEBUG',
used by the 'assert' macro.

With 'NDEBUG' defined, 'assert' does nothing.


Cheers, & hth.,

- Alf
 
C

Christopher

mthread said:
Hi,
I am new to C++, But has good experience in C. In C I have used
macros to enable / disable debug statements in the code. For ex :

#if defined ENABLE_DEBUG
#define Printf1(arg) printf(arg)
#else
#define Printf1(arg)
#endif

int main()
{
Printf1("This is a debug statement\n");
}

By defining/not-defining ENABLE_DEBUG I can enable / disable the debug
statements.


I would like to know what is the kind of standard used in C++ to
enable / disable the debug statements(ie cout statements).

I don't know about "standard" way of doing it, because it really depends
what you want. You can accomplish the same outcome in C++ as you did in C by
simply replacing printf with std::cout << statements. Although that makes
your code rather messy. If you are looking for more, than it depends what
you are looking for.

You could explore the use of std::clog which, as I understand it was a
stream that was added in C++, to the standard input (cin) standard output
(cout) and standard error (cerr) streams from C. However, I imagine its
usefulness is limited.

If you want a more elaborate solution. I can tell you I am working on a
Windows specific Log class, which creates a file stream and custom stream
along with a custom stream buffer class, every time it is instantiated.
These are in turn registered with a singleton Logger class that manages
them. Its custom stream buffer is responsible for comminicating with the
Logger Window. The Logger Window comminicates to its children, which
represent each log. The custom stream and custom stream buffer allow me to
do things like color code logged text by type (debug, info, warn, fail,
etc.), seperate thread output to seperate child windows, and other nifty
things. You could do something like that, but I'd warn it requires alot of
research on deriving your own stream and stream buffer types. I turned this
thing into a 6 mo project and had to read more than one book. At any rate,
looking into deriving streams and stream buffers could be educational.
 
M

Michael DOUBEZ

mthread a écrit :
Hi,
I am new to C++, But has good experience in C. In C I have used
macros to enable / disable debug statements in the code.
[snip]

I would like to know what is the kind of standard used in C++ to
enable / disable the debug statements(ie cout statements).

There is no standard way. But there are libraries that can spare you the
effort of reinventing the wheel. Google for them (an example is
Log4cpp:http://log4cpp.sourceforge.net/).
IIRC there was a stub of log library in Boost also (in the vault).

Michael
 
G

Grizlyk

mthread said:
   #if defined ENABLE_DEBUG
        #define Printf1(arg) printf(arg)
   #else
       #define Printf1(arg)
  #endif

I would like to know what is the kind of standard used in C++ to
enable / disable the debug statements(ie cout statements).

As i can guess, you want to have two copies of code without overhead.
You can try templates to make instance for concrete Printf1, but i can
not say, that it is standard, suitable or portable way, only if you
are going to remove preprocessor as self-aim

//debuggergs
namespace Ndebug{
class Db
{
public:
inline
void
Printf1(const char *const fstr)
{ fprintf(stderr, fstr); }
};}

namespace Nno_debug{
class Db
{
public:
inline
void
Printf1(const char *const)
{ }
};}

//user code
//old non-templated "foo()" became
template<typename Ndb>
inline
void
foo()
{
//....
Ndb db; db.Printf1("hello");
//....
}

void z()
{
foo<Nno_debug::Db>();
//...
foo<Ndebug::Db>();
}

not very nice (due to C++ limitations for templates) and i am not
sure, that all compilers can remove "hello" from object code for
Nno_debug::Db here.

Maksim A. Polyanin
http://grizlyk1.narod.ru/cpp_new
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top