Discussion:
std null stream ?
(too old to reply)
toton
2007-01-17 07:07:46 UTC
Permalink
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::ostream 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
mimi
2007-01-17 07:42:09 UTC
Permalink
"toton 写道:
"
Post by 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::ostream 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".
Ondra Holub
2007-01-17 07:46:08 UTC
Permalink
Post by 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::ostream 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).
Nate Barney
2007-01-19 06:30:05 UTC
Permalink
Post by Ondra Holub
Post by 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::ostream 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 ?
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.
Post by Ondra Holub
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.

--
Nate
Jorgen Grahn
2007-01-20 15:22:06 UTC
Permalink
Post by Nate Barney
Post by Ondra Holub
Post by 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::ostream 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 ?
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.
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::ostream.

/Jorgen
--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyndns.org> R'lyeh wgah'nagl fhtagn!
Loading...