logger-factory.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #ifndef NFD_CORE_LOGGER_FACTORY_HPP
27 #define NFD_CORE_LOGGER_FACTORY_HPP
28 
29 #include "common.hpp"
30 
31 #ifdef HAVE_CUSTOM_LOGGER
32 #include "custom-logger-factory.hpp"
33 #else
34 
35 #include "config-file.hpp"
36 #include "logger.hpp"
37 
38 #include <mutex>
39 
40 namespace nfd {
41 
42 class LoggerFactory : noncopyable
43 {
44 public:
45 
46  class Error : public std::runtime_error
47  {
48  public:
49  explicit
50  Error(const std::string& error)
51  : std::runtime_error(error)
52  {
53  }
54  };
55 
56  static LoggerFactory&
57  getInstance();
58 
59  void
60  setConfigFile(ConfigFile& config);
61 
62  void
63  onConfig(const ConfigSection& section, bool isDryRun, const std::string& filename);
64 
65  std::list<std::string>
66  getModules() const;
67 
68  static Logger&
69  create(const std::string& moduleName);
70 
71 
73 
74  // these methods are used during unit-testing
75 
76  LogLevel
77  getDefaultLevel() const;
78 
79  void
80  setDefaultLevel(LogLevel level);
81 
82  void
83  flushBackend();
84 
85 private:
86 
87  LoggerFactory();
88 
89  ~LoggerFactory();
90 
91  Logger&
92  createLogger(const std::string& moduleName);
93 
94  LogLevel
95  parseLevel(const std::string& level);
96 
97  LogLevel
98  extractLevel(const ConfigSection& item, const std::string& key);
99 
100 private:
101 
102  typedef std::map<std::string, LogLevel> LevelMap;
103  typedef std::pair<std::string, LogLevel> NameAndLevel;
104 
105  LevelMap m_levelNames;
106 
107  typedef std::map<std::string, Logger> LoggerMap;
108  typedef std::pair<std::string, Logger> NameAndLogger;
109 
110  LoggerMap m_loggers;
111  mutable std::mutex m_loggersGuard;
112 
113  LogLevel m_defaultLevel;
114 };
115 
116 inline LogLevel
117 LoggerFactory::getDefaultLevel() const
118 {
119  return m_defaultLevel;
120 }
121 
122 } // namespace nfd
123 
124 #endif // HAVE_CUSTOM_LOGGER
125 
126 #endif // NFD_CORE_LOGGER_FACTORY_HPP
configuration file parsing utility
Definition: config-file.hpp:50
void setConfigFile(ConfigFile &config)
void onConfig(const ConfigSection &section, bool isDryRun, const std::string &filename)
static LoggerFactory & getInstance()
STL namespace.
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
boost::property_tree::ptree ConfigSection
Definition: config-file.hpp:35
static Logger & create(const std::string &moduleName)
#define PUBLIC_WITH_TESTS_ELSE_PRIVATE
Definition: common.hpp:39
LogLevel
indicates a log level
Definition: logger.hpp:42
provides logging for a module
Definition: logger.hpp:58
Error(const std::string &error)
std::list< std::string > getModules() const