FREEDM DGI
CLogger.hpp
Go to the documentation of this file.
1 
24 #ifndef CLOGGER_HPP
25 #define CLOGGER_HPP
26 
27 #include <cstdlib>
28 #include <fstream>
29 #include <string>
30 
31 #include <boost/date_time/posix_time/posix_time.hpp>
32 #include <boost/foreach.hpp>
33 #include <boost/iostreams/concepts.hpp>
34 #include <boost/iostreams/stream.hpp>
35 #include <boost/lexical_cast.hpp>
36 #include <boost/program_options.hpp>
37 #include <boost/shared_ptr.hpp>
38 
39 namespace po = boost::program_options;
40 
41 // Pretty function is nonstandard. Fallback to standards if not using GNU C++.
42 // Note that GNU __PRETTY_FUNCTION__ is a local variable, NOT a macro!!!
43 #ifndef __GNUG__
44 #define __PRETTY_FUNCTION__ ( std::string("At ") + basename(__FILE__) + \
45 std::string(" line ") + boost::lexical_cast<std::string>(__LINE__) ).c_str()
46 #endif
47 
48 namespace freedm {
49 namespace broker {
50 
52 std::string basename( const std::string s );
53 
55 
58 
60 class CGlobalLogger : private boost::noncopyable
61 {
69  public:
71  static CGlobalLogger& instance();
73  void RegisterLocalLogger(const std::string logger);
75  void SetOutputLevel(const std::string logger, const unsigned int level);
77  unsigned int GetOutputLevel(const std::string logger) const;
79  void SetGlobalLevel(const unsigned int level);
81  void SetInitialLoggerLevels(const std::string loggerCfgFile);
83  void ListLoggers() const;
84  private:
86  unsigned int m_default;
88  typedef std::map< const std::string, unsigned int > OutputMap;
90  OutputMap m_loggers;
91 };
92 
94 class CLog : public boost::iostreams::sink
95 {
102  public:
104  CLog(const CLoggerPointer p, const unsigned int level_,
105  const std::string name_, std::ostream* const out_= &std::clog );
107  std::streamsize write( const char* const s, std::streamsize n);
109  unsigned int GetOutputLevel() const;
110  private:
112  const CLoggerPointer m_parent;
114  unsigned int m_level;
116  const std::string m_name;
118  std::ostream * const m_ostream;
119 };
120 
121 class CLocalLogger : private boost::noncopyable
122 {
132  public:
134  CLocalLogger(const std::string loggername);
136  boost::iostreams::stream<CLog> Trace;
138  boost::iostreams::stream<CLog> Debug;
140  boost::iostreams::stream<CLog> Info;
142  boost::iostreams::stream<CLog> Notice;
144  boost::iostreams::stream<CLog> Status;
146  boost::iostreams::stream<CLog> Warn;
148  boost::iostreams::stream<CLog> Error;
150  boost::iostreams::stream<CLog> Alert;
152  boost::iostreams::stream<CLog> Fatal;
154  std::string GetName() const;
156  unsigned int GetOutputLevel() const;
158  void SetOutputLevel(const unsigned int level);
159 
160  private:
162  const std::string m_name;
163 };
164 
165 } // namespace broker
166 } // namespace freedm
167 
168 #endif
boost::iostreams::stream< CLog > Info
Logger.
Definition: CLogger.hpp:140
const CLoggerPointer m_parent
The local logger managing this logger.
Definition: CLogger.hpp:112
const std::string m_name
String name of this logger.
Definition: CLogger.hpp:116
unsigned int GetOutputLevel(const std::string logger) const
Fetch the logging level of a specific logger.
Definition: CLogger.cpp:241
void RegisterLocalLogger(const std::string logger)
Register a local logger with the global logger.
Definition: CLogger.cpp:196
void SetInitialLoggerLevels(const std::string loggerCfgFile)
Reads the logging levels of all loggers from the config file.
Definition: CLogger.cpp:262
void SetOutputLevel(const std::string logger, const unsigned int level)
Sets the logging level of a specific logger.
Definition: CLogger.cpp:227
static CGlobalLogger & instance()
Retrieves the singleton instance of the global logger.
Definition: CLogger.cpp:183
boost::iostreams::stream< CLog > Debug
Logger.
Definition: CLogger.hpp:138
boost::iostreams::stream< CLog > Alert
Logger.
Definition: CLogger.hpp:150
std::string basename(const std::string s)
Turns a qualified path into just a filename.
Definition: CLogger.cpp:57
void ListLoggers() const
Lists all the avaible loggers and their current levels.
Definition: CLogger.cpp:320
boost::iostreams::stream< CLog > Status
Logger.
Definition: CLogger.hpp:144
Tracks the global logging configuration.
Definition: CLogger.hpp:60
std::ostream *const m_ostream
Output stream to use.
Definition: CLogger.hpp:118
void SetGlobalLevel(const unsigned int level)
Sets the logging level of all loggers.
Definition: CLogger.cpp:208
boost::iostreams::stream< CLog > Notice
Logger.
Definition: CLogger.hpp:142
Definition: CLogger.hpp:121
CLocalLogger * CLoggerPointer
Boost requires this to be a raw pointer.
Definition: CLogger.hpp:54
std::map< const std::string, unsigned int > OutputMap
Type of container for the output levels.
Definition: CLogger.hpp:88
const std::string m_name
The name of this logger.
Definition: CLogger.hpp:162
OutputMap m_loggers
The map of loggers to logger levels.
Definition: CLogger.hpp:90
unsigned int m_default
What the output level is if not set specifically.
Definition: CLogger.hpp:86
boost::iostreams::stream< CLog > Error
Logger.
Definition: CLogger.hpp:148
General FREEDM Namespace.
Definition: CBroker.cpp:53
boost::iostreams::stream< CLog > Trace
Logger.
Definition: CLogger.hpp:136
Logging Output Software.
Definition: CLogger.hpp:94
boost::iostreams::stream< CLog > Fatal
Logger.
Definition: CLogger.hpp:152
boost::iostreams::stream< CLog > Warn
Logger.
Definition: CLogger.hpp:146
unsigned int m_level
The level of this logger.
Definition: CLogger.hpp:114