简体   繁体   English

stdout / stderr消息的约定是什么?

[英]What are the conventions for stdout/stderr messages?

I have an app that will fprintf both help and error messages to stderr . 我有一个应用程序,将fprintf既帮助和错误消息到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)? 如果我退出状态EXIT_SUCCESS (例如当我向我的应用程序发出--help选项时),我应该向stdout发送消息吗?

Likewise, should I keep sending error messages to stderr on EXIT_FAILURE ? 同样,我应该继续在EXIT_FAILURE上向stderr发送错误消息吗?

Or should I send all help and error messages to stdout ? 或者我应该向stdout发送所有帮助和错误消息?

What are general conventions for this with POSIX-compliant UNIX applications? 符合POSIX标准的UNIX应用程序的一般约定是什么?

Clearly error messages should go to stderr , because you don't want to capture them when redirecting standard output. 显然错误消息应该转到stderr ,因为在重定向标准输出时你不想捕获它们。

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 . 所以应该去stderr并导致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. 当显示用法时,因为用户通过--help请求它,然后它被显示为调用命令的所需行为。 So it should go to stdout and the command should succeed with EXIT_SUCCESS . 所以它应该转到stdout ,命令应该成功使用EXIT_SUCCESS

This is briefly covered in the GNU coding standards . GNU编码标准简要介绍了这一点。

According to the POSIX standard , standard error is used for writing diagnostic output . 根据POSIX标准标准错误用于写入诊断输出 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. 然而,在我不那么谦虚的意见中,我不喜欢在stderr上写下他们的帮助文本的应用程序,因为在文本上做一个简单的grep更难。 I would say it's 50/50 which programs does this and which does not. 我会说它是50/50,哪些程序执行此操作,哪些不执行此操作。

Posix defines the standard streams thus : POSIX定义标准流从而

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: GNU C库类似地描述了标准流:

Variable: FILE * stdout 变量: FILE * 标准输出
The standard output stream, which is used for normal output from the program. 标准输出流,用于程序的正常输出。

Variable: FILE * stderr 变量: 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. 常规输出应该转到stdout特别是如果用户可能会grep或以其他方式分析它。 Help text in particular should go to stdout so that it can be easily searched and paged. 特别是帮助文本应该转到stdout以便可以轻松搜索和分页。 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. 有些系统监视stderr的输出并将其视为问题的标志,因此通常仅将其用于实际错误和其他重要诊断。 And finally, only send interactive output (like progress indicators) if the stream is actually interactive (eg, as reported by isatty ) or when explicitly enabled by a command-line option. 最后,如果流实际上是交互式的(例如,由isatty报告)或者通过命令行选项显式启用,则仅发送交互式输出(如进度指示器)。

By using strace you can find that many GNU/Linux utilities (like ls , date , gcc , make ...) output their --help to stdout . 通过使用strace您可以发现许多GNU / Linux实用程序(如lsdategccmake ...)将--help输出到stdout I suggest doing like they do. 我建议他们这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM