/src/logging-log4cxx/src/main/cpp/logmanager.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Licensed to the Apache Software Foundation (ASF) under one or more |
3 | | * contributor license agreements. See the NOTICE file distributed with |
4 | | * this work for additional information regarding copyright ownership. |
5 | | * The ASF licenses this file to You under the Apache License, Version 2.0 |
6 | | * (the "License"); you may not use this file except in compliance with |
7 | | * the License. You may obtain a copy of the License at |
8 | | * |
9 | | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | | * |
11 | | * Unless required by applicable law or agreed to in writing, software |
12 | | * distributed under the License is distributed on an "AS IS" BASIS, |
13 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | | * See the License for the specific language governing permissions and |
15 | | * limitations under the License. |
16 | | */ |
17 | | |
18 | | #include <log4cxx/logmanager.h> |
19 | | #include <log4cxx/defaultconfigurator.h> |
20 | | #include <log4cxx/spi/defaultrepositoryselector.h> |
21 | | #include <log4cxx/hierarchy.h> |
22 | | #include <log4cxx/spi/rootlogger.h> |
23 | | #include <log4cxx/spi/loggerfactory.h> |
24 | | #include <stdexcept> |
25 | | #include <log4cxx/level.h> |
26 | | #include <log4cxx/spi/loggerrepository.h> |
27 | | #include <log4cxx/helpers/exception.h> |
28 | | #include <log4cxx/helpers/optionconverter.h> |
29 | | #include <log4cxx/helpers/loglog.h> |
30 | | #include <log4cxx/helpers/threadutility.h> |
31 | | |
32 | | #include <log4cxx/spi/loggingevent.h> |
33 | | #include <log4cxx/file.h> |
34 | | #include <log4cxx/helpers/transcoder.h> |
35 | | #if !defined(LOG4CXX) |
36 | | #define LOG4CXX 1 |
37 | | #endif |
38 | | #include <log4cxx/helpers/aprinitializer.h> |
39 | | |
40 | | using namespace LOG4CXX_NS; |
41 | | using namespace LOG4CXX_NS::spi; |
42 | | using namespace LOG4CXX_NS::helpers; |
43 | | |
44 | | IMPLEMENT_LOG4CXX_OBJECT(DefaultRepositorySelector) |
45 | | |
46 | | void* LogManager::guard = 0; |
47 | | |
48 | | RepositorySelectorPtr LogManager::getRepositorySelector() |
49 | 0 | { |
50 | 0 | auto result = APRInitializer::getOrAddUnique<spi::RepositorySelector>( []() -> ObjectPtr |
51 | 0 | { |
52 | 0 | LoggerRepositoryPtr hierarchy = Hierarchy::create(); |
53 | 0 | return std::make_shared<DefaultRepositorySelector>(hierarchy); |
54 | 0 | } |
55 | 0 | ); |
56 | 0 | return result; |
57 | 0 | } |
58 | | |
59 | | void LogManager::setRepositorySelector(spi::RepositorySelectorPtr selector, void* guard1) |
60 | 0 | { |
61 | 0 | if ((LogManager::guard != 0) && (LogManager::guard != guard1)) |
62 | 0 | { |
63 | 0 | throw IllegalArgumentException(LOG4CXX_STR("Attempted to reset the LoggerFactory without possessing the guard.")); |
64 | 0 | } |
65 | | |
66 | 0 | if (selector == 0) |
67 | 0 | { |
68 | 0 | throw IllegalArgumentException(LOG4CXX_STR("RepositorySelector must be non-null.")); |
69 | 0 | } |
70 | | |
71 | 0 | LogManager::guard = guard1; |
72 | 0 | APRInitializer::setUnique<spi::RepositorySelector>(selector); |
73 | 0 | } |
74 | | |
75 | | |
76 | | |
77 | | LoggerRepositoryPtr LogManager::getLoggerRepository() |
78 | 0 | { |
79 | 0 | return getRepositorySelector()->getLoggerRepository(); |
80 | 0 | } |
81 | | |
82 | | LoggerPtr LogManager::getRootLogger() |
83 | 0 | { |
84 | | // Delegate the actual manufacturing of the logger to the logger repository. |
85 | 0 | auto r = getLoggerRepository(); |
86 | 0 | r->ensureIsConfigured(std::bind(DefaultConfigurator::configure, r)); |
87 | 0 | return r->getRootLogger(); |
88 | 0 | } |
89 | | |
90 | | /** |
91 | | Retrieve the appropriate Logger instance. |
92 | | */ |
93 | | LoggerPtr LogManager::getLoggerLS(const LogString& name) |
94 | 0 | { |
95 | 0 | auto r = getLoggerRepository(); |
96 | 0 | r->ensureIsConfigured(std::bind(DefaultConfigurator::configure, r)); |
97 | 0 | return r->getLogger(name); |
98 | 0 | } |
99 | | |
100 | | /** |
101 | | Retrieve the appropriate Logger instance. |
102 | | */ |
103 | | LoggerPtr LogManager::getLoggerLS(const LogString& name, |
104 | | const spi::LoggerFactoryPtr& factory) |
105 | 0 | { |
106 | | // Delegate the actual manufacturing of the logger to the logger repository. |
107 | 0 | auto r = getLoggerRepository(); |
108 | 0 | r->ensureIsConfigured(std::bind(DefaultConfigurator::configure, r)); |
109 | 0 | return r->getLogger(name, factory); |
110 | 0 | } |
111 | | |
112 | | LoggerPtr LogManager::getLogger(const std::string& name) |
113 | 0 | { |
114 | 0 | LOG4CXX_DECODE_CHAR(n, name); |
115 | 0 | return getLoggerLS(n); |
116 | 0 | } |
117 | | |
118 | | LoggerPtr LogManager::getLogger(const std::string& name, |
119 | | const spi::LoggerFactoryPtr& factory) |
120 | 0 | { |
121 | 0 | LOG4CXX_DECODE_CHAR(n, name); |
122 | 0 | return getLoggerLS(n, factory); |
123 | 0 | } |
124 | | |
125 | | LoggerPtr LogManager::exists(const std::string& name) |
126 | 0 | { |
127 | 0 | LOG4CXX_DECODE_CHAR(n, name); |
128 | 0 | return existsLS(n); |
129 | 0 | } |
130 | | |
131 | | #if LOG4CXX_WCHAR_T_API |
132 | | LoggerPtr LogManager::getLogger(const std::wstring& name) |
133 | 0 | { |
134 | 0 | LOG4CXX_DECODE_WCHAR(n, name); |
135 | 0 | return getLoggerLS(n); |
136 | 0 | } |
137 | | |
138 | | LoggerPtr LogManager::getLogger(const std::wstring& name, |
139 | | const spi::LoggerFactoryPtr& factory) |
140 | 0 | { |
141 | 0 | LOG4CXX_DECODE_WCHAR(n, name); |
142 | 0 | return getLoggerLS(n, factory); |
143 | 0 | } |
144 | | |
145 | | LoggerPtr LogManager::exists(const std::wstring& name) |
146 | 0 | { |
147 | 0 | LOG4CXX_DECODE_WCHAR(n, name); |
148 | 0 | return existsLS(n); |
149 | 0 | } |
150 | | #endif |
151 | | |
152 | | #if LOG4CXX_UNICHAR_API |
153 | | LoggerPtr LogManager::getLogger(const std::basic_string<UniChar>& name) |
154 | | { |
155 | | LOG4CXX_DECODE_UNICHAR(n, name); |
156 | | return getLoggerLS(n); |
157 | | } |
158 | | |
159 | | LoggerPtr LogManager::getLogger(const std::basic_string<UniChar>& name, |
160 | | const spi::LoggerFactoryPtr& factory) |
161 | | { |
162 | | LOG4CXX_DECODE_UNICHAR(n, name); |
163 | | return getLoggerLS(n, factory); |
164 | | } |
165 | | |
166 | | LoggerPtr LogManager::exists(const std::basic_string<UniChar>& name) |
167 | | { |
168 | | LOG4CXX_DECODE_UNICHAR(n, name); |
169 | | return existsLS(n); |
170 | | } |
171 | | #endif |
172 | | |
173 | | #if LOG4CXX_CFSTRING_API |
174 | | LoggerPtr LogManager::getLogger(const CFStringRef& name) |
175 | | { |
176 | | LOG4CXX_DECODE_CFSTRING(n, name); |
177 | | return getLoggerLS(n); |
178 | | } |
179 | | |
180 | | LoggerPtr LogManager::getLogger(const CFStringRef& name, |
181 | | const spi::LoggerFactoryPtr& factory) |
182 | | { |
183 | | LOG4CXX_DECODE_CFSTRING(n, name); |
184 | | return getLoggerLS(n, factory); |
185 | | } |
186 | | |
187 | | LoggerPtr LogManager::exists(const CFStringRef& name) |
188 | | { |
189 | | LOG4CXX_DECODE_CFSTRING(n, name); |
190 | | return existsLS(n); |
191 | | } |
192 | | #endif |
193 | | |
194 | | LoggerPtr LogManager::existsLS(const LogString& name) |
195 | 0 | { |
196 | 0 | return getLoggerRepository()->exists(name); |
197 | 0 | } |
198 | | |
199 | | LoggerList LogManager::getCurrentLoggers() |
200 | 0 | { |
201 | 0 | return getLoggerRepository()->getCurrentLoggers(); |
202 | 0 | } |
203 | | |
204 | | void LogManager::shutdown() |
205 | 0 | { |
206 | 0 | APRInitializer::unregisterAll(); |
207 | 0 | ThreadUtility::instance()->removeAllPeriodicTasks(); |
208 | 0 | getLoggerRepository()->shutdown(); |
209 | 0 | } |
210 | | |
211 | | void LogManager::resetConfiguration() |
212 | 0 | { |
213 | 0 | getLoggerRepository()->resetConfiguration(); |
214 | 0 | } |
215 | | |
216 | | bool LogManager::removeLogger(const LogString& name, bool ifNotUsed) |
217 | 0 | { |
218 | 0 | #if LOG4CXX_ABI_VERSION <= 15 |
219 | 0 | bool result = false; |
220 | 0 | if (auto r = dynamic_cast<Hierarchy*>(getLoggerRepository().get())) |
221 | 0 | result = r->removeLogger(name, ifNotUsed); |
222 | 0 | return result; |
223 | | #else |
224 | | return getLoggerRepository()->removeLogger(name, ifNotUsed); |
225 | | #endif |
226 | 0 | } |