473,394 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Null output stream?

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

"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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Maria Gaitani | last post by:
Hi! I have made a client and a server which are supposed to communicate by sending java objects. They don't. I have managed to send an object from the client to the server. But continuing after...
2
by: Laurens | last post by:
Hi, My application needs to create in-memory arrays, the size of which is not known, or very hard to calculate, in advance. I would like to stream the data using the standard I/O class...
3
by: Ivan Demkovitch | last post by:
Hi! I'm using output stream to output image to webpage. //Move array to response stream. Response.ContentType = "image/jpeg"; Response.OutputStream.Write(arrBt, 0, arrBt.Length);...
5
by: Lee Gillie | last post by:
I am using Cryptography. You can encrypt or decrypt by providing an output stream as a parameter to the CryptoStream constructor. But I need byte arrays, as I am encrypting on the fly to a socket,...
0
by: tom mcdonald | last post by:
I am trying to write an xmldocument to the output stream but am unable to do so. I get 'error at position 4097' on the consumer side of the web service. This only happens when my xmldocs exceed...
1
by: Derek | last post by:
>From reading countless posts here I seem to get the impression that to roll your own output stream class, the minimum you would likely needed to do would be to override the overflow() method, as...
0
by: Jonathan | last post by:
Hi everyone, I'm having an issue with my Excel streaming. I run my procedure and if I "Save As" when the dialog opens and then open the file I generate, it's perfect. Also, if I choose "Open" when...
27
dmjpro
by: dmjpro | last post by:
Today I came across a thing, is it possible to change the Standard Output Stream? Then I immediately went for assign the System.out variable. System.out = new PrintStream(new...
7
by: mathieu | last post by:
Hi there, I am trying to rewrite this very slow function (due to AFAIK the extra copy): void DoAction(std::istream &is, std::ostream &os) { uint16_t c; while( is.read((char*)&c,2) )
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.