StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StarOptionFilter.cxx
1 /*
2  * Copyright 2003,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <cstring>
18 #include "StarOptionFilter.h"
19 #include <log4cxx/spi/loggingevent.h>
20 #include <log4cxx/helpers/optionconverter.h>
21 
22 // #include <iostream>
23 
24 using namespace log4cxx;
25 using namespace log4cxx::varia;
26 using namespace log4cxx::spi;
27 using namespace log4cxx::helpers;
28 
29 IMPLEMENT_LOG4CXX_OBJECT(StarOptionFilter)
30 
31 
32  String StarOptionFilter::ACCEPT_REPEAT_COUNTER = _T("RepeatMessageQuota");
33  String StarOptionFilter::TOTAL_MESSAGE_LIMIT = _T("TotalMessagesQuota");
34  String StarOptionFilter::STRING_TO_COUNT_OPTION = _T("StringToCount");
35 
36 
37 //______________________________________________________________________________
38 StarOptionFilter::StarOptionFilter() : acceptRepeatCounter(-1),acceptTotalCounter(-1)
39  ,currentRepeatCounter(0),currentTotalCounter(0)
40  ,matchPredefinedStringOnly(false)
41 {
42 }
43 
44 //______________________________________________________________________________
45 void StarOptionFilter::setOption(const String& option,
46  const String& value)
47 {
48  // fprintf(stderr, " StarOptionFilter::setOption option = %s; value = %s\n"
49  // , option.c_str(), value.c_str());
50  if (equalsIgnoreCase(option, ACCEPT_REPEAT_COUNTER))
51  {
52  acceptRepeatCounter = OptionConverter::toInt(value,acceptRepeatCounter);
53  }
54  else if (equalsIgnoreCase(option,STRING_TO_COUNT_OPTION))
55  {
56  if ( lastLoggerMessageToCompare != value) {
57  currentRepeatCounter = 0;
58  lastLoggerMessageToCompare = value;
59  matchPredefinedStringOnly = true;
60  }
61  if (lastLoggerMessageToCompare.empty())
62  matchPredefinedStringOnly = false;
63  }
64  else if (equalsIgnoreCase(option,TOTAL_MESSAGE_LIMIT))
65  {
66  acceptTotalCounter = OptionConverter::toInt(value,acceptTotalCounter);
67  }
68 }
69 //______________________________________________________________________________
70 void StarOptionFilter::setRepeatCounterOption(int value)
71 {
72  // value = -1 there is no limit
73  // > 0 the number of times the any or preselected message can be
74  // printed out sequiencially
75  //
76  // Attn: the value zero and one have one and the same meaning
77  // 0 - the message can not be printed at all
78  // 1 - the message can be printed one times only
79 
80  acceptRepeatCounter = value;
81 }
82 
83 //______________________________________________________________________________
84 void StarOptionFilter::setTotalCounterOption(int value)
85 {
86  // value = -1 there is no limit
87  // > 0 the number of times the message can be printed out sequiencially
88  //
89  // Attn: the value zero and one have one and the same meaning
90  // 0 - the message can not be printed at all
91  // 1 - the message can be printed one times only
92 
93  acceptTotalCounter = value;
94 }
95 //______________________________________________________________________________
96 Filter::FilterDecision StarOptionFilter::decide(
97  const log4cxx::spi::LoggingEventPtr& event) const
98 {
99  Filter::FilterDecision decision = Filter::NEUTRAL;
100  const String& msg = event->getRenderedMessage();
101  //fprintf(stderr," StarOptionFilter::decide: %s, string quota=%d, totalQuota=%d match =%d\n"
102  // , msg.c_str(),currentRepeatCounter,currentTotalCounter, matchPredefinedStringOnly);
103  if( !msg.empty() ) {
104 #if 1
105  if ( (acceptRepeatCounter >= 0 ) || (acceptTotalCounter >= 0 ) )
106  {
107  bool count = !matchPredefinedStringOnly
108  ||
109  (matchPredefinedStringOnly && strstr(msg.c_str(),lastLoggerMessageToCompare.c_str())) ;
110  if (count) {
111  if (acceptRepeatCounter >= 0 ) currentRepeatCounter++;
112  if (acceptTotalCounter >= 0 ) currentTotalCounter++;
113  } else {
114  // reset the repeat counter
115  currentRepeatCounter = 0;
116  if (!matchPredefinedStringOnly && (acceptRepeatCounter >= 0 ) )
117  lastLoggerMessageToCompare = msg;
118  }
119  // we've got a match
120  if( count && (acceptRepeatCounter >=0 ) && (currentRepeatCounter > acceptRepeatCounter) )
121  decision = Filter::DENY;
122 
123  if( count && (acceptTotalCounter >=0) && (currentTotalCounter > acceptTotalCounter) )
124  decision = Filter::DENY;
125  }
126 #else
127  if (acceptRepeatCounter >= 0 ) {
128  if( strcmp(msg.c_str(),lastLoggerMessageToCompare.c_str() ) )
129  {
130  if (!matchPredefinedStringOnly) {
131  currentRepeatCounter = 2;
132  lastLoggerMessageToCompare = msg;
133  }
134  }
135  else
136  {
137  // we've got a match
138  if(currentRepeatCounter > acceptRepeatCounter) decision = Filter::DENY;
139  currentRepeatCounter++;
140  }
141  }
142 #endif
143  }
144  return decision;
145 }