LogPriority to string

This commit is contained in:
badaix 2016-03-22 23:36:57 +01:00
parent 0ed7ba89ca
commit 2872527e0b
2 changed files with 44 additions and 9 deletions

View file

@ -19,6 +19,7 @@
#include "log.h" #include "log.h"
#include <iomanip> #include <iomanip>
#include <ctime> #include <ctime>
#include <sstream>
Log::Log(std::string ident, int facility) 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; struct tm * dt;
char buffer [30]; 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() int Log::sync()
{ {
if (buffer_.str().length()) if (buffer_.str().length())
@ -52,15 +90,11 @@ int Log::sync()
#else #else
; ;
#endif #endif
else if (priority_ == kOut) else if ((priority_ == kOut) || (priority_ == kState) || (priority_ == kErr))
std::cout << Timestamp() << " [out] " << buffer_.str() << std::flush; std::cout << Timestamp() << " [" << toString(priority_) << "] " << 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 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()); syslog(priority_, "%s", buffer_.str().c_str());
} }
buffer_.str(""); buffer_.str("");

View file

@ -58,7 +58,8 @@ protected:
private: private:
friend std::ostream& operator<< (std::ostream& os, const LogPriority& log_priority); 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_; std::stringstream buffer_;
int facility_; int facility_;
LogPriority priority_; LogPriority priority_;