/src/logging-log4cxx/src/main/cpp/appenderskeleton.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/spi/loggingevent.h> |
19 | | #include <log4cxx/appenderskeleton.h> |
20 | | #include <log4cxx/helpers/loglog.h> |
21 | | #include <log4cxx/helpers/onlyonceerrorhandler.h> |
22 | | #include <log4cxx/level.h> |
23 | | #include <log4cxx/helpers/stringhelper.h> |
24 | | #include <log4cxx/private/appenderskeleton_priv.h> |
25 | | #include <mutex> |
26 | | |
27 | | using namespace LOG4CXX_NS; |
28 | | using namespace LOG4CXX_NS::spi; |
29 | | using namespace LOG4CXX_NS::helpers; |
30 | | |
31 | | IMPLEMENT_LOG4CXX_OBJECT(AppenderSkeleton) |
32 | | |
33 | | AppenderSkeleton::AppenderSkeleton( std::unique_ptr<AppenderSkeletonPrivate> priv ) |
34 | 0 | : m_priv(std::move(priv)) |
35 | 0 | { |
36 | |
|
37 | 0 | } |
38 | | |
39 | | AppenderSkeleton::AppenderSkeleton() |
40 | 0 | : m_priv(std::make_unique<AppenderSkeletonPrivate>()) |
41 | 0 | { |
42 | |
|
43 | 0 | } |
44 | | |
45 | | AppenderSkeleton::AppenderSkeleton(const LayoutPtr& layout) |
46 | 0 | : m_priv(std::make_unique<AppenderSkeletonPrivate>(layout)) |
47 | 0 | { |
48 | |
|
49 | 0 | } |
50 | | |
51 | 0 | AppenderSkeleton::~AppenderSkeleton() {} |
52 | | |
53 | | void AppenderSkeleton::finalize() |
54 | 0 | { |
55 | | // An appender might be closed then garbage collected. There is no |
56 | | // point in closing twice. |
57 | 0 | if (m_priv->closed) |
58 | 0 | { |
59 | 0 | return; |
60 | 0 | } |
61 | | |
62 | 0 | close(); |
63 | 0 | } |
64 | | |
65 | | void AppenderSkeleton::addFilter(const spi::FilterPtr newFilter) |
66 | 0 | { |
67 | 0 | std::lock_guard<std::recursive_mutex> lock(m_priv->mutex); |
68 | |
|
69 | 0 | if (m_priv->headFilter == nullptr) |
70 | 0 | { |
71 | 0 | m_priv->headFilter = m_priv->tailFilter = newFilter; |
72 | 0 | } |
73 | 0 | else |
74 | 0 | { |
75 | 0 | m_priv->tailFilter->setNext(newFilter); |
76 | 0 | m_priv->tailFilter = newFilter; |
77 | 0 | } |
78 | 0 | } |
79 | | |
80 | | void AppenderSkeleton::clearFilters() |
81 | 0 | { |
82 | 0 | std::lock_guard<std::recursive_mutex> lock(m_priv->mutex); |
83 | 0 | m_priv->headFilter = m_priv->tailFilter = nullptr; |
84 | 0 | } |
85 | | |
86 | | bool AppenderSkeleton::isAsSevereAsThreshold(const LevelPtr& level) const |
87 | 0 | { |
88 | 0 | return ((level == 0) || level->isGreaterOrEqual(m_priv->threshold)); |
89 | 0 | } |
90 | | |
91 | | void AppenderSkeleton::doAppend(const spi::LoggingEventPtr& event, Pool& pool1) |
92 | 0 | { |
93 | 0 | std::lock_guard<std::recursive_mutex> lock(m_priv->mutex); |
94 | |
|
95 | 0 | doAppendImpl(event, pool1); |
96 | 0 | } |
97 | | |
98 | | void AppenderSkeleton::doAppendImpl(const spi::LoggingEventPtr& event, Pool& pool1) |
99 | 0 | { |
100 | 0 | if (m_priv->closed) |
101 | 0 | { |
102 | 0 | LogLog::error(((LogString) LOG4CXX_STR("Attempted to append to closed appender named [")) |
103 | 0 | + m_priv->name + LOG4CXX_STR("].")); |
104 | 0 | return; |
105 | 0 | } |
106 | | |
107 | 0 | if (!isAsSevereAsThreshold(event->getLevel())) |
108 | 0 | { |
109 | 0 | return; |
110 | 0 | } |
111 | | |
112 | 0 | FilterPtr f = m_priv->headFilter; |
113 | | |
114 | |
|
115 | 0 | while (f != 0) |
116 | 0 | { |
117 | 0 | switch (f->decide(event)) |
118 | 0 | { |
119 | 0 | case Filter::DENY: |
120 | 0 | return; |
121 | | |
122 | 0 | case Filter::ACCEPT: |
123 | 0 | f = nullptr; |
124 | 0 | break; |
125 | | |
126 | 0 | case Filter::NEUTRAL: |
127 | 0 | f = f->getNext(); |
128 | 0 | } |
129 | 0 | } |
130 | | |
131 | 0 | append(event, pool1); |
132 | 0 | } |
133 | | |
134 | | bool AppenderSkeleton::AppenderSkeletonPrivate::checkNotClosed() |
135 | 0 | { |
136 | 0 | if (this->closed) |
137 | 0 | { |
138 | 0 | if (!this->warnedClosed) |
139 | 0 | { |
140 | 0 | LogLog::warn(LOG4CXX_STR("Not allowed to write to a closed appender.")); |
141 | 0 | this->warnedClosed = true; |
142 | 0 | } |
143 | 0 | return false; |
144 | 0 | } |
145 | 0 | return true; |
146 | 0 | } |
147 | | |
148 | | bool AppenderSkeleton::AppenderSkeletonPrivate::checkLayout() |
149 | 0 | { |
150 | 0 | if (!this->layout) |
151 | 0 | { |
152 | 0 | if (!this->warnedNoLayout) |
153 | 0 | { |
154 | 0 | this->errorHandler->error |
155 | 0 | ( LogString(LOG4CXX_STR("No layout set for the appender named [")) |
156 | 0 | + this->name + LOG4CXX_STR("].") |
157 | 0 | ); |
158 | 0 | this->warnedNoLayout = true; |
159 | 0 | } |
160 | 0 | return false; |
161 | 0 | } |
162 | 0 | return true; |
163 | 0 | } |
164 | | |
165 | | void AppenderSkeleton::setErrorHandler(const spi::ErrorHandlerPtr errorHandler1) |
166 | 0 | { |
167 | 0 | std::lock_guard<std::recursive_mutex> lock(m_priv->mutex); |
168 | |
|
169 | 0 | if (errorHandler1 == nullptr) |
170 | 0 | { |
171 | | // We do not throw exception here since the cause is probably a |
172 | | // bad config file. |
173 | 0 | LogLog::warn(LOG4CXX_STR("You have tried to set a null error-handler.")); |
174 | 0 | } |
175 | 0 | else |
176 | 0 | { |
177 | 0 | m_priv->errorHandler = errorHandler1; |
178 | 0 | } |
179 | 0 | } |
180 | | |
181 | | void AppenderSkeleton::setThreshold(const LevelPtr& threshold1) |
182 | 0 | { |
183 | 0 | std::lock_guard<std::recursive_mutex> lock(m_priv->mutex); |
184 | 0 | m_priv->threshold = threshold1; |
185 | 0 | } |
186 | | |
187 | | void AppenderSkeleton::setOption(const LogString& option, |
188 | | const LogString& value) |
189 | 0 | { |
190 | 0 | if (StringHelper::equalsIgnoreCase(option, |
191 | 0 | LOG4CXX_STR("THRESHOLD"), LOG4CXX_STR("threshold"))) |
192 | 0 | { |
193 | 0 | setThreshold(Level::toLevelLS(value)); |
194 | 0 | } |
195 | 0 | } |
196 | | |
197 | | const spi::ErrorHandlerPtr AppenderSkeleton::getErrorHandler() const |
198 | 0 | { |
199 | 0 | return m_priv->errorHandler; |
200 | 0 | } |
201 | | |
202 | | spi::FilterPtr AppenderSkeleton::getFilter() const |
203 | 0 | { |
204 | 0 | return m_priv->headFilter; |
205 | 0 | } |
206 | | |
207 | | const spi::FilterPtr AppenderSkeleton::getFirstFilter() const |
208 | 0 | { |
209 | 0 | return m_priv->headFilter; |
210 | 0 | } |
211 | | |
212 | | LayoutPtr AppenderSkeleton::getLayout() const |
213 | 0 | { |
214 | 0 | return m_priv->layout; |
215 | 0 | } |
216 | | |
217 | | LogString AppenderSkeleton::getName() const |
218 | 0 | { |
219 | 0 | return m_priv->name; |
220 | 0 | } |
221 | | |
222 | | const LevelPtr AppenderSkeleton::getThreshold() const |
223 | 0 | { |
224 | 0 | return m_priv->threshold; |
225 | 0 | } |
226 | | |
227 | | void AppenderSkeleton::setLayout(const LayoutPtr layout1) |
228 | 0 | { |
229 | 0 | m_priv->layout = layout1; |
230 | 0 | } |
231 | | |
232 | | void AppenderSkeleton::setName(const LogString& name1) |
233 | 0 | { |
234 | 0 | m_priv->name.assign(name1); |
235 | 0 | } |