hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
LoggerStream.hpp
Go to the documentation of this file.
1 
14 #ifndef LOGGER_STREAM_HPP
15 #define LOGGER_STREAM_HPP
16 
17 #include <functional>
18 #include <iostream>
19 #include <sstream>
20 #include <typeinfo>
21 
22 namespace HCE
23 {
24 //-----------------------------------------------------------------------------
25 typedef std::function<std::string(const std::string&)> Loggable;
26 //-----------------------------------------------------------------------------
27 class LoggerStream : public std::ostream
28 {
29 public:
30  enum Priority
31  {
32  PRIO_NONE = 0, // A none error. In the application turn off any log.
33  PRIO_FATAL = 1, // A fatal error. The application will most likely terminate. This is the highest priority.
34  PRIO_CRITICAL = 2, // A critical error. The application might not be able to continue running successfully.
35  PRIO_ERROR = 3, // An error. An operation did not complete successfully, but the application as a whole is not affected.
36  PRIO_WARNING = 4, // A warning. An operation completed with an unexpected result.
37  PRIO_NOTICE = 5, // A notice, which is an information with just a higher priority.
38  PRIO_INFORMATION = 6, // An informational message, usually denoting the successful completion of an operation.
39  PRIO_DEBUG = 7, // A debugging message.
40  PRIO_TRACE = 8 // A tracing message. This is the lowest priority.
41  };
42 public:
43  explicit LoggerStream(const std::string& source_="");
44  virtual ~LoggerStream(void);
45 
46  void setPrefix(const std::string& prefix_) {prefix=prefix_;}
47  std::string getPrefix(void) const {return prefix;}
48 
49  void setSuffix(const std::string& suffix_) {suffix=suffix_;}
50  std::string getSuffix(void) const {return suffix;}
51 
52  unsigned int getLogPriority(void) const {return logPriority;}
53 
54  void setSource(const std::string& source_) {source=source_;}
55  std::string getSource(void) const {return source;}
56 
57  void resetLogStream(void);
58  void setLogStream(std::ostream& os_);
59  std::string logMsg(bool isReset=true);
60  std::ostream& log(unsigned int logPriority);
61  std::ostream& log(Priority priority);
62 
63  void setLoggable(Loggable loggable_);
64  void resetLoggable(void);
65  std::ostream& put(const std::string&);
66  LoggerStream& endl(void);
67  LoggerStream& flush(void);
68 
69  void saveSafe(void);
70 private:
71  std::stringbuf logBuf;
72  std::basic_streambuf<char>* pBuf;
73 
74  Loggable loggable;
75 
76  std::string prefix;
77  std::string suffix;
78  unsigned int logPriority;
79  std::string source;
80 
81  typedef std::ostream inherited;
82 };
83 //-----------------------------------------------------------------------------
84 //-----------------------------------------------------------------------------
85 std::ostream& endl(std::ostream& os);
86 std::ostream& flush(std::ostream& os);
87 LoggerStream& operator<<(LoggerStream& os, const std::string& rhs);
88 LoggerStream& operator<<(LoggerStream& os, const char* rhs);
89 //-----------------------------------------------------------------------------
90 //-----------------------------------------------------------------------------
91 } // end namespace HCE
92 
93 #endif // LOGGER_STREAM_HPP