Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

easylogging++ : Clearing log file on application startup

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.

like image 286
Clint Weisbrod Avatar asked Nov 28 '14 19:11

Clint Weisbrod


2 Answers

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.

like image 200
Steohan Avatar answered Nov 15 '22 07:11

Steohan


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.

like image 21
Clint Weisbrod Avatar answered Nov 15 '22 06:11

Clint Weisbrod