Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable reopening log files #595

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions samples/logrotate/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

all:
$(CXX) ../../src/easylogging++.cc main.cpp -o logrotate-sample -DELPP_DEBUG_INFO --std=c++11

75 changes: 75 additions & 0 deletions samples/logrotate/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include "../../src/easylogging++.h"
#include <stdio.h>
#include <iostream>

INITIALIZE_EASYLOGGINGPP

/*
* Send SIGHUP to the application and log is
* reopended
*
* Ex:
* $ ls -l logfile.log
* -rw-r--r-- 1 el staff 200 Dec 8 10:29 logfile.log
* $ rm logfile.log
* $ kill -SIGHUP $(pgrep logrotate-sample)
* $ ls -l logfile.log
* -rw-r--r-- 1 el staff 0 Dec 8 10:33 logfile.log
*
* */

#define loggerName "rotate-logger"

void configure_logger()
{
el::Configurations logConf;
logConf.setToDefault();
logConf.setGlobally(el::ConfigurationType::ToStandardOutput,
"false");
logConf.setGlobally(el::ConfigurationType::ToFile, "true");
logConf.setGlobally(el::ConfigurationType::Filename, "logfile.log");

logConf.set(el::Level::Debug, el::ConfigurationType::Enabled, "true");
logConf.set(el::Level::Debug,
el::ConfigurationType::Format,
"%datetime %level %msg");
el::Logger* logger = el::Loggers::getLogger(loggerName);
el::Loggers::reconfigureLogger(logger, logConf);

}

void handler_hup(int signal)
{
LOG(INFO) << "SIGHUP received - reconfigure logger";
el::Logger* logger = el::Loggers::getLogger(loggerName);
logger->reconfigure();
}

void handler_usr1(int signal)
{
LOG(INFO) << "SIGUSR1 received - unregister logger";
el::Loggers::unregisterLogger(loggerName);
}

int main()
{
std::signal(SIGHUP, handler_hup);
std::signal(SIGUSR1, handler_usr1);

LOG(INFO) << "Configuring logger";
configure_logger();

char c;
c = getchar();
CLOG(DEBUG, loggerName) << "My logger";

while ((c = getchar()) != 'q')
{
CLOG(DEBUG, loggerName) << "My logger";
}

std::cout << "Unregistred my logger\n";

return 0;
}

18 changes: 12 additions & 6 deletions src/easylogging++.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1717,17 +1717,23 @@ void TypedConfigurations::insertFile(Level level, const std::string& fullFilenam
auto create = [&](Level level) {
base::LogStreamsReferenceMap::iterator filestreamIter = m_logStreamsReference->find(resolvedFilename);
base::type::fstream_t* fs = nullptr;
if (filestreamIter == m_logStreamsReference->end()) {
bool needNewStream = filestreamIter == m_logStreamsReference->end();
if (!needNewStream && filestreamIter->second.lock() == nullptr) {
// Filestream is expired and exists in map
needNewStream = true;
m_logStreamsReference->erase(filestreamIter);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There may still be some remaining elements in map that can't be removed

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get that can't be removed.
This erase only one expired file stream.
If it is not expired, why remove?

}
if (needNewStream) {
// We need a completely new stream, nothing to share with
fs = base::utils::File::newFileStream(resolvedFilename);
m_filenameMap.insert(std::make_pair(level, resolvedFilename));
m_fileStreamMap.insert(std::make_pair(level, base::FileStreamPtr(fs)));
m_logStreamsReference->insert(std::make_pair(resolvedFilename, base::FileStreamPtr(m_fileStreamMap.at(level))));
m_logStreamsReference->insert(std::make_pair(resolvedFilename, base::WeakFileStreamPtr(m_fileStreamMap.at(level))));
} else {
// Woops! we have an existing one, share it!
m_filenameMap.insert(std::make_pair(level, filestreamIter->first));
m_fileStreamMap.insert(std::make_pair(level, base::FileStreamPtr(filestreamIter->second)));
fs = filestreamIter->second.get();
m_fileStreamMap.insert(std::make_pair(level, base::FileStreamPtr(filestreamIter->second.lock())));
fs = filestreamIter->second.lock().get();
}
if (fs == nullptr) {
// We display bad file error from newFileStream()
Expand Down Expand Up @@ -1851,8 +1857,8 @@ void RegisteredLoggers::unsafeFlushAll(void) {
ELPP_INTERNAL_INFO(1, "Flushing all log files");
for (base::LogStreamsReferenceMap::iterator it = m_logStreamsReference.begin();
it != m_logStreamsReference.end(); ++it) {
if (it->second.get() == nullptr) continue;
it->second->flush();
if (it->second.lock() == nullptr) continue;
it->second.lock()->flush();
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/easylogging++.h
Original file line number Diff line number Diff line change
Expand Up @@ -1953,7 +1953,8 @@ class Configurations : public base::utils::RegistryWithPred<Configuration, Confi

namespace base {
typedef std::shared_ptr<base::type::fstream_t> FileStreamPtr;
typedef std::map<std::string, FileStreamPtr> LogStreamsReferenceMap;
typedef std::weak_ptr<base::type::fstream_t> WeakFileStreamPtr;
typedef std::map<std::string, WeakFileStreamPtr> LogStreamsReferenceMap;
/// @brief Configurations with data types.
///
/// @detail el::Configurations have string based values. This is whats used internally in order to read correct configurations.
Expand Down