std null stream ?

T

toton

Hi,
I want to have a log stream, where I can put certain logs. The log
can be console or file or any other ui widget.
This part I had already done with. However I want log stream
(std::eek:stream type) to initialize to a default null stream, when it is
not explicitly set to other stream, which will consume all of the
characters, like /dev/null .
How to write such a stream, derived from basic_ostream ?

abir
 
M

mimi

"toton дµÀ£º
"
Hi,
I want to have a log stream, where I can put certain logs. The log
can be console or file or any other ui widget.
This part I had already done with. However I want log stream
(std::eek:stream type) to initialize to a default null stream, when it is
not explicitly set to other stream, which will consume all of the
characters, like /dev/null .
How to write such a stream, derived from basic_ostream ?

abir
Use default constructor, or initialize it to be "\0".
 
O

Ondra Holub

toton napsal:
Hi,
I want to have a log stream, where I can put certain logs. The log
can be console or file or any other ui widget.
This part I had already done with. However I want log stream
(std::eek:stream type) to initialize to a default null stream, when it is
not explicitly set to other stream, which will consume all of the
characters, like /dev/null .
How to write such a stream, derived from basic_ostream ?

abir

Use ordinary std::fstream, open it only for writing to required file
"/dev/null". It should work.

If you really want to create own stream, just derive it from
basic_ostream and simply define your own operator<< to be function
which only returns stream reference. You will have to write dummy
'write' and 'put' method too (and all other methods for output).
 
N

Nate Barney

Ondra said:
toton napsal:

Use ordinary std::fstream, open it only for writing to required file
"/dev/null". It should work.

This would work on unices, but it is platform-specific.
If you really want to create own stream, just derive it from
basic_ostream and simply define your own operator<< to be function
which only returns stream reference. You will have to write dummy
'write' and 'put' method too (and all other methods for output).

This would be better, as it is not platform specific.

However, my suggestion would be to subclass std::streambuf to ignore any
characters written. Then the log stream can start out with the 'null'
streambuf, but be provided with a different one when logging is desired.
 
J

Jorgen Grahn

This would work on unices, but it is platform-specific.

Plus it would mean actucal I/O, actual system calls. There might be a
performance hit there which he doesn't want.

I've successfully used something like this:

logging::cerr << foo << '\n';

and a compile-time #ifdef which either pulls std::cerr into the logging
namespace, or makes logging::cerr a dummy object which supports the normal
ostream methods but does nothing (in which case you don't even pay the cost
for ostream << foo).

But (a) you have to choose at compile time and (b) you cannot pass around a
logging::cerr, because it might not be polymorphic to std::eek:stream.

/Jorgen
 
Joined
Jul 3, 2008
Messages
1
Reaction score
0
I have implemented a debug stream system which includes a 'null' stream

To quote the docs:
World Foundry has a powerful logging system with many separately enableable output streams (this is accomplished through implementing a "NULL" output stream which just discards all output. By default most log streams point to the null stream, but each can be pointed at stdout, stderr, a file, or even a window on the monochrome monitor. When combined with reproducible runs this makes debugging easy.

The debugging streams can be redirected at run time, via debugging console, or progmatically, and new debug streams can be easily be added (add a single line to a source file (libstrm.inc), and start using your new stream).

http://tenetti.org/cgi-bin/twiki/view.cgi/WorldFoundry/DebuggingWorldFoundry
has a little bit of information on using the redirectable debug streams.

http://wf-gdk.cvs.sourceforge.net/wf-gdk/wfsource/source/cpplib/
strmnull.hp/cc has an implementation of a null stream which worked on a variety of compilers/environments when World Foundry was written.

libstrm.hp & cc shows how a stream is added via libstrm.inc.

Usage is simple, if you define a stream called cphysics, the you print to it like any other stream:
cphysics << "sample output:" << x << ":" << y << std::endl;


I am still using a modified version of this debug streaming at my current job, and continue to find them quite useful.

Hope this helps,
Kevin Seghetti
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top