blob: 606ac1c9124bcc53d158c8721a2d27a6f4187f61 [file] [log] [blame]
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev8269a052015-02-09 16:25:36 -08003 * Copyright (c) 2014-2015, 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 * 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
38namespace nfd {
39
40class LoggerFactory : noncopyable
41{
42public:
43
44 class Error : public std::runtime_error
45 {
46 public:
47 explicit
48 Error(const std::string& error)
49 : std::runtime_error(error)
50 {
51 }
52 };
53
54 static LoggerFactory&
55 getInstance();
56
57 void
58 setConfigFile(ConfigFile& config);
59
60 void
61 onConfig(const ConfigSection& section, bool isDryRun, const std::string& filename);
62
63 std::list<std::string>
64 getModules() const;
65
66 static Logger&
67 create(const std::string& moduleName);
68
69
70PUBLIC_WITH_TESTS_ELSE_PRIVATE:
71
72 // these methods are used during unit-testing
73
74 LogLevel
75 getDefaultLevel() const;
76
77 void
78 setDefaultLevel(LogLevel level);
79
80private:
81
82 LoggerFactory();
83
84 Logger&
85 createLogger(const std::string& moduleName);
86
87 LogLevel
88 parseLevel(const std::string& level);
89
Alexander Afanasyev5959b012014-06-02 19:18:12 +030090 LogLevel
91 extractLevel(const ConfigSection& item, const std::string& key);
92
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060093private:
94
95 typedef std::map<std::string, LogLevel> LevelMap;
96 typedef std::pair<std::string, LogLevel> NameAndLevel;
97
98 LevelMap m_levelNames;
99
100 typedef std::map<std::string, Logger> LoggerMap;
101 typedef std::pair<std::string, Logger> NameAndLogger;
102
103 LoggerMap m_loggers;
Alexander Afanasyev6b34ab92015-02-11 21:22:46 -0800104 mutable std::mutex m_loggersGuard;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600105
106 LogLevel m_defaultLevel;
107};
108
109inline LogLevel
110LoggerFactory::getDefaultLevel() const
111{
112 return m_defaultLevel;
113}
114
115} // namespace nfd
116
Alexander Afanasyev8269a052015-02-09 16:25:36 -0800117#endif // HAVE_CUSTOM_LOGGER
118
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600119#endif // NFD_CORE_LOGGER_FACTORY_HPP