diff --git a/common/log.cpp b/common/log.cpp index 53866993..25b1cf99 100644 --- a/common/log.cpp +++ b/common/log.cpp @@ -19,6 +19,7 @@ #include "log.h" #include #include +#include Log::Log(std::string ident, int facility) { @@ -31,7 +32,7 @@ Log::Log(std::string ident, int facility) } -std::string Log::Timestamp() +std::string Log::Timestamp() const { struct tm * dt; char buffer [30]; @@ -42,6 +43,43 @@ std::string Log::Timestamp() } +std::string Log::toString(LogPriority logPriority) const +{ + switch (logPriority) + { + case kDbg: + return "dbg"; + case kOut: + return "out"; + case kState: + return "state"; + case kErr: + return "err"; + + case kLogEmerg: + return "Emerg"; + case kLogAlert: + return "Alert"; + case kLogCrit: + return "Crit"; + case kLogErr: + return "Err"; + case kLogWarning: + return "Warning"; + case kLogNotice: + return "Notice"; + case kLogInfo: + return "Info"; + case kLogDebug: + return "Debug"; + default: + std::stringstream ss; + ss << logPriority; + return ss.str(); + } +} + + int Log::sync() { if (buffer_.str().length()) @@ -52,15 +90,11 @@ int Log::sync() #else ; #endif - else if (priority_ == kOut) - std::cout << Timestamp() << " [out] " << buffer_.str() << std::flush; - else if (priority_ == kState) - std::cout << Timestamp() << " [state] " << buffer_.str() << std::flush; - else if (priority_ == kErr) - std::cout << Timestamp() << " [err] " << buffer_.str() << std::flush; + else if ((priority_ == kOut) || (priority_ == kState) || (priority_ == kErr)) + std::cout << Timestamp() << " [" << toString(priority_) << "] " << buffer_.str() << std::flush; else { - std::cout << Timestamp() << " [" << (int)priority_ << "] " << buffer_.str() << std::flush; + std::cout << Timestamp() << " [" << toString(priority_) << "] " << buffer_.str() << std::flush; syslog(priority_, "%s", buffer_.str().c_str()); } buffer_.str(""); diff --git a/common/log.h b/common/log.h index 1b9714ae..3b8add6b 100644 --- a/common/log.h +++ b/common/log.h @@ -58,7 +58,8 @@ protected: private: friend std::ostream& operator<< (std::ostream& os, const LogPriority& log_priority); - std::string Timestamp(); + std::string toString(LogPriority logPriority) const; + std::string Timestamp() const; std::stringstream buffer_; int facility_; LogPriority priority_;