如果应用程序崩溃,Windows 控制台日志记录到文件会丢失尾部; 如何看到尾部?
我们构建了复杂的基于控制台的工具,这些工具在控制台上产生大量评论。 这些工具处理大量数据,因此运行成本很高,我们不喜欢不必要地频繁执行它们,尤其是在我们已经必须重复运行它们的测试期间。 在此类测试期间,我们经常将结果记录到文件中以捕获此注释以供检查: C:> [我们的命令] >log.txt 当应用程序运行完成时,效果非常好。
如果应用程序崩溃(非法内存引用等),Windows 会在注释末尾之前砍掉日志文件,这样我们就看不到崩溃时发生了什么。 它还会在同一位置截断控制台输出。 因此,当我们得到一个被截断的日志文件时,我们必须在没有日志文件的情况下再次执行这一切,才能看到最终的答案。 如果不记录日志,Windows 似乎不会缓冲控制台输出。
有没有办法告诉 Windows 在记录时不要缓冲控制台输出? 我们的应用程序确实使用 (Windows) C 运行时系统,不久前我们使用了“setvbuf” 如下: setvbuf(stdout, NULL, _IONBF, 0); 但无济于事。
We build complicated console-based tools that produce a lot of commentary on the console. These tools process a lot of data and so runs are expensive and we don't like to do them more often than necessary, especially during testing where we already have to run them repeatedly. During such tests, we often log the result to a file to capture this commentary for inspection:
C:> [ourcommand] >log.txt
When the application runs to completion, this works great.
If the application crashes (illegal memory reference, etc.), Windows nastily chops off the log file before the tail end of the commentary so we can't see what was occuring at the point of the crash. It also chops of the console output at the same place. So, when we get an truncated log file, we have to do it all again without the log file, to see the final answer. Windows doesn't seem to buffer the console output if not logging.
Is there a way to tell Windows not to buffer the console output while logging? Our application does use the (Windows) C runtime system and awhile back we used "setvbuf"
as follows:
setvbuf(stdout, NULL, _IONBF, 0);
but to no avail.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以随时刷新控制台的缓冲区:
这使用 TextWriter.Flush()。
如果您需要确保这种情况总是发生,那么为控制台编写一个类似于 WriteLine 的方法会相当容易,但在末尾包含一个刷新语句。
编辑:
您没有指定语言...我将使用 C# 编写一个示例。
在 C# 中,您可以轻松创建一个提供如下方法的静态实用程序类:
然后只需调用:
相同的方法也应该适用于几乎任何其他语言。
编辑 2:
由于您使用的是 C++,因此除了 setvbuf 之外,还有一些您可能感兴趣的选项。
首先,您应该能够通过执行以下操作来关闭 cout 中的缓冲:
您可能还需要设置
sync_with_stdio
如果您将 stdio(即 printf)与 iostream 混合。You can flush the console's buffer at any point:
This uses TextWriter.Flush().
If you need to make sure this always happens, it'd be fairly easy to write a method for console that worked like WriteLine but included a flush statement at the end.
Edit:
You didn't specify a language... I'm going to write an example using C#.
In C#, you could easily make a static utility class that provided a method like:
Then just call:
The same approach should work in pretty much any other language, as well.
Edit 2:
Since you're using C++, there are a couple of options other than setvbuf that may be of interest.
First, you should be able to turn off the buffering in cout by doing:
You also may need to set
sync_with_stdio
if you're mixing stdio (ie: printf) with iostreams.您没有指定语言,但对于 C++,您应该能够调用 setvbuf< /a> 将 stdout 作为第一个参数来关闭缓冲。
You didn't specify language, but for C++ you should be able to call setvbuf with stdout as the first param to turn off buffering.
您似乎正在使用标准输出。 你最好使用“标准错误”。 (C 库中的 stderr,iostream 中的 cerr)这些不被缓冲。 或者,您可以在每次希望同步时使用 FlushFileBuffers(或模拟)。
You are seemingly using standard output. You'd better use "standard error". (stderr in C library, cerr in iostreams) Those are not buffered. Or you can use FlushFileBuffers (or analog), each time you wish to sync.