🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.lang.c++.moderated
9 messages
9 total messages Started by Kyle Lussier Thu, 14 Dec 1995 20:31
Re-Routing streams
#5
Author: Kyle Lussier
Date: Thu, 14 Dec 1995 20:31
20 lines
1386 bytes
X-Original-Date: 14 Dec 1995 18:06:39 GMT

How do I get re-route, or overload the standard streams to send output
to somewhere else?  Specifically I want all messages to stderr to be
sent to a file and all messages to stdout to be sent to a file.

 Kyle Lussier
 Silicon Softworks

--------------------------------------------------------------------------
 Want free source to a Windows 95 3-D Modeler?  Mail lussier@cis.ufl.edu,
 or check out the LACE-3D home page:  http://www.cis.ufl.edu/~lussier
 Using POVRay and interested in a Win95 front-end for it?  Send me mail.
--------------------------------------------------------------------------


    [ comp.lang.c++.moderated is a moderated newsgroup.  Submit articles ]
    [  to <c++-submit@netlab.cs.rpi.edu>.  The moderation policy can be  ]
    [   retrieved from <http://netlab.cs.rpi.edu/~cppmods/guide.html>.   ]
    [    Moderators can be reached at: c++-request@netlab.cs.rpi.edu.    ]
Re: Re-Routing streams
#6
Author: Steven Yampolsky
Date: Fri, 15 Dec 1995 07:52
23 lines
1520 bytes
X-Original-Date: 15 Dec 1995 04:56:42 GMT

Kyle Lussier (lussier@aviator.cis.ufl.edu) wrote:
: X-Original-Date: 14 Dec 1995 18:06:39 GMT

: How do I get re-route, or overload the standard streams to send output
: to somewhere else?  Specifically I want all messages to stderr to be
: sent to a file and all messages to stdout to be sent to a file.
Did you try to set your standard streams(cin, cout, etc) to 
,say, file buffers?  
Just a thought.

--
Steven Y.

 -------------------------------------------------------------------
|I'm not really a college student. In reality I'm a rocket scientist|
 -------------------------------------------------------------------

    [ comp.lang.c++.moderated is a moderated newsgroup.  Submit articles ]
    [  to <c++-submit@netlab.cs.rpi.edu>.  The moderation policy can be  ]
    [   retrieved from <http://netlab.cs.rpi.edu/~cppmods/guide.html>.   ]
    [    Moderators can be reached at: c++-request@netlab.cs.rpi.edu.    ]
Re: Re-Routing streams
#7
Author: Diego Novillo
Date: Fri, 15 Dec 1995 08:10
38 lines
1624 bytes
X-Original-Date: 15 Dec 1995 05:47:10 GMT

lussier@aviator.cis.ufl.edu (Kyle Lussier) writes:

>How do I get re-route, or overload the standard streams to send output
>to somewhere else?  Specifically I want all messages to stderr to be
>sent to a file and all messages to stdout to be sent to a file.
>
Here's one way of doing it. I removed the various consistency checks you
may want to do (existence of the file, etc, etc):


#include <iostream.h>

ofstream     cout_file;

void  redirectOutput(String fname)
{

   cout_file.open(fname);
   cout = cout_file;
}


Hope this helps.

Diego.


--
Diego Novillo (diego@cs.ualberta.ca)    |      Dept of Computing Science
WWW http://web.cs.ualberta.ca/~diego    |          University of Alberta
Use 'finger' to get my PGP public key   |     Edmonton, Alberta - Canada

    [ comp.lang.c++.moderated is a moderated newsgroup.  Submit articles ]
    [  to <c++-submit@netlab.cs.rpi.edu>.  The moderation policy can be  ]
    [   retrieved from <http://netlab.cs.rpi.edu/~cppmods/guide.html>.   ]
    [    Moderators can be reached at: c++-request@netlab.cs.rpi.edu.    ]
Re: Re-Routing streams
#8
Author: J. Kanze
Date: Fri, 15 Dec 1995 15:56
31 lines
1949 bytes
X-Original-Date: 15 Dec 1995 14:21:37 +0100

Kyle Lussier (lussier@aviator.cis.ufl.edu) wrote:

|> How do I get re-route, or overload the standard streams to send output
|> to somewhere else?  Specifically I want all messages to stderr to be
|> sent to a file and all messages to stdout to be sent to a file.

Generally, this will be handled before invoking the program, in a system
dependant way.

Once the program is started, if your iostream library conforms to the
draft standard (precious few do), it is possible to modify the streambuf
that the stream uses, thus:

    filebuf     logFile( "logfile.name" ) ;
    cerr.rdbuf( &logFile ) ;

This is just a quick example.  Obviously, you will probably want some
error handling.  And being a bit paranoid, I would probably save the
preceding streambuf*, and restore it at program end.
-- 
James Kanze           (+33) 88 14 49 00          email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
Conseils, itudes et rialisations en logiciel orienti objet --
              -- A la recherche d'une activiti dans une region francophone

    [ comp.lang.c++.moderated is a moderated newsgroup.  Submit articles ]
    [  to <c++-submit@netlab.cs.rpi.edu>.  The moderation policy can be  ]
    [   retrieved from <http://netlab.cs.rpi.edu/~cppmods/guide.html>.   ]
    [    Moderators can be reached at: c++-request@netlab.cs.rpi.edu.    ]
Re: Re-Routing streams
#9
Author: Dietmar Kuehl
Date: Sat, 16 Dec 1995 15:39
36 lines
1907 bytes
X-Original-Date: 15 Dec 95 16:50:42 GMT

Hi,
Diego Novillo (diego@cs.ualberta.ca) wrote:
: X-Original-Date: 15 Dec 1995 05:47:10 GMT

: #include <iostream.h>
: ofstream     cout_file;

: void  redirectOutput(String fname)
: {

:    cout_file.open(fname);
:    cout = cout_file;
: }

This is not portable as the assignment is not part of the Standard C++
Library although some current implementations allow such an assignment
(I'm not sure whether a conforming compiler/library is allowed to
accept the above code). You can replace the used 'streambuf' using the
'rdbuf()' function. Thus you can replace the line with the assignment
by the following one:

     cout.rdbuf(cout_file.rdbuf());

This is portable. Note that you might want to remember the original
'streambuf' used and restore it prior to program termination.
--
dietmar.kuehl@uni-konstanz.de
http://www.informatik.uni-konstanz.de/~kuehl
I am a realistic optimist - that's why I appear to be slightly pessimistic

    [ comp.lang.c++.moderated is a moderated newsgroup.  Submit articles ]
    [  to <c++-submit@netlab.cs.rpi.edu>.  The moderation policy can be  ]
    [   retrieved from <http://netlab.cs.rpi.edu/~cppmods/guide.html>.   ]
    [    Moderators can be reached at: c++-request@netlab.cs.rpi.edu.    ]
Re: Re-Routing streams
#10
Author: J. Kanze
Date: Sat, 16 Dec 1995 15:39
31 lines
1940 bytes
X-Original-Date: 15 Dec 1995 14:21:37 +0100

Kyle Lussier (lussier@aviator.cis.ufl.edu) wrote:

|> How do I get re-route, or overload the standard streams to send output
|> to somewhere else?  Specifically I want all messages to stderr to be
|> sent to a file and all messages to stdout to be sent to a file.

Generally, this will be handled before invoking the program, in a system
dependant way.

Once the program is started, if your iostream library conforms to the
draft standard (precious few do), it is possible to modify the streambuf
that the stream uses, thus:

    filebuf     logFile( "logfile.name" ) ;
    cerr.rdbuf( &logFile ) ;

This is just a quick example.  Obviously, you will probably want some
error handling.  And being a bit paranoid, I would probably save the
preceding streambuf*, and restore it at program end.
-- 
James Kanze           (+33) 88 14 49 00          email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
Conseils, études et réalisations en logiciel orienté objet --
              -- A la recherche d'une activité dans une region francophone

    [ comp.lang.c++.moderated is a moderated newsgroup.  Submit articles ]
    [  to <c++-submit@netlab.cs.rpi.edu>.  The moderation policy can be  ]
    [   retrieved from <http://netlab.cs.rpi.edu/~cppmods/guide.html>.   ]
    [    Moderators can be reached at: c++-request@netlab.cs.rpi.edu.    ]
Re: Re-Routing streams
#12
Author: J. Kanze
Date: Mon, 18 Dec 1995 13:36
51 lines
2715 bytes
X-Original-Date: 18 Dec 1995 11:38:09 +0100

Dietmar Kuehl (kuehl@uzwil.informatik.uni-konstanz.de) wrote:
|> X-Original-Date: 15 Dec 95 16:50:42 GMT

|> Hi,
|> Diego Novillo (diego@cs.ualberta.ca) wrote:
|> : X-Original-Date: 15 Dec 1995 05:47:10 GMT

|> : #include <iostream.h>
|> : ofstream     cout_file;

|> : void  redirectOutput(String fname)
|> : {

|> :    cout_file.open(fname);
|> :    cout = cout_file;
|> : }

|> This is not portable as the assignment is not part of the Standard C++
|> Library although some current implementations allow such an assignment
|> (I'm not sure whether a conforming compiler/library is allowed to
|> accept the above code). You can replace the used 'streambuf' using the
|> 'rdbuf()' function. Thus you can replace the line with the assignment
|> by the following one:

|>      cout.rdbuf(cout_file.rdbuf());

|> This is portable. Note that you might want to remember the original
|> 'streambuf' used and restore it prior to program termination.

A more accurate description would be: this *should* be portable.  As far
as I can tell, there are not very many compilers at present which
actually implement this.

At present, there is no portable solution to the problem as stated.
There are implementations under which neither of the above solutions
work.  The best solution is probably to ask yourself: why do I want to
do this?  In most cases, it is trivial to rewrite the code to use a
reference to an ostream everywhere, instead of cout.  Then just
conditionally seat the reference to whichever stream you want.
-- 
James Kanze           (+33) 88 14 49 00          email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
Conseils, itudes et rialisations en logiciel orienti objet --
              -- A la recherche d'une activiti dans une region francophone

    [ comp.lang.c++.moderated is a moderated newsgroup.  Submit articles ]
    [  to <c++-submit@netlab.cs.rpi.edu>.  The moderation policy can be  ]
    [   retrieved from <http://netlab.cs.rpi.edu/~cppmods/guide.html>.   ]
    [    Moderators can be reached at: c++-request@netlab.cs.rpi.edu.    ]
Re: Re-Routing streams
#11
Author: J. Kanze
Date: Mon, 18 Dec 1995 15:41
59 lines
2604 bytes
X-Original-Date: 18 Dec 1995 11:33:06 +0100

Diego Novillo (diego@cs.ualberta.ca) wrote:
|> X-Original-Date: 15 Dec 1995 05:47:10 GMT

|> lussier@aviator.cis.ufl.edu (Kyle Lussier) writes:

|> >How do I get re-route, or overload the standard streams to send output
|> >to somewhere else?  Specifically I want all messages to stderr to be
|> >sent to a file and all messages to stdout to be sent to a file.
|> >
|> Here's one way of doing it. I removed the various consistency checks you
|> may want to do (existence of the file, etc, etc):


|> #include <iostream.h>

|> ofstream     cout_file;

|> void  redirectOutput(String fname)
|> {

|>    cout_file.open(fname);
|>    cout = cout_file;
|> }

While this will work for certain implementations (including the
reference implementation which was delivered with CFront), it is
certainly not guaranteed.

In another posting, I posted the solution specified by the standard.
Regretably, of course, this will not work with all implementations yet.

Basically, the only real portable solution in this case is to not use
cout, but a reference or pointer to a ostream, perhaps thus:

    ostream&
    getOutputStream( char const* filename )
    {
        if (filename == NULL || strcmp( filename , "-" ) == 0)
            return cout ;
        else
        {
            ofstream        f( filename ) ;
            return f ;
        }
    }

    ostream&        output( getOuptutStream( filename ) ) ;
-- 
James Kanze           (+33) 88 14 49 00          email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
Conseils, études et réalisations en logiciel orienté objet --
              -- A la recherche d'une activité dans une region francophone

    [ comp.lang.c++.moderated is a moderated newsgroup.  Submit articles ]
    [  to <c++-submit@netlab.cs.rpi.edu>.  The moderation policy can be  ]
    [   retrieved from <http://netlab.cs.rpi.edu/~cppmods/guide.html>.   ]
    [    Moderators can be reached at: c++-request@netlab.cs.rpi.edu.    ]
Re: Re-Routing streams
#13
Author: Andrew Fitzgibbon
Date: Wed, 20 Dec 1995 11:14
28 lines
1719 bytes
X-Original-Date: 20 Dec 1995 10:42:12 +0000


In article <4b423t$1k@netlab.cs.rpi.edu> kanze@gabi-soft.fr (J. Kanze) writes:

> Basically, the only real portable solution in this case is to not use
> cout, but a reference or pointer to a ostream, perhaps thus:
> 
>     ostream&        output( getOuptutStream( filename ) ) ;

That's what I do, and others reading my code ask "what are these cinfo,
cwarn, cerr streams, why don't you use cout?"

Isn't it annoying that something as simple as redirecting stdout is still
being discussed?

A.

-- 
Andrew Fitzgibbon (Research Associate),                     andrewfg@ed.ac.uk
Artificial Intelligence, Edinburgh University.               +44 031 650 4504
<a href=http://www.dai.ed.ac.uk/staff/personal_pages/andrewfg> Home Page </a>
                         "Never say there is no way" -- me.

    [ comp.lang.c++.moderated is a moderated newsgroup.  Submit articles ]
    [  to <c++-submit@netlab.cs.rpi.edu>.  The moderation policy can be  ]
    [   retrieved from <http://netlab.cs.rpi.edu/~cppmods/guide.html>.   ]
    [    Moderators can be reached at: c++-request@netlab.cs.rpi.edu.    ]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads