easylogging++ : Clearing log file on application startup
Asked Answered
C

2

5

I've recently adopted Easylogging++ in my C++ application and have run into what I hope is just something left out of their documentation.

I would like my log file to be cleared each time my application is launched, rather than appending log events from previous application instances. I realize I could just delete the log file on startup prior to any logging events, but this seems like a hack.

Any help would be appreciated. Thanks.

Culpa answered 28/11, 2014 at 19:14 Comment(0)
S
4

As of version v9.84 there is the possibility to configure this by defining a configuration macro.

You need to add #define ELPP_FRESH_LOG_FILE before #include "easylogging++"

Most likely you do not want to do this with every include. The author recommends using compiler flags. Alternatively you can create a wrapper header.

Swarthy answered 29/8, 2016 at 21:43 Comment(2)
Just downloaded easylogging++_v9.84.zip. Opened easylogging++.h and noticed this line at the top of the file: // Easylogging++ v9.83Culpa
I confirmed that the code in easylogging++.h is in fact handling the ELPP_FRESH_LOG_FILE compiler def (and it works). Nice! So this appears to be just a case of not updating the comment to reflect the correct version number.Culpa
C
3

I wasn't able to find a solution to this problem without resorting to editing easylogging++.h. Obviously, I was hoping that wouldn't be necessary but I'm quite certain that as of v9.77, there exists no built-in facility for resetting the log file on application launch. By all means, please correct me if I'm wrong.

Inspecting the code, I found that log files are always created in append mode. There doesn't appear to be any further logic that explicitly deletes log files.

For anyone interested in my hack job, I changed the open mode argument passed into the fstream constructor in utils::File::newFileStream() to include fstream::trunc instead of fstream::app. The change occurs near line 1084 in easylogging++.h, v9.77.

Here's the section of code I'm referring to:

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);

Sorry for the nasty code formatting. I just copied the code the way it is currently formatted in easylogging++.h so that it can be easily recognized.

Culpa answered 1/1, 2015 at 22:47 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.