Apache Log4cxx  Version 1.3.1
loggerinstance.h
Go to the documentation of this file.
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 #ifndef LOG4CXX_LOGGER_INSTANCE_HDR_
19 #define LOG4CXX_LOGGER_INSTANCE_HDR_
20 
21 #include <log4cxx/logmanager.h>
22 #include <log4cxx/logger.h>
23 
24 namespace LOG4CXX_NS
25 {
26 
47 {
48  bool m_hadConfiguration;
49  LoggerPtr m_logger;
50 public: // ...structors
52  LoggerInstancePtr() : m_hadConfiguration(false)
53  {}
55  template <class StringType>
56  LoggerInstancePtr(const StringType& instanceName)
57  : m_hadConfiguration(LogManager::exists(instanceName))
58  , m_logger(LogManager::getLogger(instanceName))
59  {
60  }
63  {
64  reset();
65  }
66 
67  const LoggerPtr& operator->() const noexcept
68  {
69  return m_logger;
70  }
71 
72  explicit operator bool() const noexcept
73  {
74  return !!m_logger;
75  }
76 
77  operator LoggerPtr&() noexcept
78  {
79  return m_logger;
80  }
81 
82  operator const LoggerPtr&() const noexcept
83  {
84  return m_logger;
85  }
86 
87  Logger* get() noexcept
88  {
89  return m_logger.get();
90  }
91 
92  const Logger* get() const noexcept
93  {
94  return m_logger.get();
95  }
96 
98  void reset()
99  {
100  if (m_logger && !m_hadConfiguration)
101  {
102  auto name = m_logger->getName();
103  m_logger.reset(); // Decrease reference count
104  LogManager::removeLogger(name);
105  }
106  else
107  {
108  m_hadConfiguration = false;
109  m_logger.reset();
110  }
111  }
112 
114  template <class StringType>
115  void reset(const StringType& instanceName)
116  {
117  reset();
118  m_hadConfiguration = !!LogManager::exists(instanceName);
119  m_logger = LogManager::getLogger(instanceName);
120  }
121 };
122 
123 } // namespace LOG4CXX_NS
124 
125 #endif // LOG4CXX_LOGGER_INSTANCE_HDR_
Use the LogManager class to retreive Logger instances or to operate on the current LoggerRepository.
Definition: logmanager.h:45
A smart pointer (implicity convertable to LoggerPtr) that conditionally removes a Logger from the spi...
Definition: loggerinstance.h:47
const LoggerPtr & operator->() const noexcept
Definition: loggerinstance.h:67
LoggerInstancePtr(const StringType &instanceName)
A separately configurable logger named instanceName.
Definition: loggerinstance.h:56
Logger * get() noexcept
Definition: loggerinstance.h:87
void reset(const StringType &instanceName)
Change this to a logger named instanceName.
Definition: loggerinstance.h:115
LoggerInstancePtr()
A null LoggerPtr.
Definition: loggerinstance.h:52
~LoggerInstancePtr()
Conditionally remove the logger from the the spi::LoggerRepository.
Definition: loggerinstance.h:62
const Logger * get() const noexcept
Definition: loggerinstance.h:92
void reset()
Conditionally remove the Logger from the spi::LoggerRepository.
Definition: loggerinstance.h:98
This is the central class in the log4cxx package.
Definition: log4cxx/logger.h:51
std::shared_ptr< Logger > LoggerPtr
Definition: defaultloggerfactory.h:26