How to compare stderr to stdout
-
In a console app, I want to be able to test whether stdout and stderr are both printing to the screen. My output is messy unless I get it right (I'm printing to both stdout, and to stderr, and if they both happen to be the screen, I get two copies; I only want one copy on the screen). A line like
if( stdout == stderr )
always tests false under all the Windows compilers I've tried, so I get the duplication I'm trying to avoid. Any ideas?David --------- Empirical studies indicate that 20% of the people drink 80% of the beer. With C++ developers, the rule is that 80% of the developers understand at most 20% of the language. It is not the same 20% for different people, so don't count on them to understand each other's code. http://yosefk.com/c++fqa/picture.html#fqa-6.6 ---------
-
In a console app, I want to be able to test whether stdout and stderr are both printing to the screen. My output is messy unless I get it right (I'm printing to both stdout, and to stderr, and if they both happen to be the screen, I get two copies; I only want one copy on the screen). A line like
if( stdout == stderr )
always tests false under all the Windows compilers I've tried, so I get the duplication I'm trying to avoid. Any ideas?David --------- Empirical studies indicate that 20% of the people drink 80% of the beer. With C++ developers, the rule is that 80% of the developers understand at most 20% of the language. It is not the same 20% for different people, so don't count on them to understand each other's code. http://yosefk.com/c++fqa/picture.html#fqa-6.6 ---------
DQNOK wrote:
A line like if( stdout == stderr ) always tests false under all the Windows compilers I've tried, so I get the duplication I'm trying to avoid.
What about:
if (&stdout == &stderr)
In any case, I would really expect them to not ever be *equal* even though they may resolve to the same location (i.e., screen).
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
DQNOK wrote:
A line like if( stdout == stderr ) always tests false under all the Windows compilers I've tried, so I get the duplication I'm trying to avoid.
What about:
if (&stdout == &stderr)
In any case, I would really expect them to not ever be *equal* even though they may resolve to the same location (i.e., screen).
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
DavidCrow wrote:
What about: if (&stdout == &stderr)
That won't compile under Visual C++ The standard says that stdout and stderr are macros that evaluate to FILE pointer rvalues. Taking the address them seems suspicious from the outset...
David --------- Empirical studies indicate that 20% of the people drink 80% of the beer. With C++ developers, the rule is that 80% of the developers understand at most 20% of the language. It is not the same 20% for different people, so don't count on them to understand each other's code. http://yosefk.com/c++fqa/picture.html#fqa-6.6 ---------
-
In a console app, I want to be able to test whether stdout and stderr are both printing to the screen. My output is messy unless I get it right (I'm printing to both stdout, and to stderr, and if they both happen to be the screen, I get two copies; I only want one copy on the screen). A line like
if( stdout == stderr )
always tests false under all the Windows compilers I've tried, so I get the duplication I'm trying to avoid. Any ideas?David --------- Empirical studies indicate that 20% of the people drink 80% of the beer. With C++ developers, the rule is that 80% of the developers understand at most 20% of the language. It is not the same 20% for different people, so don't count on them to understand each other's code. http://yosefk.com/c++fqa/picture.html#fqa-6.6 ---------
DQNOK wrote:
Any ideas?
Does GetStdHandle() help?
Nibu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com
-
In a console app, I want to be able to test whether stdout and stderr are both printing to the screen. My output is messy unless I get it right (I'm printing to both stdout, and to stderr, and if they both happen to be the screen, I get two copies; I only want one copy on the screen). A line like
if( stdout == stderr )
always tests false under all the Windows compilers I've tried, so I get the duplication I'm trying to avoid. Any ideas?David --------- Empirical studies indicate that 20% of the people drink 80% of the beer. With C++ developers, the rule is that 80% of the developers understand at most 20% of the language. It is not the same 20% for different people, so don't count on them to understand each other's code. http://yosefk.com/c++fqa/picture.html#fqa-6.6 ---------
-
If your output is being flooded why dont you just re-direct stderr to a file. console_app 2> stderr.log
I like that idea. But this needs to be general. It's an "alert" library (a very minature logging library) that needs to detect on its own whether stderr has already been redirected. Key idea: *auto-detect* where stderr is outputting to, and if it's going to stdout, skip over the requirement to write to stderr.
vprintf( alertMsg, msgArgs ); //print to stdout if( stdout != stderr ) vfprintf( stderr, alertMsg, msgArgs ); //print to stderr //otherwise, already printed to stderr. Just move on. ...
(Note: code is not complete; it needs some complicating va_end and va_starts in there to make it work correctly. Don't use it as a model!)
David --------- Empirical studies indicate that 20% of the people drink 80% of the beer. With C++ developers, the rule is that 80% of the developers understand at most 20% of the language. It is not the same 20% for different people, so don't count on them to understand each other's code. http://yosefk.com/c++fqa/picture.html#fqa-6.6 ---------
-
DQNOK wrote:
Any ideas?
Does GetStdHandle() help?
Nibu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com
-
DQNOK wrote:
Any ideas?
Does GetStdHandle() help?
Nibu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com
Nibu babu thomas wrote:
Does GetStdHandle() help?
From reading the MSDN site, I thought it would; but it doesn't do what I thought.
HANDLE stderrHndl = GetStdHandle(STD_ERROR_HANDLE); HANDLE stdoutHndl = GetStdHandle(STD_OUTPUT_HANDLE); ... (very incomplete!) vfprintf( stderr, msg, args ); if( stderrHndl == stdoutHndl ) //still ALWAYS tests FALSE goto logprint; //skip printing to stdout vfprintf( stdout, msg, args ); logprint:
Because the stderrHndl == stdoutHndl is always evaluating to FALSE, I'm still double printing to the console.
David