如何在不缓冲 stdout 和 stderr 的情况下记录 make 输出

发布于 2024-07-13 23:17:28 字数 525 浏览 8 评论 0原文

我在记录自动构建的输出时遇到问题。

构建是通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

此岸叶落 2024-07-20 23:17:28
make -k > build.log 2>&1

这应该更适合您,因为它不是分别重定向 stderr 和 stdout,而是将 stderr 重定向到 stdout,这应该使缓冲同步。

如果您想将其记录到文件并将其打印到控制台:

make -k 2>&1 | tee build.log
make -k > build.log 2>&1

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:

make -k 2>&1 | tee build.log
夏花。依旧 2024-07-20 23:17:28

尝试这个

make -k > build.log 2>&1

Try this

make -k > build.log 2>&1
海之角 2024-07-20 23:17:28

我可以将输出捕获为 2
单独的日志文件,但我会
没有关于如何将它们粘回去的信息
一起放入一个日志文件中。

将它们重新粘在一起很棘手,但也有一个正确的答案,而且它有效! 请参阅“如何将 stdout 和 stderr 保存或重定向到不同的位置文件?”,作者:Vivek Gite。

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.

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.

独孤求败 2024-07-20 23:17:28

setbuf(标准输出,NULL); -> 关闭标准输出缓冲

setbuf(stdout,NULL); ->turns off stdout buffering

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文