[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [CPROG] Midlertidig udskrift til stderr





Klaus S. Madsen wrote:
On Sun, Apr 13, 2003 at 05:30:39PM +0200, Jan Eggert Kofoed wrote:

Jeg har brug for at lave redirection af stderr til en fil,
som jeg har åbnet. Det virker sådan fint nok:

 string sErr = "TestCscanInput.err";
 ofstream oError(sErr.c_str());
 cerr = oError;
 ...
 oError.close();

Mit problem er at få stderr tilbage igen!

Hvordan kan jeg lave en midlertidig redirection af stderr?

På et lidt lavere niveau kan du rode med at kopiere fildescriptorere rundt. Det ud gør er noget i stil med flg (som ikke er testet, og som heller ikke laver nogen form for fejl check).

Start af redirection:

int oldstderr = dup(2);
int newstderr = open("fil.log", O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);
dup2(2, newstderr);
close(newstderr);

Efter den dup2, vil fildescriptor 2 (som er stderr) pege på din fil.
Fint, der foresvævede mig noget om 'duplicate file descriptor', men tænkte, at der kunne være noget på fstream-niveau. Fordelen ved dup kan jo så være, at det virker ved blanding af sprog.


Stop af redirection:


dup2(2, oldstderr)
close(oldstderr);

Og så vil disse stumper kode også virke med C og hvad du ellers kunne
finde på af underlige ting.

Ellers kunne du jo bare tildele den oprindelige værdi af cerr til en
variabel i begyndelsen af programmet.
Det havde jeg prøvet uden held, men kom nu igennem ved at vælge de stream typer, som compileren så venligt henviser til:
_IO_ostream_withassign* pOldStderr = &cerr;
oError.open("pip");
cerr = oError;
...
oError.close();
cerr = *pOldStderr;
og alle mine CPPunit tests kører nu fint igennem.
Thnx






 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:09 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *