[英]C++ - How to make application-logs written to disk even in case of a sudden power down
A short background:简短的背景:
I'm developing an embedded Linux application on a drone.我正在无人机上开发嵌入式 Linux 应用程序。 In the last field test we had, the battery juice ran out without any warning during the flight.
在我们进行的最后一次现场测试中,电池电量在飞行过程中没有任何警告就耗尽了。 The drone had a rough landing.
这架无人机艰难着陆。 When I restarted it in the lab, out of 20 log files (*CSV and *TXT of different application modules), only 3 had data, and the rest were completely empty, but obviously, they had.
当我在实验室重新启动它时,在 20 个日志文件(不同应用程序模块的 *CSV 和 *TXT)中,只有 3 个有数据,而 rest 完全是空的,但显然它们有。 to have data and lots of it, (14 minutes flight = 840 seconds, and at 50Hz frequency. we are expected to have 10ths of thousands of data lines in our logs).
拥有大量数据(14 分钟飞行 = 840 秒,频率为 50Hz。我们的日志中预计会有千分之一的数据行)。
So I have 2 questions:所以我有两个问题:
Is it possible that while some of the logs flushed every few seconds (or less - maybe every write command), other logs are kept in a big buffer that is being flushed to memory only on a clean power down or when the buffer is getting exhausted?是否有可能,虽然某些日志每隔几秒(或更少 - 可能是每个写入命令)刷新一次,但其他日志保存在一个大缓冲区中,仅在干净断电或缓冲区耗尽时才刷新到 memory ?
Given that we are using C++, what is the best practice to make all logs being flushed more frequently to the disk (so in case of a sudden power down, I still have some logs to inspect), and still have as little as possible impact on performance?鉴于我们使用的是 C++,让所有日志更频繁地刷新到磁盘的最佳做法是什么(所以在突然断电的情况下,我仍然有一些日志要检查),并且仍然具有尽可能小的影响关于性能?
thanks谢谢
The fundamental issue here is your program, your language, your operating system, and your hardware may each be buffering writes in order to delay them until the most opportune time.这里的基本问题是您的程序、您的语言、您的操作系统和您的硬件可能都在缓冲写入,以便将它们延迟到最合适的时间。
Are you REALLY having performance problems with flushing each write?你真的有刷新每次写入的性能问题吗? Or is that just theoretical?
或者这只是理论上的?
Use one or more of the following recommendations as appropriate to your situation.根据您的情况使用以下一项或多项建议。
I had to write an application with file integrity and I did need to buffer writes (because an event during processing could tell the application "don't write those queued up writes, we change our mind").我必须编写一个具有文件完整性的应用程序,并且我确实需要缓冲写入(因为处理期间的事件可以告诉应用程序“不要写那些排队的写入,我们改变主意”)。 To avoid complications everything is combined into a single string then written with a single write() function when the time comes.
为避免复杂性,所有内容都组合成一个字符串,然后在时机成熟时使用单个 write() function 写入。 I've never had a corrupt file even when the app was shutdown will kill -9.
即使应用程序关闭会杀死-9,我也从来没有损坏过文件。 HOWEVER, it's never had power failures.
但是,它从未出现过电源故障。 (UPS backed up).
(UPS 备份)。
If you are working with a very underpowered platform, then a lot of the above may not be useful.如果您正在使用一个功能非常不足的平台,那么上面的很多内容可能没有用。 Sorry, there wasn't enough detail in what you had to work with.
抱歉,您必须处理的内容不够详细。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.