MIRA
LogCustomizableFilter.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
47 #ifndef _MIRA_LOGCUSTOMIZABLEFILTER_H_
48 #define _MIRA_LOGCUSTOMIZABLEFILTER_H_
49 
50 #include <error/LoggingCore.h>
51 
52 namespace mira {
53 
55 
80 {
81 public:
83  {
84  LESS = 0,
89  NOT,
90  };
91 
96  {
97  public:
98  virtual ~CustomFilterBase() {}
105  virtual bool filter(const LogRecord& record) = 0;
106  };
107 
112  template <typename Derived>
113  class CustomFilter : public CustomFilterBase {};
114 
118  template<LogCustomizableFilter::CompareFlags F = LogCustomizableFilter::EQUAL>
119  class Class : public LogCustomizableFilter::CustomFilter<LogCustomizableFilter::Class<F> >
120  {
121  public:
122  Class(const std::string& iClassName) :
123  className(iClassName)
124  {
125  }
126 
127  virtual bool filter(const LogRecord& record)
128  {
130  return MIRA_LOGGER.parseFunction(record).className == className;
132  return MIRA_LOGGER.parseFunction(record).className != className;
133  return false;
134  }
135 
136  std::string className;
137  };
138 
142  template<LogCustomizableFilter::CompareFlags F = LogCustomizableFilter::EQUAL>
143  class Namespace : public LogCustomizableFilter::CustomFilter<LogCustomizableFilter::Namespace<F> >
144  {
145  public:
146  Namespace(const std::string& iNS) :
147  ns(iNS)
148  {
149  }
150 
151  virtual bool filter(const LogRecord& record)
152  {
154  return MIRA_LOGGER.parseFunction(record).nameSpace == ns;
156  return MIRA_LOGGER.parseFunction(record).nameSpace != ns;
157  return false;
158  }
159 
160  std::string ns;
161  };
162 
166  template<LogCustomizableFilter::CompareFlags F = LogCustomizableFilter::LESS_EQUAL>
167  class Severity : public LogCustomizableFilter::CustomFilter<LogCustomizableFilter::Severity<F> >
168  {
169  public:
171  level(iLevel)
172  {
173  }
174 
175  virtual bool filter(const LogRecord& record)
176  {
178  return record.level < level;
180  return record.level <= level;
182  return record.level == level;
184  return record.level >= level;
186  return record.level > level;
188  return record.level != level;
189  return false;
190  }
191 
193  };
194 
198  template<LogCustomizableFilter::CompareFlags F = LogCustomizableFilter::EQUAL>
199  class Thread : public LogCustomizableFilter::CustomFilter<LogCustomizableFilter::Thread<F> >
200  {
201  public:
202  Thread(ThreadID iThreadID) :
203  threadID(iThreadID)
204  {
205  }
206 
207  virtual bool filter(const LogRecord& record)
208  {
210  return record.threadID == threadID;
212  return record.threadID != threadID;
213  return false;
214  }
215 
217  };
218 
222  template <typename F1, typename F2>
223  class AndOperator : public LogCustomizableFilter::CustomFilter<LogCustomizableFilter::AndOperator<F1, F2> >
224  {
225  public:
226  AndOperator(const F1& f1, const F2& f2)
227  {
228  mF1.reset(new F1(f1));
229  mF2.reset(new F2(f2));
230  }
231 
232  virtual bool filter(const LogRecord& record)
233  {
234  return mF1->filter(record) && mF2->filter(record);
235  }
236 
237  private:
238  boost::shared_ptr<LogCustomizableFilter::CustomFilterBase> mF1;
239  boost::shared_ptr<LogCustomizableFilter::CustomFilterBase> mF2;
240  };
241 
245  template <typename F1, typename F2>
246  class OrOperator : public LogCustomizableFilter::CustomFilter<LogCustomizableFilter::OrOperator<F1, F2> >
247  {
248  public:
249  OrOperator(const F1& f1, const F2& f2)
250  {
251  mF1.reset(new F1(f1));
252  mF2.reset(new F2(f2));
253  }
254 
255  virtual bool filter(const LogRecord& record)
256  {
257  return mF1->filter(record) || mF2->filter(record);
258  }
259 
260  private:
261  boost::shared_ptr<LogCustomizableFilter::CustomFilterBase> mF1;
262  boost::shared_ptr<LogCustomizableFilter::CustomFilterBase> mF2;
263  };
264 
270  template<typename T>
272  {
273  mFilter.push_back(boost::shared_ptr<
275  return *this;
276  }
277 
278 
279  virtual bool filter(const LogRecord& record)
280  {
281  for (size_t i = 0; i < mFilter.size(); ++i)
282  if (!mFilter[i]->filter(record))
283  return false;
284  return true;
285  }
286 
287 protected:
288  std::vector<boost::shared_ptr<LogCustomizableFilter::CustomFilterBase> > mFilter;
289 };
290 
295 template <typename Derived1, typename Derived2>
298 {
299  const Derived1* t1 = (const Derived1*)&f1;
300  const Derived2* t2 = (const Derived2*)&f2;
302 }
303 
308 template <typename Derived1, typename Derived2>
311 {
312  const Derived1* t1 = (const Derived1*)&f1;
313  const Derived2* t2 = (const Derived2*)&f2;
315 }
316 
318 
319 }
320 
321 #endif
SeverityLevel level
Definition: LogCustomizableFilter.h:192
SeverityLevel level
Definition: LoggingCore.h:108
Class filter, used to filter log entries with a given class name.
Definition: LogCustomizableFilter.h:119
Severity(SeverityLevel iLevel)
Definition: LogCustomizableFilter.h:170
ThreadID threadID
Definition: LogCustomizableFilter.h:216
Holds all the information about a log entry.
Definition: LoggingCore.h:106
AndOperator(const F1 &f1, const F2 &f2)
Definition: LogCustomizableFilter.h:226
LogCustomizableFilter & operator<<(const T &f)
Add filters by chaining them with this operator.
Definition: LogCustomizableFilter.h:271
std::string ns
Definition: LogCustomizableFilter.h:160
Abstract base class for log filters.
Definition: LoggingCore.h:182
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
A customizable log filter to match the users needs.
Definition: LogCustomizableFilter.h:79
virtual bool filter(const LogRecord &record)
Called by LogCustomizableFilter whenever a new log entry is getting filtered.
Definition: LogCustomizableFilter.h:255
Class(const std::string &iClassName)
Definition: LogCustomizableFilter.h:122
virtual bool filter(const LogRecord &record)
Filters a log entry.
Definition: LogCustomizableFilter.h:279
Namespace filter, used to filter log entries with a given namespace.
Definition: LogCustomizableFilter.h:143
CompareFlags
Definition: LogCustomizableFilter.h:82
uint32 ThreadID
Platform independent thread ID.
Definition: ThreadID.h:68
LogCustomizableFilter::AndOperator< Derived1, Derived2 > operator &(const LogCustomizableFilter::CustomFilter< Derived1 > &f1, const LogCustomizableFilter::CustomFilter< Derived2 > &f2)
Operator to combine filters by and.
Definition: LogCustomizableFilter.h:296
SeverityLevel
Severity levels to graduate between different log outputs.
Definition: LoggingCore.h:71
Definition: LogCustomizableFilter.h:85
virtual bool filter(const LogRecord &record)
Called by LogCustomizableFilter whenever a new log entry is getting filtered.
Definition: LogCustomizableFilter.h:232
Definition: LogCustomizableFilter.h:88
Operator class used to combine two custom filters by the & operator.
Definition: LogCustomizableFilter.h:223
virtual bool filter(const LogRecord &record)
Called by LogCustomizableFilter whenever a new log entry is getting filtered.
Definition: LogCustomizableFilter.h:127
Definition: LogCustomizableFilter.h:87
Namespace(const std::string &iNS)
Definition: LogCustomizableFilter.h:146
Core class of the logging library.
Definition: LogCustomizableFilter.h:84
virtual bool filter(const LogRecord &record)
Called by LogCustomizableFilter whenever a new log entry is getting filtered.
Definition: LogCustomizableFilter.h:207
LogCustomizableFilter::OrOperator< Derived1, Derived2 > operator|(const LogCustomizableFilter::CustomFilter< Derived1 > &f1, const LogCustomizableFilter::CustomFilter< Derived2 > &f2)
Operator to combine filters by or.
Definition: LogCustomizableFilter.h:309
Definition: LogCustomizableFilter.h:89
Severity filter, used to filter log entries with a given severity level.
Definition: LogCustomizableFilter.h:167
Operator class used to combine two custom filters by the | operator.
Definition: LogCustomizableFilter.h:246
virtual bool filter(const LogRecord &record)=0
Called by LogCustomizableFilter whenever a new log entry is getting filtered.
virtual ~CustomFilterBase()
Definition: LogCustomizableFilter.h:98
std::string className
Definition: LogCustomizableFilter.h:136
ThreadID filter, used to filter log entries with a given thread id.
Definition: LogCustomizableFilter.h:199
virtual bool filter(const LogRecord &record)
Called by LogCustomizableFilter whenever a new log entry is getting filtered.
Definition: LogCustomizableFilter.h:151
ThreadID threadID
Definition: LoggingCore.h:116
Thread(ThreadID iThreadID)
Definition: LogCustomizableFilter.h:202
virtual bool filter(const LogRecord &record)
Called by LogCustomizableFilter whenever a new log entry is getting filtered.
Definition: LogCustomizableFilter.h:175
Abstract base class for custom filters.
Definition: LogCustomizableFilter.h:95
Helper class to get the type info of the derived class.
Definition: LogCustomizableFilter.h:113
Definition: LogCustomizableFilter.h:86
#define MIRA_LOGGER
Macro for easier access to the logging core instance.
Definition: LoggingCore.h:416
OrOperator(const F1 &f1, const F2 &f2)
Definition: LogCustomizableFilter.h:249
std::vector< boost::shared_ptr< LogCustomizableFilter::CustomFilterBase > > mFilter
Definition: LogCustomizableFilter.h:288