PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > freopen on both stdout and stderr
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
freopen on both stdout and stderr

Réponse
 
LinkBack Outils de la discussion
Vieux 23/11/2007, 11h49   #1
Guillaume Dargaud
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut freopen on both stdout and stderr

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.


  Réponse avec citation
Vieux 23/11/2007, 12h01   #2
Mark Bluemel
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: freopen on both stdout and stderr

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.
  Réponse avec citation
Vieux 23/11/2007, 14h02   #3
Flash Gordon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: freopen on both stdout and stderr

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
  Réponse avec citation
Vieux 23/11/2007, 19h22   #4
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: freopen on both stdout and stderr

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) kst-u@mib.org
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"
  Réponse avec citation
Vieux 24/11/2007, 09h13   #5
santosh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: freopen on both stdout and stderr

In article <fi7994$1j5$1@aioe.org>, Keith Thompson <kst-u@mib.org> 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.

  Réponse avec citation
Vieux 24/11/2007, 21h05   #6
Justin Spahr-Summers
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: freopen on both stdout and stderr

On Nov 23, 6:01 am, Mark Bluemel <mark_blue...@pobox.com> 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."
  Réponse avec citation
Vieux 26/11/2007, 14h40   #7
Charlie Gordon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: freopen on both stdout and stderr

"Guillaume Dargaud" <use_the_form_on_my_contact_page@www.gdargaud.ne t> a
écrit dans le message de news: fi6eo6$um6$1@ccpntc8.in2p3.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.


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 05h07.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,23346 seconds with 15 queries