1 /*
2  *          Copyright Andrey Semashev 2007 - 2015.
3  * Distributed under the Boost Software License, Version 1.0.
4  *    (See accompanying file LICENSE_1_0.txt or copy at
5  *          http://www.boost.org/LICENSE_1_0.txt)
6  */
7 /*!
8  * \file   test_sink.hpp
9  * \author Andrey Semashev
10  * \date   18.03.2009
11  *
12  * \brief  This header contains a test sink frontend that is used through various tests.
13  */
14 
15 #ifndef BOOST_LOG_TESTS_TEST_SINK_HPP_INCLUDED_
16 #define BOOST_LOG_TESTS_TEST_SINK_HPP_INCLUDED_
17 
18 #include <cstddef>
19 #include <map>
20 #include <boost/log/core/record_view.hpp>
21 #include <boost/log/attributes/attribute_name.hpp>
22 #include <boost/log/attributes/attribute_value_set.hpp>
23 #include <boost/log/sinks/sink.hpp>
24 #include <boost/log/expressions/filter.hpp>
25 
26 //! A sink implementation for testing purpose
27 struct test_sink :
28     public boost::log::sinks::sink
29 {
30 public:
31     typedef boost::log::attribute_value_set attribute_values;
32     typedef boost::log::record_view record_type;
33     typedef boost::log::filter filter_type;
34     typedef attribute_values::key_type key_type;
35 
36     struct key_type_order
37     {
38         typedef bool result_type;
39 
operator ()test_sink::key_type_order40         result_type operator() (key_type const& left, key_type const& right) const
41         {
42             return left.id() < right.id();
43         }
44     };
45 
46     typedef std::map< key_type, std::size_t, key_type_order > attr_counters_map;
47 
48 public:
49     filter_type m_Filter;
50     attr_counters_map m_Consumed;
51     std::size_t m_RecordCounter;
52 
53 public:
test_sinktest_sink54     test_sink() : boost::log::sinks::sink(false), m_RecordCounter(0) {}
55 
set_filtertest_sink56     void set_filter(filter_type const& f)
57     {
58         m_Filter = f;
59     }
60 
reset_filtertest_sink61     void reset_filter()
62     {
63         m_Filter.reset();
64     }
65 
will_consumetest_sink66     bool will_consume(attribute_values const& attributes)
67     {
68         return m_Filter(attributes);
69     }
70 
consumetest_sink71     void consume(record_type const& record)
72     {
73         ++m_RecordCounter;
74         attribute_values::const_iterator
75             it = record.attribute_values().begin(),
76             end = record.attribute_values().end();
77         for (; it != end; ++it)
78             ++m_Consumed[it->first];
79     }
80 
flushtest_sink81     void flush()
82     {
83     }
84 
cleartest_sink85     void clear()
86     {
87         m_RecordCounter = 0;
88         m_Consumed.clear();
89     }
90 };
91 
92 #endif // BOOST_LOG_TESTS_TEST_SINK_HPP_INCLUDED_
93