By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,165 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

Null output stream?

P: n/a
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.

--
My e-mail is valid but not my primary mailbox, aside from its being the
unfortunate victim of intensive mail-bombing at the moment. Please keep
replies on the 'group where everyone may benefit.
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Stewart Gordon wrote:
But does anyone know of a better solution?


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

std::ostream& Debug(int level) {
std::clog.clear(levell <= shown_level
? std::ios_base::goodbit
: std::ios_base::badbit);
return std::clog;
}
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
Jul 22 '05 #2

P: n/a

"Dietmar Kuehl" <di***********@yahoo.com> wrote in message news:bq*************@ID-86292.news.uni-berlin.de...
Stewart Gordon wrote:
But does anyone know of a better solution?


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.
Jul 22 '05 #3

P: n/a
Stewart Gordon <sm*******@yahoo.com> wrote in message news:<bq**********@sun-cc204.lut.ac.uk>...
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 :-)

--
hth
GJD
Jul 22 '05 #4

P: n/a
Gavin Deane wrote:
Stewart Gordon <sm*******@yahoo.com> wrote in message news:<bq**********@sun-cc204.lut.ac.uk>...
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 :-)


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
Jul 22 '05 #5

P: n/a
red floyd wrote:
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.
Jul 22 '05 #6

P: n/a
red floyd wrote:
Gavin Deane wrote:
Try searching comp.lang.c++ for "null stream" in Google Groups.
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::ostream {
nullstream(): std::ios(0), std::ostream(0) {}
};

This simply constructs an 'std::ostream' 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::ostream {
struct nullbuf: std::streambuf {
int overflow(int c) { return traits_type::not_eof(c); }
} m_sbuf;
nullstream(): std::ios(&m_sbuf), std::ostream(&m_sbuf) {}
};
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
Jul 22 '05 #7

P: n/a
Ron Natalie wrote:
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::ostream&*Debug(int*level)*{
static std::ostream rc(std::clog.rdbuf());
****rc.rdbuf(levell*<=*shown_level*?*std::clog.rdb uf(): 0);
****return*rc;
**}
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.