blob: e61d60fe11ebfce954de31d0c6ecb858403ea8ae [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 <boost/log/sinks.hpp>
39#include <mutex>
40
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060041namespace nfd {
42
43class LoggerFactory : noncopyable
44{
45public:
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
73PUBLIC_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 Xiaab497452016-05-10 20:23:24 +080083 void
84 flushBackend();
85
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060086private:
87
88 LoggerFactory();
89
Yumin Xiaab497452016-05-10 20:23:24 +080090 ~LoggerFactory();
91
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060092 Logger&
93 createLogger(const std::string& moduleName);
94
95 LogLevel
96 parseLevel(const std::string& level);
97
Alexander Afanasyev5959b012014-06-02 19:18:12 +030098 LogLevel
99 extractLevel(const ConfigSection& item, const std::string& key);
100
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600101private:
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 Afanasyev6b34ab92015-02-11 21:22:46 -0800112 mutable std::mutex m_loggersGuard;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600113
114 LogLevel m_defaultLevel;
Yumin Xiaab497452016-05-10 20:23:24 +0800115
116 typedef boost::log::sinks::asynchronous_sink<boost::log::sinks::text_ostream_backend> Sink;
117 boost::shared_ptr<Sink> m_sink;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600118};
119
120inline LogLevel
121LoggerFactory::getDefaultLevel() const
122{
123 return m_defaultLevel;
124}
125
126} // namespace nfd
127
Alexander Afanasyev8269a052015-02-09 16:25:36 -0800128#endif // HAVE_CUSTOM_LOGGER
129
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -0600130#endif // NFD_CORE_LOGGER_FACTORY_HPP