/src/log4cplus/src/appenderattachableimpl.cxx
Line | Count | Source |
1 | | // Module: Log4CPLUS |
2 | | // File: appenderattachableimpl.cxx |
3 | | // Created: 6/2001 |
4 | | // Author: Tad E. Smith |
5 | | // |
6 | | // |
7 | | // Copyright 2001-2017 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 | | |
22 | | #include <log4cplus/appender.h> |
23 | | #include <log4cplus/helpers/appenderattachableimpl.h> |
24 | | #include <log4cplus/helpers/loglog.h> |
25 | | #include <log4cplus/spi/loggingevent.h> |
26 | | #include <log4cplus/thread/syncprims-pub-impl.h> |
27 | | |
28 | | #include <algorithm> |
29 | | |
30 | | |
31 | | namespace log4cplus |
32 | | { |
33 | | |
34 | | |
35 | | namespace spi |
36 | | { |
37 | | |
38 | | |
39 | | AppenderAttachable::~AppenderAttachable() |
40 | 7.77M | { } |
41 | | |
42 | | |
43 | | } // namespace spi |
44 | | |
45 | | |
46 | | namespace helpers |
47 | | { |
48 | | |
49 | | |
50 | | ////////////////////////////////////////////////////////////////////////////// |
51 | | // log4cplus::helpers::AppenderAttachableImpl ctor and dtor |
52 | | ////////////////////////////////////////////////////////////////////////////// |
53 | | |
54 | | AppenderAttachableImpl::AppenderAttachableImpl() |
55 | 249 | { } |
56 | | |
57 | | |
58 | | AppenderAttachableImpl::~AppenderAttachableImpl() |
59 | 0 | { } |
60 | | |
61 | | |
62 | | |
63 | | /////////////////////////////////////////////////////////////////////////////// |
64 | | // log4cplus::helpers::AppenderAttachableImpl public methods |
65 | | /////////////////////////////////////////////////////////////////////////////// |
66 | | |
67 | | void |
68 | | AppenderAttachableImpl::addAppender(SharedAppenderPtr newAppender) |
69 | 528k | { |
70 | 528k | if (! newAppender) |
71 | 0 | { |
72 | 0 | getLogLog().warn( LOG4CPLUS_TEXT("Tried to add NULL appender") ); |
73 | 0 | return; |
74 | 0 | } |
75 | | |
76 | 528k | thread::MutexGuard guard (appender_list_mutex); |
77 | | |
78 | 528k | auto it = std::find(appenderList.begin(), appenderList.end(), newAppender); |
79 | 528k | if (it == appenderList.end()) |
80 | 528k | { |
81 | 528k | appenderList.push_back(newAppender); |
82 | 528k | } |
83 | 528k | } |
84 | | |
85 | | |
86 | | |
87 | | AppenderAttachableImpl::ListType |
88 | | AppenderAttachableImpl::getAllAppenders() |
89 | 6.01M | { |
90 | 6.01M | thread::MutexGuard guard (appender_list_mutex); |
91 | | |
92 | 6.01M | return appenderList; |
93 | 6.01M | } |
94 | | |
95 | | |
96 | | |
97 | | SharedAppenderPtr |
98 | | AppenderAttachableImpl::getAppender(const log4cplus::tstring& name) |
99 | 824k | { |
100 | 824k | thread::MutexGuard guard (appender_list_mutex); |
101 | | |
102 | 824k | for (SharedAppenderPtr & ptr : appenderList) |
103 | 131k | { |
104 | 131k | if (ptr->getName() == name) |
105 | 0 | return ptr; |
106 | 131k | } |
107 | | |
108 | 824k | return SharedAppenderPtr (); |
109 | 824k | } |
110 | | |
111 | | |
112 | | |
113 | | void |
114 | | AppenderAttachableImpl::removeAllAppenders() |
115 | 3.25M | { |
116 | 3.25M | thread::MutexGuard guard (appender_list_mutex); |
117 | | |
118 | | // Clear appenders in specific order because the order of destruction of |
119 | | // std::vector elements is surprisingly unspecified and it breaks our |
120 | | // tests' expectations. |
121 | | |
122 | 3.25M | for (auto & app : appenderList) |
123 | 528k | app = SharedAppenderPtr (); |
124 | | |
125 | 3.25M | appenderList.clear (); |
126 | 3.25M | } |
127 | | |
128 | | |
129 | | |
130 | | void |
131 | | AppenderAttachableImpl::removeAppender(SharedAppenderPtr appender) |
132 | 0 | { |
133 | 0 | if (! appender) |
134 | 0 | { |
135 | 0 | getLogLog().warn( LOG4CPLUS_TEXT("Tried to remove NULL appender") ); |
136 | 0 | return; |
137 | 0 | } |
138 | | |
139 | 0 | thread::MutexGuard guard (appender_list_mutex); |
140 | |
|
141 | 0 | auto it = std::find(appenderList.begin(), appenderList.end(), appender); |
142 | 0 | if (it != appenderList.end()) |
143 | 0 | { |
144 | 0 | appenderList.erase(it); |
145 | 0 | } |
146 | 0 | } |
147 | | |
148 | | |
149 | | |
150 | | void |
151 | | AppenderAttachableImpl::removeAppender(const log4cplus::tstring& name) |
152 | 0 | { |
153 | 0 | removeAppender(getAppender(name)); |
154 | 0 | } |
155 | | |
156 | | |
157 | | |
158 | | int |
159 | | AppenderAttachableImpl::appendLoopOnAppenders(const spi::InternalLoggingEvent& event) const |
160 | 2.62M | { |
161 | 2.62M | int count = 0; |
162 | | |
163 | 2.62M | thread::MutexGuard guard (appender_list_mutex); |
164 | | |
165 | 2.62M | for (auto & appender : appenderList) |
166 | 1.09M | { |
167 | 1.09M | ++count; |
168 | 1.09M | appender->doAppend(event); |
169 | 1.09M | } |
170 | | |
171 | 2.62M | return count; |
172 | 2.62M | } |
173 | | |
174 | | |
175 | | } // namespace helpers |
176 | | |
177 | | |
178 | | } // namespace log4cplus |