Topic: How do you shut off cerr?


Author: mikael@sundsvall.trab.se (Mikael Eriksson)
Date: 3 Jul 92 11:54:34 GMT
Raw View
noren@possum.den.mmc.com (Chuck Noren) writes:

>Are there any suggestions on how to shut off cerr?  [...]
>    How can you do it with the ostream
>member functions?
>Chuck Noren
>noren@inlm5.den.mmc.com     noren@pogo.den.mmc.com
>Martin Marieta, Mail Stop XL8058, P.O. Box 179, Denver, CO 80201
>(303)977-1646


Since the iostreams does not do anything when they are in an
error state you could set cerr to a bad state at the beginning
of the program, like this.

   cerr.clear(ios::badbit);

All output on cerr should then be no-ops.

  Mikael

--

  Mikael Eriksson          |  Phone : +46 60 16 10 62
  Telia Research AB        |  Fax   : +46 60 12 29 44
  BOX 883                  |  Email : Mikael.Eriksson@sundsvall.trab.se




Author: noren@possum.den.mmc.com (Chuck Noren)
Date: Thu, 2 Jul 1992 21:33:31 GMT
Raw View
We have created a daemon process in SunOS 4.1.1 (as BSD derivative)
which happens to use cerr ostream.  We will clean up output to
cerr in the future, but now we would like to quickly shutoff
the ostream output.  When creating a daemon, among other things,
we close all the file descriptors.  Messages to cout present no
problem, however messages to cerr cause the daemon process to
hang (one message with a flush -- I know cerr is supposed to flush
on every character).  With several messages the process dies.

Are there any suggestions on how to shut off cerr?  Piping to
the /dev/null on the bourne command line is too late for this
process (and really does not apply since we close stderr at the
start of process execution).  How can you do it with the ostream
member functions?

Thanks,

--
Chuck Noren
noren@inlm5.den.mmc.com     noren@pogo.den.mmc.com
Martin Marieta, Mail Stop XL8058, P.O. Box 179, Denver, CO 80201
(303)977-1646




Author: jss@lucid.com (Jerry Schwarz)
Date: Fri, 3 Jul 92 01:56:16 GMT
Raw View
In article <1992Jul2.213331.3474@den.mmc.com>, noren@possum.den.mmc.com (Chuck Noren) writes:
|>
|> We have created a daemon process in SunOS 4.1.1 (as BSD derivative)
|> which happens to use cerr ostream.  We will clean up output to
|> cerr in the future, but now we would like to quickly shutoff
|> the ostream output.  When creating a daemon, among other things,
|> we close all the file descriptors.  Messages to cout present no
|> problem, however messages to cerr cause the daemon process to
|> hang (one message with a flush -- I know cerr is supposed to flush
|> on every character).  With several messages the process dies.
|>
|> Are there any suggestions on how to shut off cerr?  Piping to
|> the /dev/null on the bourne command line is too late for this
|> process (and really does not apply since we close stderr at the
|> start of process execution).  How can you do it with the ostream
|> member functions?

It isn't clear whether the question is what to do in the program
so that nothing comes out on cerr or how to deal with the daemon
process.  I'll address the former question.  The latter is more
a UNIX question than a C++ question.


The first thing to try is

 cerr.clear(badbit)

which will put the ostream into a state where all attempts to insert
characters will fail.  If your program is ignoring these failures
it should do what you want.

Since cerr is attached to file descriptor 2 in most (probably
all unix) implementations

     close(2)

will have essentially the same effect.

If neither of the above works you can divert the stream to another
streambuf, eg

 ostrstream devnull;
 cerr = devnull ;

Assigning to cerr is allowed because it is an ostream_withassign rather
than a plain ostream.  I generally don't think it is good style, but
in extremus go ahead.

   -- Jerry Schwarz