1 // Module:  Log4CPLUS
2 // File:    logger.cxx
3 // Created: 6/2001
4 // Author:  Tad E. Smith
5 //
6 //
7 // Copyright 2001-2013 Tad E. Smith
8 //
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 //
13 //     http://www.apache.org/licenses/LICENSE-2.0
14 //
15 // Unless required by applicable law or agreed to in writing, software
16 // distributed under the License is distributed on an "AS IS" BASIS,
17 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 // See the License for the specific language governing permissions and
19 // limitations under the License.
20 
21 #include <log4cplus/logger.h>
22 #include <log4cplus/appender.h>
23 #include <log4cplus/hierarchy.h>
24 #include <log4cplus/helpers/loglog.h>
25 #include <log4cplus/spi/loggerimpl.h>
26 #include <utility>
27 
28 
29 namespace log4cplus
30 {
31 
32 
33 Logger
makeNewLoggerInstance(const log4cplus::tstring & name,Hierarchy & h)34 DefaultLoggerFactory::makeNewLoggerInstance (const log4cplus::tstring & name,
35     Hierarchy& h)
36 {
37     return Logger (new spi::LoggerImpl (name, h));
38 }
39 
40 
41 //////////////////////////////////////////////////////////////////////////////
42 // static Logger Methods
43 //////////////////////////////////////////////////////////////////////////////
44 //
45 Hierarchy &
getDefaultHierarchy()46 Logger::getDefaultHierarchy ()
47 {
48     return log4cplus::getDefaultHierarchy ();
49 }
50 
51 
52 bool
exists(const log4cplus::tstring & name)53 Logger::exists (const log4cplus::tstring & name)
54 {
55     return getDefaultHierarchy().exists(name);
56 }
57 
58 
59 LoggerList
getCurrentLoggers()60 Logger::getCurrentLoggers ()
61 {
62     return getDefaultHierarchy ().getCurrentLoggers ();
63 }
64 
65 
66 Logger
getInstance(const log4cplus::tstring & name)67 Logger::getInstance (const log4cplus::tstring& name)
68 {
69     return getDefaultHierarchy().getInstance(name);
70 }
71 
72 
73 Logger
getInstance(const log4cplus::tstring & name,spi::LoggerFactory & factory)74 Logger::getInstance (const log4cplus::tstring& name,
75     spi::LoggerFactory& factory)
76 {
77     return getDefaultHierarchy().getInstance(name, factory);
78 }
79 
80 
81 Logger
getRoot()82 Logger::getRoot ()
83 {
84     return getDefaultHierarchy ().getRoot ();
85 }
86 
87 
88 void
shutdown()89 Logger::shutdown ()
90 {
91     getDefaultHierarchy ().shutdown ();
92 }
93 
94 
95 
96 //////////////////////////////////////////////////////////////////////////////
97 // Logger ctors and dtor
98 //////////////////////////////////////////////////////////////////////////////
99 
Logger()100 Logger::Logger ()
101     : value (0)
102 { }
103 
104 
Logger(spi::LoggerImpl * ptr)105 Logger::Logger (spi::LoggerImpl * ptr)
106     : value (ptr)
107 {
108     if (value)
109         value->addReference ();
110 }
111 
112 
Logger(const Logger & rhs)113 Logger::Logger (const Logger& rhs)
114     : spi::AppenderAttachable (rhs)
115     , value (rhs.value)
116 {
117     if (value)
118         value->addReference ();
119 }
120 
121 
122 Logger &
operator =(const Logger & rhs)123 Logger::operator = (const Logger& rhs)
124 {
125     Logger (rhs).swap (*this);
126     return *this;
127 }
128 
129 
130 #if defined (LOG4CPLUS_HAVE_RVALUE_REFS)
Logger(Logger && rhs)131 Logger::Logger (Logger && rhs)
132     : spi::AppenderAttachable (std::move (rhs))
133     , value (std::move (rhs.value))
134 {
135     rhs.value = 0;
136 }
137 
138 
139 Logger &
operator =(Logger && rhs)140 Logger::operator = (Logger && rhs)
141 {
142     Logger (std::move (rhs)).swap (*this);
143     return *this;
144 }
145 
146 #endif
147 
148 
~Logger()149 Logger::~Logger ()
150 {
151     if (value)
152         value->removeReference ();
153 }
154 
155 
156 //////////////////////////////////////////////////////////////////////////////
157 // Logger Methods
158 //////////////////////////////////////////////////////////////////////////////
159 
160 void
swap(Logger & other)161 Logger::swap (Logger & other)
162 {
163     std::swap (value, other.value);
164 }
165 
166 
167 Logger
getParent() const168 Logger::getParent () const
169 {
170     if (value->parent)
171         return Logger (value->parent.get ());
172     else
173     {
174         helpers::getLogLog().error(
175             LOG4CPLUS_TEXT("********* This logger has no parent: "
176             + getName()));
177         return *this;
178     }
179 }
180 
181 
182 void
addAppender(SharedAppenderPtr newAppender)183 Logger::addAppender (SharedAppenderPtr newAppender)
184 {
185     value->addAppender(newAppender);
186 }
187 
188 
189 SharedAppenderPtrList
getAllAppenders()190 Logger::getAllAppenders ()
191 {
192     return value->getAllAppenders();
193 }
194 
195 
196 SharedAppenderPtr
getAppender(const log4cplus::tstring & name)197 Logger::getAppender (const log4cplus::tstring& name)
198 {
199     return value->getAppender (name);
200 }
201 
202 
203 void
removeAllAppenders()204 Logger::removeAllAppenders ()
205 {
206     value->removeAllAppenders ();
207 }
208 
209 
210 void
removeAppender(SharedAppenderPtr appender)211 Logger::removeAppender (SharedAppenderPtr appender)
212 {
213     value->removeAppender(appender);
214 }
215 
216 
217 void
removeAppender(const log4cplus::tstring & name)218 Logger::removeAppender (const log4cplus::tstring& name)
219 {
220     value->removeAppender (name);
221 }
222 
223 
224 void
assertion(bool assertionVal,const log4cplus::tstring & msg) const225 Logger::assertion (bool assertionVal, const log4cplus::tstring& msg) const
226 {
227     if (! assertionVal)
228         log (FATAL_LOG_LEVEL, msg);
229 }
230 
231 
232 void
closeNestedAppenders() const233 Logger::closeNestedAppenders () const
234 {
235     value->closeNestedAppenders ();
236 }
237 
238 
239 bool
isEnabledFor(LogLevel ll) const240 Logger::isEnabledFor (LogLevel ll) const
241 {
242     return value->isEnabledFor (ll);
243 }
244 
245 
246 void
log(LogLevel ll,const log4cplus::tstring & message,const char * file,int line) const247 Logger::log (LogLevel ll, const log4cplus::tstring& message, const char* file,
248     int line) const
249 {
250     value->log (ll, message, file, line);
251 }
252 
253 
254 void
log(spi::InternalLoggingEvent const & ev) const255 Logger::log (spi::InternalLoggingEvent const & ev) const
256 {
257     value->log (ev);
258 }
259 
260 
261 void
forcedLog(LogLevel ll,const log4cplus::tstring & message,const char * file,int line) const262 Logger::forcedLog (LogLevel ll, const log4cplus::tstring& message,
263     const char* file, int line) const
264 {
265     value->forcedLog (ll, message, file, line);
266 }
267 
268 
269 void
forcedLog(spi::InternalLoggingEvent const & ev) const270 Logger::forcedLog (spi::InternalLoggingEvent const & ev) const
271 {
272     value->forcedLog (ev);
273 }
274 
275 
276 void
callAppenders(const spi::InternalLoggingEvent & event) const277 Logger::callAppenders (const spi::InternalLoggingEvent& event) const
278 {
279     value->callAppenders (event);
280 }
281 
282 
283 LogLevel
getChainedLogLevel() const284 Logger::getChainedLogLevel () const
285 {
286     return value->getChainedLogLevel ();
287 }
288 
289 
290 LogLevel
getLogLevel() const291 Logger::getLogLevel() const
292 {
293     return value->getLogLevel ();
294 }
295 
296 
297 void
setLogLevel(LogLevel ll)298 Logger::setLogLevel (LogLevel ll)
299 {
300     value->setLogLevel (ll);
301 }
302 
303 
304 Hierarchy &
getHierarchy() const305 Logger::getHierarchy () const
306 {
307     return value->getHierarchy ();
308 }
309 
310 
311 log4cplus::tstring const &
getName() const312 Logger::getName () const
313 {
314     return value->getName ();
315 }
316 
317 
318 bool
getAdditivity() const319 Logger::getAdditivity () const
320 {
321     return value->getAdditivity ();
322 }
323 
324 
325 void
setAdditivity(bool additive)326 Logger::setAdditivity (bool additive)
327 {
328     value->setAdditivity (additive);
329 }
330 
331 
332 } // namespace log4cplus
333