Adding debug message



 DEVELOP > c-Plus-Plus > Adding debug message

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "mthread"
Date: 27 Jan 2008 11:23:45 PM
Object: Adding debug message
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).
.

User: "Christopher"

Title: Re: Adding debug message 28 Jan 2008 12:05:38 AM
"mthread" <rjkumr@gmail.com> wrote in message
news:27432990-a972-49ee-a9d8-4e0a76ed0b8f@e6g2000prf.googlegroups.com...

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

User: "Alf P. Steinbach"

Title: Re: Adding debug message 27 Jan 2008 11:57:22 PM
* 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
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
.

User: "Michael DOUBEZ"

Title: Re: Adding debug message 28 Jan 2008 02:30:01 AM
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
.
User: "Charles"

Title: Re: Adding debug message 28 Jan 2008 07:58:35 PM
Michael DOUBEZ wrote:

IIRC there was a stub of log library in Boost also (in the vault).

Version 2 of John Torjo's Boost Logging Library comes up for review in about
a week. See http://torjo.com/log2/ for more.
--
Chuck
.


User: "Grizlyk"

Title: Re: Adding debug message 29 Jan 2008 12:25:30 AM
mthread wrote:


=A0 =A0#if defined ENABLE_DEBUG
=A0 =A0 =A0 =A0 #define Printf1(arg) printf(arg)
=A0 =A0#else
=A0 =A0 =A0 =A0#define Printf1(arg)
=A0 #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
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER