Re: freopen on both stdout and stderr
- From: Flash Gordon <spam@xxxxxxxxxxxxxxxxxx>
- Date: Fri, 23 Nov 2007 14:02:41 +0000
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
.
- Follow-Ups:
- Re: freopen on both stdout and stderr
- From: Keith Thompson
- Re: freopen on both stdout and stderr
- References:
- freopen on both stdout and stderr
- From: Guillaume Dargaud
- Re: freopen on both stdout and stderr
- From: Mark Bluemel
- freopen on both stdout and stderr
- Prev by Date: Re: why the usage of gets() is dangerous.
- Next by Date: Re: heap status
- Previous by thread: Re: freopen on both stdout and stderr
- Next by thread: Re: freopen on both stdout and stderr
- Index(es):
Relevant Pages
|