1 // Module:  Log4CPLUS
2 // File:    filter.cxx
3 // Created: 5/2003
4 // Author:  Tad E. Smith
5 //
6 //
7 // Copyright 2003-2010 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 "dcmtk/oflog/spi/filter.h"
22 #include "dcmtk/oflog/helpers/loglog.h"
23 #include "dcmtk/oflog/helpers/strhelp.h"
24 #include "dcmtk/oflog/helpers/property.h"
25 #include "dcmtk/oflog/spi/logevent.h"
26 #include "dcmtk/oflog/thread/syncpub.h"
27 
28 
29 namespace dcmtk {
30 namespace log4cplus { namespace spi {
31 
32 ///////////////////////////////////////////////////////////////////////////////
33 // global methods
34 ///////////////////////////////////////////////////////////////////////////////
35 
36 FilterResult
checkFilter(const Filter * filter,const InternalLoggingEvent & event)37 checkFilter(const Filter* filter, const InternalLoggingEvent& event)
38 {
39     const Filter* currentFilter = filter;
40     while(currentFilter) {
41         FilterResult result = currentFilter->decide(event);
42         if(result != NEUTRAL) {
43             return result;
44         }
45 
46         currentFilter = currentFilter->next.get();
47     }
48 
49     return ACCEPT;
50 }
51 
52 
53 
54 ///////////////////////////////////////////////////////////////////////////////
55 // Filter implementation
56 ///////////////////////////////////////////////////////////////////////////////
57 
Filter()58 Filter::Filter() : next()
59 {
60 }
61 
62 
~Filter()63 Filter::~Filter()
64 {
65 }
66 
67 
68 void
appendFilter(FilterPtr filter)69 Filter::appendFilter(FilterPtr filter)
70 {
71     if (! next)
72         next = filter;
73     else
74         next->appendFilter(filter);
75 }
76 
77 
78 
79 ///////////////////////////////////////////////////////////////////////////////
80 // DenyAllFilter implementation
81 ///////////////////////////////////////////////////////////////////////////////
82 
DenyAllFilter()83 DenyAllFilter::DenyAllFilter ()
84 { }
85 
86 
DenyAllFilter(const helpers::Properties &)87 DenyAllFilter::DenyAllFilter (const helpers::Properties&)
88 { }
89 
90 
91 FilterResult
decide(const InternalLoggingEvent &) const92 DenyAllFilter::decide(const InternalLoggingEvent&) const
93 {
94     return DENY;
95 }
96 
97 
98 
99 ///////////////////////////////////////////////////////////////////////////////
100 // LogLevelMatchFilter implementation
101 ///////////////////////////////////////////////////////////////////////////////
102 
LogLevelMatchFilter()103 LogLevelMatchFilter::LogLevelMatchFilter()
104     : acceptOnMatch(), logLevelToMatch()
105 {
106     init();
107 }
108 
109 
110 
LogLevelMatchFilter(const helpers::Properties & properties)111 LogLevelMatchFilter::LogLevelMatchFilter(const helpers::Properties& properties)
112     : acceptOnMatch(), logLevelToMatch()
113 {
114     init();
115 
116     properties.getBool (acceptOnMatch = false,
117         DCMTK_LOG4CPLUS_TEXT("AcceptOnMatch"));
118 
119     tstring const & log_level_to_match
120         = properties.getProperty( DCMTK_LOG4CPLUS_TEXT("LogLevelToMatch") );
121     logLevelToMatch = getLogLevelManager().fromString(log_level_to_match);
122 }
123 
124 
125 void
init()126 LogLevelMatchFilter::init()
127 {
128     acceptOnMatch = true;
129     logLevelToMatch = NOT_SET_LOG_LEVEL;
130 }
131 
132 
133 FilterResult
decide(const InternalLoggingEvent & event) const134 LogLevelMatchFilter::decide(const InternalLoggingEvent& event) const
135 {
136     if(logLevelToMatch == NOT_SET_LOG_LEVEL) {
137         return NEUTRAL;
138     }
139 
140     bool matchOccurred = (logLevelToMatch == event.getLogLevel());
141 
142     if(matchOccurred) {
143         return (acceptOnMatch ? ACCEPT : DENY);
144     }
145     else {
146         return NEUTRAL;
147     }
148 }
149 
150 
151 
152 ///////////////////////////////////////////////////////////////////////////////
153 // LogLevelRangeFilter implementation
154 ///////////////////////////////////////////////////////////////////////////////
155 
LogLevelRangeFilter()156 LogLevelRangeFilter::LogLevelRangeFilter()
157     : acceptOnMatch(), logLevelMin(), logLevelMax()
158 {
159     init();
160 }
161 
162 
163 
LogLevelRangeFilter(const helpers::Properties & properties)164 LogLevelRangeFilter::LogLevelRangeFilter(const helpers::Properties& properties)
165     : acceptOnMatch(), logLevelMin(), logLevelMax()
166 {
167     init();
168 
169     properties.getBool (acceptOnMatch = false,
170         DCMTK_LOG4CPLUS_TEXT("AcceptOnMatch"));
171 
172     tstring const & log_level_min
173         = properties.getProperty( DCMTK_LOG4CPLUS_TEXT("LogLevelMin") );
174     logLevelMin = getLogLevelManager().fromString(log_level_min);
175 
176     tstring const & log_level_max
177         = properties.getProperty( DCMTK_LOG4CPLUS_TEXT("LogLevelMax") );
178     logLevelMax = getLogLevelManager().fromString(log_level_max);
179 }
180 
181 
182 void
init()183 LogLevelRangeFilter::init()
184 {
185     acceptOnMatch = true;
186     logLevelMin = NOT_SET_LOG_LEVEL;
187     logLevelMax = NOT_SET_LOG_LEVEL;
188 }
189 
190 
191 FilterResult
decide(const InternalLoggingEvent & event) const192 LogLevelRangeFilter::decide(const InternalLoggingEvent& event) const
193 {
194     if((logLevelMin != NOT_SET_LOG_LEVEL) && (event.getLogLevel() < logLevelMin)) {
195         // priority of event is less than minimum
196         return DENY;
197     }
198 
199     if((logLevelMax != NOT_SET_LOG_LEVEL) && (event.getLogLevel() > logLevelMax)) {
200         // priority of event is greater than maximum
201         return DENY;
202     }
203 
204     if(acceptOnMatch) {
205         // this filter set up to bypass later filters and always return
206         // accept if priority in range
207         return ACCEPT;
208     }
209     else {
210         // event is ok for this filter; allow later filters to have a look...
211         return NEUTRAL;
212     }
213 }
214 
215 
216 
217 ///////////////////////////////////////////////////////////////////////////////
218 // StringMatchFilter implementation
219 ///////////////////////////////////////////////////////////////////////////////
220 
StringMatchFilter()221 StringMatchFilter::StringMatchFilter()
222     : acceptOnMatch(), stringToMatch()
223 {
224     init();
225 }
226 
227 
228 
StringMatchFilter(const helpers::Properties & properties)229 StringMatchFilter::StringMatchFilter(const helpers::Properties& properties)
230     : acceptOnMatch(), stringToMatch()
231 {
232     init();
233 
234     properties.getBool (acceptOnMatch = false,
235         DCMTK_LOG4CPLUS_TEXT("AcceptOnMatch"));
236     stringToMatch = properties.getProperty( DCMTK_LOG4CPLUS_TEXT("StringToMatch") );
237 }
238 
239 
240 void
init()241 StringMatchFilter::init()
242 {
243     acceptOnMatch = true;
244 }
245 
246 
247 FilterResult
decide(const InternalLoggingEvent & event) const248 StringMatchFilter::decide(const InternalLoggingEvent& event) const
249 {
250     const tstring& message = event.getMessage();
251 
252     if(stringToMatch.empty () || message.empty ()) {
253         return NEUTRAL;
254     }
255 
256     if(message.find(stringToMatch) == OFString_npos) {
257         return NEUTRAL;
258     }
259     else {  // we've got a match
260         return (acceptOnMatch ? ACCEPT : DENY);
261     }
262 }
263 
264 
265 } } // namespace log4cplus { namespace spi {
266 } // end namespace dcmtk
267