/src/logging-log4cxx/src/main/cpp/appenderskeleton.cpp
Line | Count | Source |
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 | 1 | : m_priv(std::make_unique<AppenderSkeletonPrivate>()) |
41 | 1 | { |
42 | | |
43 | 1 | } |
44 | | |
45 | | AppenderSkeleton::AppenderSkeleton(const LayoutPtr& layout) |
46 | 0 | : m_priv(std::make_unique<AppenderSkeletonPrivate>(layout)) |
47 | 0 | { |
48 | |
|
49 | 0 | } |
50 | | |
51 | 1 | 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 | 155k | { |
88 | 155k | return ((level == 0) || level->isGreaterOrEqual(m_priv->threshold)); |
89 | 155k | } |
90 | | |
91 | | void AppenderSkeleton::doAppend(const spi::LoggingEventPtr& event, Pool& pool1) |
92 | 155k | { |
93 | 155k | std::lock_guard<std::recursive_mutex> lock(m_priv->mutex); |
94 | | |
95 | 155k | doAppendImpl(event, pool1); |
96 | 155k | } |
97 | | |
98 | | void AppenderSkeleton::doAppendImpl(const spi::LoggingEventPtr& event, Pool& pool) |
99 | 155k | { |
100 | 155k | if (m_priv->closed) |
101 | 0 | { |
102 | 0 | LogLog::warn(LOG4CXX_STR("Attempted to append to closed appender named [") |
103 | 0 | + m_priv->name + LOG4CXX_STR("].")); |
104 | 0 | } |
105 | 155k | else if (isAsSevereAsThreshold(event->getLevel()) && isAccepted(event)) |
106 | 155k | { |
107 | 155k | append(event, pool); |
108 | 155k | } |
109 | 155k | } |
110 | | |
111 | | bool AppenderSkeleton::isAccepted(const spi::LoggingEventPtr& event) const |
112 | 155k | { |
113 | 155k | FilterPtr f = m_priv->headFilter; |
114 | 155k | while (f != 0) |
115 | 0 | { |
116 | 0 | switch (f->decide(event)) |
117 | 0 | { |
118 | 0 | case Filter::DENY: |
119 | 0 | return false; |
120 | | |
121 | 0 | case Filter::ACCEPT: |
122 | 0 | f = nullptr; |
123 | 0 | break; |
124 | | |
125 | 0 | default: |
126 | 0 | case Filter::NEUTRAL: |
127 | 0 | f = f->getNext(); |
128 | 0 | } |
129 | 0 | } |
130 | 155k | return true; |
131 | 155k | } |
132 | | |
133 | | bool AppenderSkeleton::AppenderSkeletonPrivate::checkNotClosed() |
134 | 0 | { |
135 | 0 | if (this->closed) |
136 | 0 | { |
137 | 0 | if (!this->warnedClosed) |
138 | 0 | { |
139 | 0 | LogLog::warn(LOG4CXX_STR("Not allowed to write to a closed appender.")); |
140 | 0 | this->warnedClosed = true; |
141 | 0 | } |
142 | 0 | return false; |
143 | 0 | } |
144 | 0 | return true; |
145 | 0 | } |
146 | | |
147 | | bool AppenderSkeleton::AppenderSkeletonPrivate::checkLayout() |
148 | 0 | { |
149 | 0 | if (!this->layout) |
150 | 0 | { |
151 | 0 | if (!this->warnedNoLayout) |
152 | 0 | { |
153 | 0 | this->errorHandler->error |
154 | 0 | ( LogString(LOG4CXX_STR("No layout set for the appender named [")) |
155 | 0 | + this->name + LOG4CXX_STR("].") |
156 | 0 | ); |
157 | 0 | this->warnedNoLayout = true; |
158 | 0 | } |
159 | 0 | return false; |
160 | 0 | } |
161 | 0 | return true; |
162 | 0 | } |
163 | | |
164 | | void AppenderSkeleton::setErrorHandler(const spi::ErrorHandlerPtr errorHandler1) |
165 | 0 | { |
166 | 0 | std::lock_guard<std::recursive_mutex> lock(m_priv->mutex); |
167 | |
|
168 | 0 | if (errorHandler1 == nullptr) |
169 | 0 | { |
170 | | // We do not throw exception here since the cause is probably a |
171 | | // bad config file. |
172 | 0 | LogLog::warn(LOG4CXX_STR("You have tried to set a null error-handler.")); |
173 | 0 | } |
174 | 0 | else |
175 | 0 | { |
176 | 0 | m_priv->errorHandler = errorHandler1; |
177 | 0 | } |
178 | 0 | } |
179 | | |
180 | | void AppenderSkeleton::setThreshold(const LevelPtr& threshold1) |
181 | 0 | { |
182 | 0 | std::lock_guard<std::recursive_mutex> lock(m_priv->mutex); |
183 | 0 | m_priv->threshold = threshold1; |
184 | 0 | } |
185 | | |
186 | | void AppenderSkeleton::setOption(const LogString& option, |
187 | | const LogString& value) |
188 | 0 | { |
189 | 0 | if (StringHelper::equalsIgnoreCase(option, |
190 | 0 | LOG4CXX_STR("THRESHOLD"), LOG4CXX_STR("threshold"))) |
191 | 0 | { |
192 | 0 | setThreshold(Level::toLevelLS(value)); |
193 | 0 | } |
194 | 0 | else if (StringHelper::equalsIgnoreCase(option, |
195 | 0 | LOG4CXX_STR("NAME"), LOG4CXX_STR("name"))) |
196 | 0 | { |
197 | 0 | setName(value); |
198 | 0 | } |
199 | 0 | } |
200 | | |
201 | | const spi::ErrorHandlerPtr AppenderSkeleton::getErrorHandler() const |
202 | 0 | { |
203 | 0 | return m_priv->errorHandler; |
204 | 0 | } |
205 | | |
206 | | spi::FilterPtr AppenderSkeleton::getFilter() const |
207 | 0 | { |
208 | 0 | return m_priv->headFilter; |
209 | 0 | } |
210 | | |
211 | | const spi::FilterPtr AppenderSkeleton::getFirstFilter() const |
212 | 0 | { |
213 | 0 | return m_priv->headFilter; |
214 | 0 | } |
215 | | |
216 | | LayoutPtr AppenderSkeleton::getLayout() const |
217 | 155k | { |
218 | 155k | return m_priv->layout; |
219 | 155k | } |
220 | | |
221 | | LogString AppenderSkeleton::getName() const |
222 | 1 | { |
223 | 1 | return m_priv->name; |
224 | 1 | } |
225 | | |
226 | | const LevelPtr AppenderSkeleton::getThreshold() const |
227 | 0 | { |
228 | 0 | return m_priv->threshold; |
229 | 0 | } |
230 | | |
231 | | void AppenderSkeleton::setLayout(const LayoutPtr layout1) |
232 | 1 | { |
233 | 1 | m_priv->layout = layout1; |
234 | 1 | } |
235 | | |
236 | | void AppenderSkeleton::setName(const LogString& name1) |
237 | 1 | { |
238 | 1 | m_priv->name.assign(name1); |
239 | 1 | } |