473,585 Members | 2,501 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 14375
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.co m> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.co m/>
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*******@yaho o.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*******@yaho o.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<<(null stream& 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::no t_eof(c); }
} m_sbuf;
nullstream(): std::ios(&m_sbu f), std::ostream(&m _sbuf) {}
};
--
<mailto:di***** ******@yahoo.co m> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.co m/>
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*leve l)*{
static std::ostream rc(std::clog.rd buf());
****rc.rdbuf(le vell*<=*shown_l evel*?*std::clo g.rdbuf(): 0);
****return*rc;
**}
--
<mailto:di***** ******@yahoo.co m> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.co m/>
Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
10447
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 that I can't do the opposite. I want the program to send an object-reply from the server to the client. But on this step boths sides throw a socket...
2
5721
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 framework. The class should take care of the underlying memory management. Now which class is most suitable for this? (Assuming there is one.) Will...
3
5089
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); Response.End();
5
8047
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, and need to manage all socket traffic. My thought was a stateful call to my own Encrypt and Decrypt routines. That I would pass a MemoryStream to...
0
845
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 4096 characters. Otherwise it works. Does anyone know why? Here is the code
1
2679
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 it is apparently the default action of sync() is to call overflow(EOF). In my own testing under Linux with g++, it appears that it is the sync()...
0
1703
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 Excel is already running, it's perfect. If I click "Open" but Excel isn't running, I get an error message: 'C:\...snip...\Temporary Internet...
27
4085
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 File("some_file_name")); Then I got a message, the final variable not to be updated. Then I came to know that I have to take help from API,
7
1959
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
7908
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8199
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8336
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7950
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8212
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6606
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5389
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1447
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.