5

我最近在我的 C++ 应用程序中采用了Easylogging++,并且遇到了我希望他们的文档中遗漏的内容。

我希望每次启动我的应用程序时都清除我的日志文件,而不是从以前的应用程序实例中附加日志事件。我意识到我可以在任何日志记录事件之前在启动时删除日志文件,但这似乎是一个 hack。

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

4

2 回答 2

4

从 v9.84 版本开始,可以通过定义 配置宏来配置它。

您需要先#define ELPP_FRESH_LOG_FILE添加#include "easylogging++"

您很可能不想对每个包含都执行此操作。作者推荐使用编译器标志。或者,您可以创建一个包装头。

于 2016-08-29T21:43:32.837 回答
3

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

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

对于任何对我的 hack 工作感兴趣的人,我将在 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);

对不起,讨厌的代码格式。我只是按照目前在 easylogging++.h 中的格式复制了代码,以便可以轻松识别它。

于 2015-01-01T22:47:45.273 回答