Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yumin Xia | ab49745 | 2016-05-10 20:23:24 +0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
Alexander Afanasyev | 8269a05 | 2015-02-09 16:25:36 -0800 | [diff] [blame] | 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 | * The University of Memphis. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Alexander Afanasyev | 8269a05 | 2015-02-09 16:25:36 -0800 | [diff] [blame] | 24 | */ |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 25 | |
| 26 | #ifndef NFD_CORE_LOGGER_FACTORY_HPP |
| 27 | #define NFD_CORE_LOGGER_FACTORY_HPP |
| 28 | |
| 29 | #include "common.hpp" |
Alexander Afanasyev | 8269a05 | 2015-02-09 16:25:36 -0800 | [diff] [blame] | 30 | |
| 31 | #ifdef HAVE_CUSTOM_LOGGER |
| 32 | #include "custom-logger-factory.hpp" |
| 33 | #else |
| 34 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 35 | #include "config-file.hpp" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 36 | #include "logger.hpp" |
| 37 | |
Yumin Xia | ab49745 | 2016-05-10 20:23:24 +0800 | [diff] [blame] | 38 | #include <mutex> |
| 39 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 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 | |
| 72 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 73 | |
| 74 | // these methods are used during unit-testing |
| 75 | |
| 76 | LogLevel |
| 77 | getDefaultLevel() const; |
| 78 | |
| 79 | void |
| 80 | setDefaultLevel(LogLevel level); |
| 81 | |
Yumin Xia | ab49745 | 2016-05-10 20:23:24 +0800 | [diff] [blame] | 82 | void |
| 83 | flushBackend(); |
| 84 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 85 | private: |
| 86 | |
| 87 | LoggerFactory(); |
| 88 | |
Yumin Xia | ab49745 | 2016-05-10 20:23:24 +0800 | [diff] [blame] | 89 | ~LoggerFactory(); |
| 90 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 91 | Logger& |
| 92 | createLogger(const std::string& moduleName); |
| 93 | |
| 94 | LogLevel |
| 95 | parseLevel(const std::string& level); |
| 96 | |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 97 | LogLevel |
| 98 | extractLevel(const ConfigSection& item, const std::string& key); |
| 99 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 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; |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame] | 111 | mutable std::mutex m_loggersGuard; |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 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 | |
Alexander Afanasyev | 8269a05 | 2015-02-09 16:25:36 -0800 | [diff] [blame] | 124 | #endif // HAVE_CUSTOM_LOGGER |
| 125 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 126 | #endif // NFD_CORE_LOGGER_FACTORY_HPP |