artdaq_mfextensions  v1_05_00
ma_condition.cpp
1 #include "ErrorHandler/MessageAnalyzer/ma_condition.h"
2 #include "ErrorHandler/MessageAnalyzer/ma_parse.h"
3 
4 using namespace novadaq::errorhandler;
5 
6 ma_condition::ma_condition( std::string const & desc
7  , std::string const & sev
8  , std::vector<std::string> const & sources
9  , std::vector<std::string> const & categories
10  , std::string const & regex
11  , std::string const & test
12  , bool persistent_cond
13  , int occur
14  , bool at_least
15  , int timespan
16  , bool per_source
17  , bool per_target
18  , int target_group
19  , ma_timing_events & events
20  )
21 : description_ ( desc )
22  , severity_(get_sev_from_string(sev))
23  , srcs_str ( )
24 , e_srcs ( )
25 , any_src ( false )
26 , cats_str ( )
27 , e_cats ( )
28 , any_cat ( false )
29 , match_type ( MATCH_REGEX )
30 , regex_str ( regex )
31 , e ( regex )
32 , test_expr ( )
33 , tc ( at_least ? occur : occur+1 )
34 , at_least_ ( at_least )
35 , ts ( timespan )
36 , ps ( per_source )
37 , pt ( per_target )
38 , t_group ( target_group )
39 , persistent_ ( persistent_cond )
40 , hitmap ( )
41 , events ( events )
42 , sev_ ( )
43 , src_ ( )
44 , tgt_ ( )
45 , cat_ ( )
46 , bdy_ ( )
47 , what_ ( )
48 , last_sev_ ( )
49 , last_src_ ( )
50 , last_tgt_ ( )
51 , last_cat_ ( )
52 , last_bdy_ ( )
53 , last_what_ ( )
54 , notify_on_source ( )
55 , notify_on_target ( )
56 , notify_on_status ( )
57 , catched_messages ( 0 )
58 {
59  // parse sources
60  std::vector<std::string>::const_iterator it = sources.begin();
61  while(it!=sources.end())
62  {
63  if (*it == "*")
64  {
65  any_src = true;
66  e_srcs.clear();
67  srcs_str = "any, ";
68  break;
69  }
70  e_srcs.push_back(regex_t(*it));
71  srcs_str.append(*it).append(", ");
72  ++it;
73  }
74 
75  // remove the last ", "
76  srcs_str.resize(srcs_str.size()-2);
77 
78  // parse categories
79  it = categories.begin();
80  while(it!=categories.end())
81  {
82  if (*it == "*")
83  {
84  any_cat = true;
85  e_cats.clear();
86  cats_str = "any, ";
87  break;
88  }
89  e_cats.push_back(regex_t(*it));
90  cats_str.append(*it).append(", ");
91  ++it;
92  }
93 
94  // remove the last ", "
95  cats_str.resize(cats_str.size()-2);
96 
97  // regex or contains?
98  if (regex.empty() || (regex.compare("*")==0))
99  match_type = MATCH_ANY;
100  else
101  match_type = MATCH_REGEX;
102 
103  // test functions
104  if( !parse_condition_test( test, test_expr) )
105  throw std::runtime_error("condition test function parse failed");
106 }
107 
108 void
109  ma_condition::reset( )
110 {
111  hitmap.reset();
112  catched_messages = 0;
113 }
114 
115 void ma_condition::init( )
116 {
117  hitmap.set_parent(this);
118 }
119 
120 bool
121  ma_condition::match( qt_mf_msg const & msg
122  , conds_t & status
123  , conds_t & source
124  , conds_t & target )
125 {
126  extract_fields(msg);
127 
128  // filtering conditions
129  if (sev_<severity_) return false;
130  if (!match_srcs()) return false;
131  if (!match_cats()) return false;
132 
133  // match condition
134  if (!match_body()) return false;
135 
136  // update fields after passing the match condition
137  update_fields();
138 
139  // test condition
140  if (!match_test()) return false;
141 
142  // register to hitmap
143  unsigned int result = hitmap.capture(msg, src_, tgt_, what_);
144 
145  TLOG(TLVL_DEBUG) << "cond::match() result = " << result << " src = " << src_;
146 
147  // update reaction_start list
148  if (result & STATUS_CHANGE) status.push_back(this);
149  if (result & SOURCE_CHANGE) source.push_back(this);
150  if (result & TARGET_CHANGE) target.push_back(this);
151 
152  //if (result & STATUS_CHANGE) update_fields();
153 
154  ++catched_messages;
155 
156  return true;
157 }
158 
159 bool ma_condition::event( size_t src, size_t tgt, time_t t, conds_t & status )
160 {
161  if ( hitmap.event(src, tgt, t) )
162  {
163  // we have a status flip
164  status.push_back(this);
165  return true;
166  }
167 
168  return false;
169 }
170 
171 void ma_condition::extract_fields (qt_mf_msg const & msg)
172 {
173  sev_ = msg.sev();
174  get_source_from_msg(src_, msg);
175  cat_ = msg.cat().toStdString();
176  bdy_ = msg.text(false).toStdString();
177 }
178 
179 void ma_condition::update_fields ( )
180 {
181  last_sev_ = sev_;
182  last_src_ = src_;
183  last_tgt_ = tgt_;
184  last_cat_ = cat_;
185  last_bdy_ = bdy_;
186  last_what_ = what_;
187 }
188 
189 bool ma_condition::match_srcs ( )
190 {
191  if (any_src) return true;
192 
193  size_t imax = e_srcs.size();
194 
195  for (size_t i=0; i<imax; ++i)
196  {
197  if (boost::regex_match(src_, what_, e_srcs[i])) return true;
198  }
199 
200  return false;
201 }
202 
203 bool ma_condition::match_cats ( )
204 {
205  if (any_cat) return true;
206 
207  size_t imax = e_cats.size();
208 
209  for (size_t i=0; i<imax; ++i)
210  {
211  if (boost::regex_match(cat_, what_, e_cats[i])) return true;
212  }
213 
214  return false;
215 }
216 
217 bool ma_condition::match_body ( )
218 {
219  if (match_type == MATCH_ANY)
220  return true;
221 
222  if (boost::regex_search(bdy_, what_, e))
223  {
224  if( pt ) // per_target, now extract the target string
225  {
226  if( t_group > what_.size() )
227  throw std::runtime_error("match_body: target group does not exit");
228 
229  tgt_ = std::string(what_[t_group].first, what_[t_group].second);
230  }
231 
232  return true;
233  }
234 
235  return false;
236 }
237 
238 bool ma_condition::match_test ( )
239 {
240  return test_expr.evaluate( this );
241 }
Qt wrapper around MessageFacility message
Definition: qt_mf_msg.hh:37
sev_code_t sev() const
Get the severity of the message
Definition: qt_mf_msg.hh:76
QString const & text(bool mode) const
Get the text of the message
Definition: qt_mf_msg.hh:66
QString const & cat() const
Get the category of the message
Definition: qt_mf_msg.hh:86