Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014 Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology |
| 9 | * |
| 10 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 11 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 12 | * |
| 13 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 14 | * of the GNU General Public License as published by the Free Software Foundation, |
| 15 | * either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 18 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 19 | * PURPOSE. See the GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along with |
| 22 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 23 | **/ |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 24 | |
| 25 | #ifndef NFD_CORE_LOGGER_FACTORY_HPP |
| 26 | #define NFD_CORE_LOGGER_FACTORY_HPP |
| 27 | |
| 28 | #include "common.hpp" |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 29 | #include "config-file.hpp" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 30 | #include "logger.hpp" |
| 31 | |
| 32 | namespace nfd { |
| 33 | |
| 34 | class LoggerFactory : noncopyable |
| 35 | { |
| 36 | public: |
| 37 | |
| 38 | class Error : public std::runtime_error |
| 39 | { |
| 40 | public: |
| 41 | explicit |
| 42 | Error(const std::string& error) |
| 43 | : std::runtime_error(error) |
| 44 | { |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | static LoggerFactory& |
| 49 | getInstance(); |
| 50 | |
| 51 | void |
| 52 | setConfigFile(ConfigFile& config); |
| 53 | |
| 54 | void |
| 55 | onConfig(const ConfigSection& section, bool isDryRun, const std::string& filename); |
| 56 | |
| 57 | std::list<std::string> |
| 58 | getModules() const; |
| 59 | |
| 60 | static Logger& |
| 61 | create(const std::string& moduleName); |
| 62 | |
| 63 | |
| 64 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 65 | |
| 66 | // these methods are used during unit-testing |
| 67 | |
| 68 | LogLevel |
| 69 | getDefaultLevel() const; |
| 70 | |
| 71 | void |
| 72 | setDefaultLevel(LogLevel level); |
| 73 | |
| 74 | private: |
| 75 | |
| 76 | LoggerFactory(); |
| 77 | |
| 78 | Logger& |
| 79 | createLogger(const std::string& moduleName); |
| 80 | |
| 81 | LogLevel |
| 82 | parseLevel(const std::string& level); |
| 83 | |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame^] | 84 | LogLevel |
| 85 | extractLevel(const ConfigSection& item, const std::string& key); |
| 86 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 87 | private: |
| 88 | |
| 89 | typedef std::map<std::string, LogLevel> LevelMap; |
| 90 | typedef std::pair<std::string, LogLevel> NameAndLevel; |
| 91 | |
| 92 | LevelMap m_levelNames; |
| 93 | |
| 94 | typedef std::map<std::string, Logger> LoggerMap; |
| 95 | typedef std::pair<std::string, Logger> NameAndLogger; |
| 96 | |
| 97 | LoggerMap m_loggers; |
| 98 | |
| 99 | LogLevel m_defaultLevel; |
| 100 | }; |
| 101 | |
| 102 | inline LogLevel |
| 103 | LoggerFactory::getDefaultLevel() const |
| 104 | { |
| 105 | return m_defaultLevel; |
| 106 | } |
| 107 | |
| 108 | } // namespace nfd |
| 109 | |
| 110 | #endif // NFD_CORE_LOGGER_FACTORY_HPP |