Null output stream?

S

Stewart Gordon

Using g++ 3.1, Mac OS X.

I'm doing something trying to involve a null output stream, i.e. a
stream that just discards what goes into it.

The idea is that it gives a simple way of producing debugging output,
which can be controlled at runtime. For example:

Debug(5) << "Doing something..." << endl;

would output iff the debug level is at least 5. If the level is less
than 5, Debug(5) would need to evaluate to some kind of null stream.

Meanwhile I've solved it by having Debug(int) return a wrapper object,
which has operator << overloaded.

template <class T>
const DebugStream& operator<< (const DebugStream& ds, const T& t) {
if (ds.Show) std::clog << t;
return ds;
}

However, this doesn't like manipulators for some reason.

I suppose I could go platform-specific by opening /dev/null for output
and using that instead of the makeshift DebugStream class.

But does anyone know of a better solution?

Stewart.
 
D

Dietmar Kuehl

Stewart said:
But does anyone know of a better solution?

Well, nothing new but the old solution: set eg. "badbit" to suppress
the output:

std::eek:stream& Debug(int level) {
std::clog.clear(levell <= shown_level
? std::ios_base::goodbit
: std::ios_base::badbit);
return std::clog;
}
 
R

Ron Natalie

Dietmar Kuehl said:
Well, nothing new but the old solution: set eg. "badbit" to suppress
the output:
Of course you have to bank on nobody clearing the bit.
 
G

Gavin Deane

Stewart Gordon said:
Using g++ 3.1, Mac OS X.

I'm doing something trying to involve a null output stream, i.e. a
stream that just discards what goes into it.

Try searching comp.lang.c++ for "null stream" in Google Groups. I was
interested in exactly the same thing not too long ago and I found a
few previous threads that seemed to cover it. Basically you have to
roll your own, but it's not very complicated. Or at least it didn't
seem to be - I haven't actually tried to do it yet :)
 
R

red floyd

Gavin said:
Try searching comp.lang.c++ for "null stream" in Google Groups. I was
interested in exactly the same thing not too long ago and I found a
few previous threads that seemed to cover it. Basically you have to
roll your own, but it's not very complicated. Or at least it didn't
seem to be - I haven't actually tried to do it yet :)

How about something like this:

file nullstream.h
------------------
#ifndef NULLSTREAM_H
#define NULLSTREAM_H

struct nullstream {
};

template <typename T>
nullstream& operator<<(nullstream& ns, T)
{
return ns;
}

#endif
 
R

red floyd

red said:
Gavin Deane wrote:
[redacted]
Sorry, my newsreader had truncated the thread, and I didn't see the original
post for it. I didn't realize that that's what he was already doing.
 
D

Dietmar Kuehl

How about something like this:

You should have followed the above advice. The "real" solution is
quite different. If you don't mind that 'badbit' is set all the time,
this is sufficient:

struct nullstream:
std::eek:stream {
nullstream(): std::ios(0), std::eek:stream(0) {}
};

This simply constructs an 'std::eek:stream' without a stream buffer.
The effect is that 'badbit' will be set all the time and no formatting
will happen.

If you dislike 'badbit' being set, you can install a stream buffer
which simply does nothing than returning success:

struct nullstream:
std::eek:stream {
struct nullbuf: std::streambuf {
int overflow(int c) { return traits_type::not_eof(c); }
} m_sbuf;
nullstream(): std::ios(&m_sbuf), std::eek:stream(&m_sbuf) {}
};
 
D

Dietmar Kuehl

Ron said:
Of course you have to bank on nobody clearing the bit.

Well, I consider it unlikely that someone really clears the bits in a
debug stream... However, if you want to play safe, here is a version
where you would have to set a new stream buffer to make it write
anything (and there is no protection against setting a stream buffer):

  std::eek:stream& Debug(int level) {
static std::eek:stream rc(std::clog.rdbuf());
    rc.rdbuf(levell <= shown_level ? std::clog.rdbuf(): 0);
    return rc;
  }
 

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

Forum statistics

Threads
474,151
Messages
2,570,854
Members
47,394
Latest member
Olekdev

Latest Threads

Top