TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

How to write to stderr so people will like you

73 点作者 keyist大约 15 年前

8 条评论

ready大约 15 年前
"flush stdout, write your message to stderr, then flush stderr."<p>Standard Error is unbuffered: <a href="http://www.opengroup.org/onlinepubs/009695399/functions/stdin.html" rel="nofollow">http://www.opengroup.org/onlinepubs/009695399/functions/stdi...</a><p>"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."
crc5002大约 15 年前
If you can't recompile the program, an <i>expect</i> script will come very handy:<p><pre><code> $ ./unbuffer ./tst &#62;/tmp/logit 2&#62;&#38;1; cat /tmp/logit line 1 stderr line 1 line 2 stderr line 2 </code></pre> <a href="http://expect.nist.gov/example/unbuffer" rel="nofollow">http://expect.nist.gov/example/unbuffer</a>
gte910h大约 15 年前
This is a nice paradigm when it works, however for programs that are often piped to other programs, I believe this can run into issues with the flush on stdout blocking. Am I wrong here?
评论 #1271395 未加载
评论 #1271229 未加载
评论 #1271834 未加载
zokier大约 15 年前
imho better way to solve this 'problem' would be to write error messages in such way that you don't need stdout as context to interpret them.
评论 #1271831 未加载
评论 #1271725 未加载
j_baker大约 15 年前
"(Note that programs that run other programs need to do more than just this; they need to flush stdout and perhaps stderr before they start another program.)"<p>Could someone explain exactly why this is?
评论 #1271465 未加载
评论 #1271471 未加载
kqueue大约 15 年前
a good practice is not to combine stdout and stderr into the same output when the ordering matters. You'll get a different result depending on the shell you are using.
JoachimSchipper大约 15 年前
Erm... if the problem is that stdout may be fully buffered, just set it to line buffered explicitly? (See setbuf(3) and friends.)<p>(Note that only stdout is the issue, see ready's comment.)
CamperBob大约 15 年前
I'm in the habit of using doing a setbuf(stdout,NULL) in the beginning of all my console apps, anyway. Buffered console I/O is something that made sense 20 years ago but certainly not anymore.<p>If you did a setbuf(stderr,NULL) as well as setbuf(stdout,NULL), you'd achieve the same effect that the author's going for, with no need to remember to flush.
评论 #1272591 未加载