string replaced with stream

This commit is contained in:
badaix 2015-08-12 22:03:04 +02:00
parent 0cc2c39357
commit d51cb125fc
2 changed files with 11 additions and 9 deletions

View file

@ -44,24 +44,25 @@ std::string Log::Timestamp()
int Log::sync() int Log::sync()
{ {
if (buffer_.length()) if (buffer_.str().length())
{ {
if (priority_ == kDbg) if (priority_ == kDbg)
#ifdef DEBUG_LOG #ifdef DEBUG_LOG
std::cout << Timestamp() << " [dbg] " << buffer_.c_str() << std::flush; std::cout << Timestamp() << " [dbg] " << buffer_.str() << std::flush;
#else #else
; ;
#endif #endif
else if (priority_ == kOut) else if (priority_ == kOut)
std::cout << Timestamp() << " [out] " << buffer_.c_str() << std::flush; std::cout << Timestamp() << " [out] " << buffer_.str() << std::flush;
else if (priority_ == kErr) else if (priority_ == kErr)
std::cout << Timestamp() << " [err] " << buffer_.c_str() << std::flush; std::cout << Timestamp() << " [err] " << buffer_.str() << std::flush;
else else
{ {
std::cout << Timestamp() << " [" << std::to_string(priority_) << "] " << buffer_.c_str() << std::flush; std::cout << Timestamp() << " [" << std::to_string(priority_) << "] " << buffer_.str() << std::flush;
syslog(priority_, "%s", buffer_.c_str()); syslog(priority_, "%s", buffer_.str().c_str());
} }
buffer_.erase(); buffer_.str("");
buffer_.clear();
priority_ = kLogDebug; // default to debug for each message priority_ = kLogDebug; // default to debug for each message
} }
return 0; return 0;
@ -72,7 +73,7 @@ int Log::overflow(int c)
{ {
if (c != EOF) if (c != EOF)
{ {
buffer_ += static_cast<char>(c); buffer_ << static_cast<char>(c);
if (c == '\n') if (c == '\n')
sync(); sync();
} }

View file

@ -23,6 +23,7 @@
#include <syslog.h> #include <syslog.h>
#include <iostream> #include <iostream>
#include <cstring> #include <cstring>
#include <sstream>
#define logD std::clog << kDbg #define logD std::clog << kDbg
#define logO std::clog << kOut #define logO std::clog << kOut
@ -57,7 +58,7 @@ 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 Timestamp();
std::string buffer_; std::stringstream buffer_;
int facility_; int facility_;
LogPriority priority_; LogPriority priority_;
char ident_[50]; char ident_[50];