What are the conventions for stdout/stderr messages?

丶灬走出姿态 提交于 2019-12-10 21:54:11

问题


I have an app that will fprintf both help and error messages to stderr.

Should I send messages to stdout if I am exiting with status EXIT_SUCCESS (such as when I issue the --help option to my app)?

Likewise, should I keep sending error messages to stderr on EXIT_FAILURE?

Or should I send all help and error messages to stdout?

What are general conventions for this with POSIX-compliant UNIX applications?


回答1:


Clearly error messages should go to stderr, because you don't want to capture them when redirecting standard output.

When the usage is displayed because some command line option was invalidly used, then it's being shown as (part of) an error message. So it should go to stderr and cause EXIT_FAILURE.

When the usage is being displayed because the user asked for it via --help, then it's being shown as the desired behaviour of invoking the command. So it should go to stdout and the command should succeed with EXIT_SUCCESS.

This is briefly covered in the GNU coding standards.




回答2:


According to the POSIX standard, standard error is used for writing diagnostic output. They seem to leave it up to the applications to define what is diagnostic output.

However, in my not so humble opinion, I dislike applications that write their help-text on stderr, because it's harder to do a simple grep on the text. I would say it's 50/50 which programs does this and which does not.




回答3:


Posix defines the standard streams thus:

At program start-up, three streams shall be predefined and need not be opened explicitly: standard input (for reading conventional input), standard output (for writing conventional output), and standard error (for writing diagnostic output). When opened, the standard error stream is not fully buffered; the standard input and standard output streams are fully buffered if and only if the stream can be determined not to refer to an interactive device.

The GNU C Library describes the standard streams similarly:

Variable: FILE * stdout
The standard output stream, which is used for normal output from the program.

Variable: FILE * stderr
The standard error stream, which is used for error messages and diagnostics issued by the program.

Thus, standard definitions have little guidance for stream usage beyond “conventional/normal output” and “diagnostic/error output.” In practice, it’s common to redirect either or both of these streams to files and pipelines, so consider the likely usage. Regular output should go to stdout, especially if users are likely to grep or otherwise analyze it. Help text in particular should go to stdout so that it can be easily searched and paged. Some systems monitor stderr for output and consider it a sign of problems, so generally use it only for actual errors and other significant diagnostics. And finally, only send interactive output (like progress indicators) if the stream is actually interactive (e.g., as reported by isatty) or when explicitly enabled by a command-line option.




回答4:


By using strace you can find that many GNU/Linux utilities (like ls, date, gcc, make ...) output their --help to stdout. I suggest doing like they do.



来源:https://stackoverflow.com/questions/7977852/what-are-the-conventions-for-stdout-stderr-messages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!