easyloggingpp icon indicating copy to clipboard operation
easyloggingpp copied to clipboard

How do I close the log, delete the log, and then start logging again while the program is running

Open hgc1 opened this issue 3 years ago • 1 comments

hgc1 avatar May 26 '22 06:05 hgc1

I wrote this a couple of years ago to move the log from WinPE without a formatted drive, to a formatted drive before reboot so post reboot the logging could continue. I dug around in the code just enought to accomplish the task. Once it did what I wanted, I moved on, so I'm not sure if this is the "proper" way to do it or not.

While I move the log and continue logging, your use case would be simpler. Instead of copying the file, just std::filesystem::remove(). Hope this helps.

// Get current configuration and log file name
auto logger = el::Loggers::getLogger("default");
auto config = logger->configurations();
std::filesystem::path current_log_file = logger->typedConfigurations()->filename(el::Level::Info);

// Shut down the logging file.
logger->typedConfigurations()->acquireLock();
el::base::type::EnumType starting_level = el::LevelHelper::kMinValid;
el::LevelHelper::forEachLevel(&starting_level, [&](void) -> bool {
     logger->typedConfigurations()->fileStream(el::LevelHelper::castFromInt(starting_level))->close();
     return false;
});

// Copy log file from current_log_file to new_logfile_path

// Restart logging from the new location
config->setGlobally(el::ConfigurationType::Filename, new_logfile_path.string().c_str());
logger->configure(*config);

jidn avatar Jun 08 '22 04:06 jidn