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 <boost/log/sinks.hpp> |
| 39 | #include <mutex> |
| 40 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 41 | namespace nfd { |
| 42 | |
| 43 | class LoggerFactory : noncopyable |
| 44 | { |
| 45 | public: |
| 46 | |
| 47 | class Error : public std::runtime_error |
| 48 | { |
| 49 | public: |
| 50 | explicit |
| 51 | Error(const std::string& error) |
| 52 | : std::runtime_error(error) |
| 53 | { |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | static LoggerFactory& |
| 58 | getInstance(); |
| 59 | |
| 60 | void |
| 61 | setConfigFile(ConfigFile& config); |
| 62 | |
| 63 | void |
| 64 | onConfig(const ConfigSection& section, bool isDryRun, const std::string& filename); |
| 65 | |
| 66 | std::list<std::string> |
| 67 | getModules() const; |
| 68 | |
| 69 | static Logger& |
| 70 | create(const std::string& moduleName); |
| 71 | |
| 72 | |
| 73 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 74 | |
| 75 | // these methods are used during unit-testing |
| 76 | |
| 77 | LogLevel |
| 78 | getDefaultLevel() const; |
| 79 | |
| 80 | void |
| 81 | setDefaultLevel(LogLevel level); |
| 82 | |
Yumin Xia | ab49745 | 2016-05-10 20:23:24 +0800 | [diff] [blame] | 83 | void |
| 84 | flushBackend(); |
| 85 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 86 | private: |
| 87 | |
| 88 | LoggerFactory(); |
| 89 | |
Yumin Xia | ab49745 | 2016-05-10 20:23:24 +0800 | [diff] [blame] | 90 | ~LoggerFactory(); |
| 91 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 92 | Logger& |
| 93 | createLogger(const std::string& moduleName); |
| 94 | |
| 95 | LogLevel |
| 96 | parseLevel(const std::string& level); |
| 97 | |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 98 | LogLevel |
| 99 | extractLevel(const ConfigSection& item, const std::string& key); |
| 100 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 101 | private: |
| 102 | |
| 103 | typedef std::map<std::string, LogLevel> LevelMap; |
| 104 | typedef std::pair<std::string, LogLevel> NameAndLevel; |
| 105 | |
| 106 | LevelMap m_levelNames; |
| 107 | |
| 108 | typedef std::map<std::string, Logger> LoggerMap; |
| 109 | typedef std::pair<std::string, Logger> NameAndLogger; |
| 110 | |
| 111 | LoggerMap m_loggers; |
Alexander Afanasyev | 6b34ab9 | 2015-02-11 21:22:46 -0800 | [diff] [blame] | 112 | mutable std::mutex m_loggersGuard; |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 113 | |
| 114 | LogLevel m_defaultLevel; |
Yumin Xia | ab49745 | 2016-05-10 20:23:24 +0800 | [diff] [blame] | 115 | |
| 116 | typedef boost::log::sinks::asynchronous_sink<boost::log::sinks::text_ostream_backend> Sink; |
| 117 | boost::shared_ptr<Sink> m_sink; |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | inline LogLevel |
| 121 | LoggerFactory::getDefaultLevel() const |
| 122 | { |
| 123 | return m_defaultLevel; |
| 124 | } |
| 125 | |
| 126 | } // namespace nfd |
| 127 | |
Alexander Afanasyev | 8269a05 | 2015-02-09 16:25:36 -0800 | [diff] [blame] | 128 | #endif // HAVE_CUSTOM_LOGGER |
| 129 | |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 130 | #endif // NFD_CORE_LOGGER_FACTORY_HPP |