Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > freopen on both stdout and stderr

Reply
Thread Tools

freopen on both stdout and stderr

 
 
Guillaume Dargaud
Guest
Posts: n/a
 
      11-23-2007
Hello all,
a while ago I was pointed towards freopen as a way to redirect stderr to a
log file. It works great, but apparently the app also writes a few lines to
stdout. Now I could redirect to 2 separate files, but would rather keep the
2 flows together.

Is it correct to do this:
stderr=freopen(LogFile, "w", stderr);
stdout=freopen(LogFile, "a", stdout);

I can see plenty of reasons why it would fail (buffering and flushes come to
mind).
Any advice ?
--
Guillaume Dargaud
http://www.gdargaud.net/Climbing/
"Faith can move mountains but let them happily fall down on the heads of
other people. What's the point in moving mountains when it's so simple to
climb over them ?" - Boris Vian, surrealist French writer and singer, En
verve.


 
Reply With Quote
 
 
 
 
Mark Bluemel
Guest
Posts: n/a
 
      11-23-2007
Guillaume Dargaud wrote:
> Hello all,
> a while ago I was pointed towards freopen as a way to redirect stderr to a
> log file. It works great, but apparently the app also writes a few lines to
> stdout. Now I could redirect to 2 separate files, but would rather keep the
> 2 flows together.
>
> Is it correct to do this:
> stderr=freopen(LogFile, "w", stderr);
> stdout=freopen(LogFile, "a", stdout);


It's not in accordance with the standard to do this, as "stderr" and
"stdout" are not, as I recall, guaranteed to be lvalues...

If you can assign them, and you're prepared to accept non-portability,
then you could perhaps try the following, untested, hackery

stderr=freopen(LogFile, "w", stderr);
fclose(stdout);
stdout=stderr;

Otherwise, I'm not convinced you have many options available to you.
 
Reply With Quote
 
 
 
 
Flash Gordon
Guest
Posts: n/a
 
      11-23-2007
Mark Bluemel wrote, On 23/11/07 12:01:
> Guillaume Dargaud wrote:
>> Hello all,
>> a while ago I was pointed towards freopen as a way to redirect stderr
>> to a log file. It works great, but apparently the app also writes a
>> few lines to stdout. Now I could redirect to 2 separate files, but
>> would rather keep the 2 flows together.
>>
>> Is it correct to do this:
>> stderr=freopen(LogFile, "w", stderr);
>> stdout=freopen(LogFile, "a", stdout);

>
> It's not in accordance with the standard to do this, as "stderr" and
> "stdout" are not, as I recall, guaranteed to be lvalues...
>
> If you can assign them, and you're prepared to accept non-portability,
> then you could perhaps try the following, untested, hackery
>
> stderr=freopen(LogFile, "w", stderr);
> fclose(stdout);
> stdout=stderr;
>
> Otherwise, I'm not convinced you have many options available to you.


Personally I would use a mechanism outside the program in order to
achieve this and not redirect either stdout or stderr within the
program. E.g.
prog >log 2>&1
The precise mechanism (and whether it is even possible) is dependant on
the system being used.
--
Flash Gordon
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      11-23-2007
Flash Gordon wrote:
[...]
> Personally I would use a mechanism outside the program in order to
> achieve this and not redirect either stdout or stderr within the
> program. E.g.
> prog >log 2>&1
> The precise mechanism (and whether it is even possible) is dependant on
> the system being used.


That's fine if you want to redirect stderr and stdout to the same place
for the entire run of the program. If you want to redirect it only for
part of the program, it's more difficult.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed)
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      11-24-2007
In article <fi7994$1j5$(E-Mail Removed)>, Keith Thompson <(E-Mail Removed)> wrote
on Saturday 24 Nov 2007 12:52 am:

> Flash Gordon wrote:
> [...]
>> Personally I would use a mechanism outside the program in order to
>> achieve this and not redirect either stdout or stderr within the
>> program. E.g.
>> prog >log 2>&1
>> The precise mechanism (and whether it is even possible) is dependant
>> on the system being used.

>
> That's fine if you want to redirect stderr and stdout to the same
> place for the entire run of the program. If you want to redirect it
> only for part of the program, it's more difficult.


As far as I can see, the only fully portable solution is to design the
program from the ground up in such a way that all I/O functions take
explicit stream parameters instead of being hard-wired to a particular
stream.

Also when stdout is redirected there is no way to recover the path to
the original device.

 
Reply With Quote
 
Justin Spahr-Summers
Guest
Posts: n/a
 
      11-24-2007
On Nov 23, 6:01 am, Mark Bluemel <(E-Mail Removed)> wrote:
> Guillaume Dargaud wrote:
> > Hello all,
> > a while ago I was pointed towards freopen as a way to redirect stderr to a
> > log file. It works great, but apparently the app also writes a few lines to
> > stdout. Now I could redirect to 2 separate files, but would rather keep the
> > 2 flows together.

>
> > Is it correct to do this:
> > stderr=freopen(LogFile, "w", stderr);
> > stdout=freopen(LogFile, "a", stdout);

>
> It's not in accordance with the standard to do this, as "stderr" and
> "stdout" are not, as I recall, guaranteed to be lvalues...
>
> If you can assign them, and you're prepared to accept non-portability,
> then you could perhaps try the following, untested, hackery
>
> stderr=freopen(LogFile, "w", stderr);
> fclose(stdout);
> stdout=stderr;
>
> Otherwise, I'm not convinced you have many options available to you.


N1256 seems to specify that, on success, freopen() just returns its
third argument, so there'd be no need to assign the result to stdout/
stderr when passing them as the stream. There's even a footnote:

"The primary use of the freopen function is to change the file
associated with a standard text stream (stderr, stdin, or stdout), as
those identifiers need not be modifiable lvalues to which the value
returned by the fopen function may be assigned."
 
Reply With Quote
 
Charlie Gordon
Guest
Posts: n/a
 
      11-26-2007
"Guillaume Dargaud" <(E-Mail Removed) t> a
écrit dans le message de news: fi6eo6$um6$(E-Mail Removed)2p3.fr...
> Hello all,
> a while ago I was pointed towards freopen as a way to redirect stderr to a
> log file. It works great, but apparently the app also writes a few lines
> to stdout. Now I could redirect to 2 separate files, but would rather keep
> the 2 flows together.
>
> Is it correct to do this:
> stderr=freopen(LogFile, "w", stderr);
> stdout=freopen(LogFile, "a", stdout);


Do not store the result of freopen into stderr or stdout, just check that it
is not NULL.
In order to preserve proper flows for both streams, you should make both
unbuffered via setvbuf.
You might also want to first create the LogFile, and then freopen both
streams in append mode.

--
Chqrlie.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
freopen, fclose and stdout Alex Vinokur C Programming 4 01-11-2013 07:22 AM
Making `logging.basicConfig` log to *both* `sys.stderr` and `sys.stdout`? Michel Albert Python 5 10-24-2012 09:32 AM
stdout and stderr don't occur sequentially in Eclipse and Netbeans ZelluX Java 15 06-25-2008 12:06 AM
Re: embedding Python and changing stdin, stdout and stderr Vincent Touquet Python 0 09-06-2004 04:35 PM
embedding Python and changing stdin, stdout and stderr Vincent Touquet Python 1 09-03-2004 12:25 PM



Advertisments