views:

91

answers:

4

Should a Unix GUI application's warning be sent to std::cerr or std::cout?

This presumes that GUI normally presents the warnings and errors in a console window and also sends them to a log file. But in the case that the console is missing and thus cannot be used should std::cerr, std::cout, or std::clog be used for such messages?

I'm thinking std::cerr is where they belong.

+5  A: 

I prefer cerr. If the user pipes the output or sends it to a file, they can opt-out of cerr with

tool 2>/dev/null >output

but putting everything in one stream leaves them SOL.

Also cerr is unbuffered, so error messages are guaranteed to appear no matter how hard you crash and burn. And warnings should be piped along with errors, if the user replaced /dev/null above with something else… I'm not sure if that's a distinct argument or not.

Potatoswatter
+4  A: 

If your program is designed to have a properly formatted output that may be piped to another program, or is going to be parsed, you should better redirect warnings to std::cerr.

Benoit
+1  A: 

For a compiler, error messages about the code being compiled are the "normal" output, so they should be written to stdout, not stderr. The only messages that should be written to stderr would be about errors in running the compiler itself (e.g., if a file that makes up part of the compiler can't be found, so the compiler couldn't run).

The same basic guideline applies to most other programs: if the "message" in question is part of the "standard" output of that program, and a user would normally expect it to be included when/if they redirect the output, then it should be written to standard output. Standard error is intended for messages the user will normally want/need to see even if they have standard output redirected to a file -- primarily those saying that the program couldn't run, so there is no output, or if there is that it's likely to be incomplete or invalid.

Jerry Coffin
@Jerry Coffin How about for a GUI program where the output of that program is not primarily its output to stdout?
WilliamKF
@WilliamKF: A GUI program *usually* shouldn't write to standard output or standard error at all. For truly dramatic problems, it should probably use a message box. Otherwise, it can typically have a "log" window to tell about what's happened and/or use the system's event logging capability for that kind of job.
Jerry Coffin
A: 

On Windows, neither std::cerr nor std::cout are directed anywhere from a GUI program, they just go to that big bit bucket in the sky. I presume you're talking about *nix based systems though.

Mark Ransom
Correct, this is Unix.
WilliamKF