easylogging ++:在应用程序启动时清除日志文件

Cli*_*rod 5 logging reset app-startup easylogging++

我最近在我的C++应用程序中采用了Easylogging ++,并且遇到了我希望只是文档遗漏的东西.

我希望每次启动应用程序时都清除我的日志文件,而不是从以前的应用程序实例添加日志事件.我意识到我可以在任何日志记录事件之前在启动时删除日志文件,但这看起来像是一个黑客.

任何帮助,将不胜感激.谢谢.

Cli*_*rod 3

如果不编辑 easylogging++.h,我无法找到此问题的解决方案。显然,我希望这不是必需的,但我非常确定,从 v9.77 开始,不存在用于在应用程序启动时重置日志文件的内置工具。无论如何,如果我错了,请纠正我。

检查代码,我发现日志文件始终以附加模式创建。似乎没有任何进一步的逻辑显式删除日志文件。

对于任何对我的黑客工作感兴趣的人,我更改了传递到 utils::File::newFileStream() 中 fstream 构造函数的开放模式参数,以包含 fstream::trunc 而不是 fstream::app。该更改发生在 easylogging++.h v9.77 的第 1084 行附近。

这是我所指的代码部分:

namespace utils {
class File : base::StaticClass {
public:
/// @brief Creates new out file stream for specified filename.
/// @return Pointer to newly created fstream or nullptr
static base::type::fstream_t* newFileStream(const std::string& filename) {
    // CLW: Dec 29, 2014:
    // I don't want a log file containing log events from past application instances,
    // but there seems to be no built-in way to reset the log file when the app is started
    // So, I'm changing the open mode in the following fstream constructor call to include 
    // fstream::trunc instead of fstream::app.
    base::type::fstream_t *fs = new base::type::fstream_t(filename.c_str(),
        base::type::fstream_t::out | base::type::fstream_t::trunc);
//  base::type::fstream_t *fs = new base::type::fstream_t(filename.c_str(), 
//      base::type::fstream_t::out | base::type::fstream_t::app);
Run Code Online (Sandbox Code Playgroud)

抱歉,代码格式很糟糕。我只是按照当前在 easylogging++.h 中的格式复制了代码,以便可以轻松识别它。