Skip to content

Commit

Permalink
Implemented logRotation function
Browse files Browse the repository at this point in the history
  • Loading branch information
Martijn van Eijk committed Jan 14, 2016
1 parent 3e6c7f1 commit 14d5e19
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main/CommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ void
CommandHandler::logRotate(std::string const& params, std::string& retStr)
{
retStr = "Log rotate...";
Logging::rotateLogFile();
}

void
Expand Down
20 changes: 20 additions & 0 deletions src/util/Logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
namespace stellar
{
el::Configurations Logging::gDefaultConf;
int32_t Logging::logFileIndex;

void
Logging::setFmt(std::string const& peerID, bool timestamps)
Expand Down Expand Up @@ -60,7 +61,10 @@ Logging::init()
gDefaultConf.setToDefault();
gDefaultConf.setGlobally(el::ConfigurationType::ToStandardOutput, "true");
gDefaultConf.setGlobally(el::ConfigurationType::ToFile, "false");
gDefaultConf.setGlobally(el::ConfigurationType::MaxLogFileSize, "1");
setFmt("<startup>");
logFileIndex = 0;
el::Helpers::installPreRollOutCallback(rolloutHandler);
}

void
Expand All @@ -71,6 +75,22 @@ Logging::setLoggingToFile(std::string const& filename)
el::Loggers::reconfigureAllLoggers(gDefaultConf);
}

void
Logging::rolloutHandler(const char* filename, std::size_t size) {
// size is there to match easylogging++ library template
std::stringstream ss;
ss << "mv " << filename << " " << filename << "." << ++logFileIndex;

This comment has been minimized.

Copy link
@graydon

graydon Jan 18, 2016

I think I'd prefer std::rename() here, not system("mv...")

system(ss.str().c_str());
el::Loggers::removeFlag(el::LoggingFlag::StrictLogFileSizeCheck);
}


void
Logging::rotateLogFile()
{
el::Loggers::addFlag(el::LoggingFlag::StrictLogFileSizeCheck);
}

el::Level
Logging::getLogLevel(std::string const& partition)
{
Expand Down
3 changes: 3 additions & 0 deletions src/util/Logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ namespace stellar
class Logging
{
static el::Configurations gDefaultConf;
static int32_t logFileIndex;

public:
static void init();
static void setFmt(std::string const& peerID, bool timestamps = true);
static void setLoggingToFile(std::string const& filename);
static void rotateLogFile();
static void rolloutHandler(const char* filename, std::size_t size);
static void setLogLevel(el::Level level, const char* partition);
static el::Level getLLfromString(std::string const& levelName);
static el::Level getLogLevel(std::string const& partition);
Expand Down

0 comments on commit 14d5e19

Please sign in to comment.