Line data Source code
1 : /*
2 : * FilterStream.cxx
3 : * ers
4 : *
5 : * Created by Matthias Wiesmann on 31.03.05.
6 : * Modified by Serguei Kolos on 11.08.05.
7 : * Copyright 2005 CERN. All rights reserved.
8 : *
9 : */
10 :
11 : #include <ers/internal/FilterStream.hpp>
12 : #include <ers/internal/Util.hpp>
13 : #include <ers/StreamFactory.hpp>
14 : #include <algorithm>
15 :
16 41 : ERS_REGISTER_OUTPUT_STREAM( ers::FilterStream, "filter", format )
17 :
18 : namespace
19 : {
20 : const char NOT = '!';
21 : const char * const SEPARATORS = "," ;
22 : } // anonymous namespace
23 :
24 : /** Constructor that creates a new instance of the filter stream with the given configuration.
25 : * Only messages that pass the given filter will go through, the others will be filtered out.
26 : * \param format filter expression.
27 : */
28 0 : ers::FilterStream::FilterStream( const std::string & format )
29 : {
30 0 : std::vector<std::string> tokens;
31 0 : ers::tokenize( format, SEPARATORS, tokens );
32 0 : for( size_t i = 0; i < tokens.size(); i++ )
33 : {
34 0 : if ( !tokens[i].empty() && tokens[i][0] == NOT )
35 0 : m_exclude.push_back( tokens[i].substr( 1 ) );
36 : else
37 0 : m_include.push_back( tokens[i] );
38 : }
39 0 : }
40 :
41 :
42 : /** Filtering method
43 : * This method checks if an Issue is to be accepted or not.
44 : * \param issue_ptr the issue to check
45 : * \return \c true if the Issue passes filtering, \c false otherwise.
46 : */
47 : bool
48 0 : ers::FilterStream::is_accepted( const ers::Issue & issue )
49 : {
50 0 : const std::vector<std::string> & qualifiers = issue.qualifiers( );
51 :
52 0 : std::vector<std::string>::const_iterator it;
53 0 : for( it = m_exclude.begin(); it != m_exclude.end(); ++it )
54 : {
55 0 : std::vector<std::string>::const_iterator conflict = std::find( qualifiers.begin(), qualifiers.end(), *it );
56 0 : if ( conflict != qualifiers.end() )
57 : {
58 0 : return false;
59 : }
60 : }
61 :
62 0 : for( it = m_include.begin(); it != m_include.end(); ++it )
63 : {
64 0 : std::vector<std::string>::const_iterator match = std::find( qualifiers.begin(), qualifiers.end(), *it );
65 0 : if ( match != qualifiers.end() )
66 : {
67 0 : return true;
68 : }
69 : }
70 :
71 0 : return m_include.empty();
72 : }
73 :
74 : /** Write method
75 : * basically calls \c is_accept to check if the issue is accepted.
76 : * If this is the case, the \c write method on the chained stream is called with
77 : * \c issue.
78 : * \param issue issue to be sent.
79 : */
80 : void
81 0 : ers::FilterStream::write( const ers::Issue & issue )
82 : {
83 0 : if ( is_accepted( issue ) )
84 : {
85 0 : chained().write( issue );
86 : }
87 0 : } // send
88 :
|