hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
LoggerStream.cpp
Go to the documentation of this file.
1 #include <Poco/FileChannel.h>
2 
3 #include "LoggerStream.hpp"
4 
5 #ifndef USE_POCO_LOGGER
6 #define USE_POCO_LOGGER 1
7 #endif
8 
9 #if USE_POCO_LOGGER == 1
10  #include <Poco/Logger.h>
11 #else
12  //TODO
13 #endif
14 
15 namespace HCE
16 {
17 //-----------------------------------------------------------------------------
18 LoggerStream::LoggerStream(const std::string& source_)
19 : inherited(), logBuf(), pBuf(nullptr), prefix(""), suffix(""), logPriority(Priority::PRIO_NONE), source(source_)
20 {
22  resetLoggable();
23 }
24 //-----------------------------------------------------------------------------
26 {
27 }
28 //-----------------------------------------------------------------------------
30 {
31  logBuf.str("");
32  pBuf = &logBuf;
33  this->rdbuf(pBuf);
34 }
35 //-----------------------------------------------------------------------------
36 void LoggerStream::setLogStream(std::ostream& os_)
37 {
38  pBuf=os_.rdbuf();
39  this->rdbuf(pBuf);
40 }
41 //-----------------------------------------------------------------------------
42 std::string LoggerStream::logMsg(bool isReset)
43 {
44  std::string result = logBuf.str();
45  if (isReset)
46  logBuf.str("");
47  return result;
48 }
49 //-----------------------------------------------------------------------------
50 std::ostream& LoggerStream::log(unsigned int logPriority_)
51 {
52  logPriority = logPriority_;
53  return *this;
54 }
55 //-----------------------------------------------------------------------------
56 std::ostream& LoggerStream::log(Priority priority)
57 {
58  return log(static_cast<unsigned int>(priority));
59 }
60 //-----------------------------------------------------------------------------
62 {
63  loggable = loggable_;
64 }
65 //-----------------------------------------------------------------------------
67 {
68  loggable = [](const std::string& data) {return data;};
69 }
70 //-----------------------------------------------------------------------------
71 std::ostream& LoggerStream::put(const std::string& data)
72 {
73  *this << prefix << loggable(data) << suffix;
74  return *this;
75 }
76 //-----------------------------------------------------------------------------
78 {
79  std::stringstream str;
80  str << rdbuf();
81  logBuf.str("");
82  std::endl(put(str.str()));
83  return *this;
84 }
85 //-----------------------------------------------------------------------------
87 {
88  std::stringstream str;
89  str << rdbuf();
90  logBuf.str("");
91  std::flush(put(str.str()));
92  return *this;
93 }
94 //-----------------------------------------------------------------------------
96 {
97  try
98  {
99 #if USE_POCO_LOGGER == 1
100  static Poco::Logger& logger = Poco::Logger::root();
101  logger.log(Poco::Message(getSource(), logMsg(), static_cast<Poco::Message::Priority>(getLogPriority())));
102 #else
103  //TODO (implementation in future)
104 #endif
105  }
106  catch(Poco::Exception& e)
107  {
108  std::cerr << "LoggerStream: " << e.displayText() << std::endl;
109  }
110 }
111 //-----------------------------------------------------------------------------
112 //-----------------------------------------------------------------------------
113 std::ostream& endl(std::ostream& os)
114 {
115  if (typeid(os)==typeid(LoggerStream))
116  {
117  LoggerStream& ls = reinterpret_cast<LoggerStream&>(os);
118  ls.endl();
119  ls.saveSafe();
120  }
121  else
122  std::endl(os);
123  return os;
124 }
125 //-----------------------------------------------------------------------------
126 std::ostream& flush(std::ostream& os)
127 {
128  if (typeid(os)==typeid(LoggerStream))
129  {
130  LoggerStream& ls = reinterpret_cast<LoggerStream&>(os);
131  ls.flush();
132  ls.saveSafe();
133  }
134  else
135  std::flush(os);
136  return os;
137 }
138 //-----------------------------------------------------------------------------
139 LoggerStream& operator<<(LoggerStream& os, const std::string& rhs)
140 {
141  os.log(os.getLogPriority()) << rhs;
142  return os;
143 }
144 //-----------------------------------------------------------------------------
145 LoggerStream& operator<<(LoggerStream& os, const char* rhs)
146 {
147  os.log(os.getLogPriority()) << rhs;
148  return os;
149 }
150 //-----------------------------------------------------------------------------
151 //-----------------------------------------------------------------------------
152 } // end namespace HCE