如何在不缓冲 stdout 和 stderr 的情况下记录 make 输出
我在记录自动构建的输出时遇到问题。
构建是通过 Makefile 和 makefile 实用程序完成的。
问题是像编译器命令行这样的正常输出会转到 stdout,而编译错误会转到 stderr。
我想获得屏幕上显示的构建输出。 所以类似:
(stdout) CC -c file.cpp
(stderr) Compile error at file.cpp line 232, blah blah blah
(stdout) CC -c file2.cpp
我尝试的(来自 ksh 脚本)是:
make -k > build.log 2> build.log
这会产生单个日志文件,但问题是流被缓冲,因此日志文件中的结果全部混合在一起。
我可以将输出捕获到两个单独的日志文件中,但随后我就没有关于如何将它们重新粘合到单个日志文件中的信息。
在这种情况下,有没有办法关闭 stdout 和 stderr 的缓冲?
I am having a problem with logging to output from an automated build.
The build is done with a Makefile and the makefile utility.
The problem is that normal output like compiler command lines go to stdout and compile errors go to stderr.
I want to get the output from the build as it would show on the screen. So something like:
(stdout) CC -c file.cpp
(stderr) Compile error at file.cpp line 232, blah blah blah
(stdout) CC -c file2.cpp
What I tried (from a ksh script) is:
make -k > build.log 2> build.log
This results in a single log file but the problem is that the streams are buffered and so the result in the log file is all mixed up.
I could capture the output into 2 separate log files but then I would have no info on how to glue them back together into a single log file.
Is there a way to turn off buffering for stdout and stderr in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这应该更适合您,因为它不是分别重定向 stderr 和 stdout,而是将 stderr 重定向到 stdout,这应该使缓冲同步。
如果您想将其记录到文件并将其打印到控制台:
This should work better for you because it is not redirecting stderr and stdout separately, but redirecting stderr to stdout, which should make the buffering sync up.
If you want to log it to a file as well as print it to the console:
尝试这个
Try this
将它们重新粘在一起很棘手,但也有一个正确的答案,而且它有效! 请参阅“如何将 stdout 和 stderr 保存或重定向到不同的位置文件?”,作者:Vivek Gite。
Gluing them back together is tricky but there is a right answer for that too, and it works! See "How do I save or redirect stdout and stderr into different files?" by Vivek Gite.
setbuf(标准输出,NULL); -> 关闭标准输出缓冲
setbuf(stdout,NULL); ->turns off stdout buffering