Allow log levels to be tweaked at run time.

This commit is contained in:
JoelKatz
2012-10-17 23:39:14 -07:00
parent bacfcff48c
commit e534f0ba78
4 changed files with 123 additions and 14 deletions

View File

@@ -4,6 +4,7 @@
#include <fstream>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/algorithm/string.hpp>
boost::recursive_mutex Log::sLock;
@@ -15,6 +16,28 @@ uint32 Log::logRotateCounter = 0;
LogPartition* LogPartition::headLog = NULL;
LogPartition::LogPartition(const char *name) : mNextLog(headLog), mMinSeverity(lsWARNING)
{
const char *ptr = strrchr(name, '/');
mName = (ptr == NULL) ? name : (ptr + 1);
size_t p = mName.find(".cpp");
if (p != std::string::npos)
mName.erase(mName.begin() + p, mName.end());
headLog = this;
}
std::vector< std::pair<std::string, std::string> > LogPartition::getSeverities()
{
std::vector< std::pair<std::string, std::string> > sevs;
for (LogPartition *l = headLog; l != NULL; l = l->mNextLog)
sevs.push_back(std::make_pair(l->mName, Log::severityToString(l->mMinSeverity)));
return sevs;
}
Log::~Log()
{
std::string logMsg = boost::posix_time::to_simple_string(boost::posix_time::second_clock::universal_time());
@@ -26,6 +49,7 @@ Log::~Log()
case lsWARNING: logMsg += " WARN "; break;
case lsERROR: logMsg += " EROR "; break;
case lsFATAL: logMsg += " FATL "; break;
case lsINVALID: assert(false); return;
}
logMsg += oss.str();
boost::recursive_mutex::scoped_lock sl(sLock);
@@ -84,6 +108,44 @@ void Log::setMinSeverity(LogSeverity s)
LogPartition::setSeverity(s);
}
LogSeverity Log::getMinSeverity()
{
boost::recursive_mutex::scoped_lock sl(sLock);
return sMinSeverity;
}
std::string Log::severityToString(LogSeverity s)
{
switch (s)
{
case lsTRACE: return "Trace";
case lsDEBUG: return "Debug";
case lsINFO: return "Info";
case lsWARNING: return "Warning";
case lsERROR: return "Error";
case lsFATAL: return "Fatal";
default: assert(false); return "Unknown";
}
}
LogSeverity Log::stringToSeverity(const std::string& s)
{
if (boost::iequals(s, "trace"))
return lsTRACE;
if (boost::iequals(s, "debug"))
return lsDEBUG;
if (boost::iequals(s, "info") || boost::iequals(s, "information"))
return lsINFO;
if (boost::iequals(s, "warn") || boost::iequals(s, "warning") || boost::iequals(s, "warnings"))
return lsWARNING;
if (boost::iequals(s, "error") || boost::iequals(s, "errors"))
return lsERROR;
if (boost::iequals(s, "fatal") || boost::iequals(s, "fatals"))
return lsFATAL;
return lsINVALID;
}
void Log::setLogFile(boost::filesystem::path path)
{
std::ofstream* newStream = new std::ofstream(path.c_str(), std::fstream::app);
@@ -103,14 +165,15 @@ void Log::setLogFile(boost::filesystem::path path)
pathToLog = new boost::filesystem::path(path);
}
void LogPartition::setSeverity(const char *partition, LogSeverity severity)
bool LogPartition::setSeverity(const std::string& partition, LogSeverity severity)
{
for (LogPartition *p = headLog; p != NULL; p = p->mNextLog)
if (p->mName == partition)
if (boost::iequals(p->mName, partition))
{
p->mMinSeverity = severity;
return;
return true;
}
return false;
}
void LogPartition::setSeverity(LogSeverity severity)