Easylogging ++: Clearing the Log File on Application Startup

I recently adopted Easylogging ++ in my C ++ application and ran into what I hope is just something missing in the documentation.

I would like my log file to be cleared every time my application started, instead of adding log events from previous instances of the application. I understand that I could just delete the log file on startup prior to any logging events, but that seems like a hack.

Any help would be greatly appreciated. Thank.

+3


source to share


2 answers


I have not been able to find a solution to this problem without resorting to easylogging ++ editing. h. Obviously I was hoping it wouldn't be necessary, but I'm pretty sure as of version 9.77 there is no built-in facility to flush the log file on application startup. By all means, please correct me if I am wrong.

By checking the code, I found that the log files are always created in append mode. There doesn't seem to be any additional logic that explicitly deletes the log files.

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



Here's the section of code I mean:

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 annoying code formatting. I just copied the code the way it is currently formatted in easylogging ++. H so that it can be easily recognized.

+3


source


Since v9.84 it is possible to customize this by defining a configuration macro .

You need to add #define ELPP_FRESH_LOG_FILE

before#include "easylogging++"



Chances are you don't want to do this every time you turn it on. The author recommends using compiler flags. Alternatively, you can create a wrapper header.

+2


source







All Articles