简体   繁体   English

什么时候需要使用stderr? 重要错误还是所有错误?

[英]When I need to use stderr ? important errors or all errors?

It's a basic question, but i can't find a good answer. 这是一个基本问题,但是我找不到一个好的答案。

For example, if i use malloc , i check the malloc and if i saw an error i can do : 例如,如果我使用malloc ,则检查malloc ,如果看到错误,我可以这样做:

fprintf(stderr, "Message");

It's an important error message, the output is on stderr . 这是一条重要的错误消息,输出在stderr

Now if i have a program who ask numbers between 1 an 5. The program ask to the user until he enters a good number. 现在,如果我有一个程序要求1至5之间的数字。该程序会询问用户,直到他输入一个好的数字。

If the user enter 10 for example, is it good things to redirect to error message to stderr ? 例如,如果用户输入10,将错误消息重定向到stderr是件好事吗?

fprintf(stderr, "Message, wrong number, try again");

Or it's a normal error, and the message should go on stdout ? 还是正常错误,消息应该在stdout

If you'd ever want to redirect certain types of errors independently of all other output, send them to stderr . 如果您想独立于所有其他输出重定向某些类型的错误,请将其发送到stderr

Otherwise, I think that it's important to be consistent throughout your program, so decide on what goes where and stick to it. 否则,我认为在整个程序中保持一致是很重要的,因此请决定在什么地方坚持下去。

It is up to you how and when you decide to use it. 由您决定如何以及何时使用它。

A common application is to separate the program output from any sort of notification that is not part of the output, be it errors or just progress so the user knows the program didn't hang or anything. 常见的应用程序是将程序输出与任何不是输出的一部分的通知(无论是错误还是只是进度)分开,以便用户知道程序没有挂起或发生任何异常。

I have, for instance, a code that fetches several tables of data in XML format from a web server, converts and generates a SQL dump as output, so I can pass it directly to the command mysql , as in my_importer | mysql -options 例如,我有一个代码,可以从Web服务器获取XML格式的几张数据表,转换并生成SQL转储作为输出,因此我可以将其直接传递给mysql命令,如my_importer | mysql -options my_importer | mysql -options . my_importer | mysql -options While the operation running, the program outputs feedback to stderr , just so I know its working and what it has imported so far. 当操作运行时,程序会将反馈输出到stderr ,以使我知道它的工作原理以及到目前为止已导入的内容。

Remember that stderr is not really about errors, but about diagnostics. 请记住,stderr并不是真正关于错误,而是关于诊断。 Any diagnostic messages (including debug mode notifications and fancy progress bars) should go to stderr, while all the interactions, be it user or another program, should go on stdout. 任何诊断消息(包括调试模式通知和特殊进度条)都应发送到stderr,而所有交互(无论是用户还是其他程序)都应在stdout上进行。 Also stderr is normally connected to a terminal, while stdout may be part of a shell pipe (thus invisible). stderr通常也连接到端子,而stdout可能是壳管的一部分(因此是不可见的)。 And there is no way to connect stderr to next program separately from stdout. 而且没有办法将stderr与stdout分开连接到下一个程序。

Regarding your case, the message about wrong number is obviously the part of interaction, so use stdout. 对于您的情况,有关错误号码的消息显然是交互的一部分,因此请使用stdout。 It is bad idea to move active conversation to other stream. 将活动对话移至其他流是个坏主意。 But if it is not intended that user/peer process should read that and react, but is just info for someone diagnosing/debugging the program, then use stderr. 但是,如果不打算让用户/对等进程读取该消息并做出反应,而仅仅是给诊断/调试程序的人提供信息,请使用stderr。

For an interactive program like the one you are discussing, I don't think it makes a lot of difference. 对于您正在讨论的交互式程序,我认为它没有太大的区别。 If the program cannot be used with standard output and/or standard error redirected, you might as well simplify and use standard output for prompts and diagnostics as well. 如果该程序不能与标准输出和/或标准错误重定向一起使用,则最好也将标准输出简化并用于提示和诊断。

Having said that, there are many scenarios where redirection makes sense, or even is imperative for some use cases to work correctly. 话虽如此,在许多情况下重定向是有意义的,甚至对于某些用例而言,必须正确地工作。 If there is any imaginable way your program could be used that way, it is extremely important to make sure that standard output only contains actual production output, and everything else goes to standard error. 如果可以使用您的程序的任何可以想象的方式,那么确保标准输出仅包含实际的生产输出,而所有其他一切都变为标准错误,这非常重要。

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

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