blob: 5cafc6455e693822ce0893f23c360db936eef2af [file] [log] [blame]
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yumin Xiaab497452016-05-10 20:23:24 +08003 * Copyright (c) 2014-2016, Regents of the University of California,
Alexander Afanasyev8269a052015-02-09 16:25:36 -08004 * 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 Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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 Afanasyev8269a052015-02-09 16:25:36 -080024 */
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060025
26#ifndef NFD_CORE_LOGGER_FACTORY_HPP
27#define NFD_CORE_LOGGER_FACTORY_HPP
28
29#include "common.hpp"
Alexander Afanasyev8269a052015-02-09 16:25:36 -080030
31#ifdef HAVE_CUSTOM_LOGGER
32#include "custom-logger-factory.hpp"
33#else
34
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070035#include "config-file.hpp"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060036#include "logger.hpp"
37
Yumin Xiaab497452016-05-10 20:23:24 +080038#include <mutex>
39
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060040namespace nfd {
41
42class LoggerFactory : noncopyable
43{
44public:
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
72PUBLIC_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 Xiaab497452016-05-10 20:23:24 +080082 void
83 flushBackend();
84
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060085private:
86
87 LoggerFactory();
88
Yumin Xiaab497452016-05-10 20:23:24 +080089 ~LoggerFactory();
90
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060091 Logger&
92 createLogger(const std::string& moduleName);
93
94 LogLevel
95 parseLevel(const std::string& level);
96
Alexander Afanasyev5959b012014-06-02 19:18:12 +030097 LogLevel
98 extractLevel(const ConfigSection& item, const std::string& key);
99
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600100private:
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 Afanasyev6b34ab92015-02-11 21:22:46 -0800111 mutable std::mutex m_loggersGuard;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600112
113 LogLevel m_defaultLevel;
114};
115
116inline LogLevel
117LoggerFactory::getDefaultLevel() const
118{
119 return m_defaultLevel;
120}
121
122} // namespace nfd
123
Alexander Afanasyev8269a052015-02-09 16:25:36 -0800124#endif // HAVE_CUSTOM_LOGGER
125
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600126#endif // NFD_CORE_LOGGER_FACTORY_HPP