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)
11 #include <boost/smart_ptr/shared_ptr.hpp>
12 #include <boost/smart_ptr/make_shared_object.hpp>
13 #include <boost/lexical_cast.hpp>
14 #include <boost/phoenix.hpp>
15 #include <boost/log/core.hpp>
16 #include <boost/log/expressions.hpp>
17 #include <boost/log/attributes/attribute_name.hpp>
18 #include <boost/log/attributes/scoped_attribute.hpp>
19 #include <boost/log/sources/logger.hpp>
20 #include <boost/log/sources/record_ostream.hpp>
21 #include <boost/log/utility/value_ref.hpp>
22 #include <boost/log/utility/formatting_ostream.hpp>
23 #include <boost/log/utility/manipulators/add_value.hpp>
24 #include <boost/log/utility/setup/filter_parser.hpp>
25 #include <boost/log/utility/setup/common_attributes.hpp>
26 #include <boost/log/utility/setup/console.hpp>
28 namespace logging
= boost::log
;
29 namespace attrs
= boost::log::attributes
;
30 namespace src
= boost::log::sources
;
31 namespace expr
= boost::log::expressions
;
32 namespace sinks
= boost::log::sinks
;
33 namespace keywords
= boost::log::keywords
;
35 //[ example_extension_filter_parser_point_definition
40 point() : m_x(0.0f
), m_y(0.0f
) {}
41 point(float x
, float y
) : m_x(x
), m_y(y
) {}
44 bool operator== (point
const& left
, point
const& right
);
45 bool operator!= (point
const& left
, point
const& right
);
47 template< typename CharT
, typename TraitsT
>
48 std::basic_ostream
< CharT
, TraitsT
>& operator<< (std::basic_ostream
< CharT
, TraitsT
>& strm
, point
const& p
);
49 template< typename CharT
, typename TraitsT
>
50 std::basic_istream
< CharT
, TraitsT
>& operator>> (std::basic_istream
< CharT
, TraitsT
>& strm
, point
& p
);
53 const float epsilon
= 0.0001f
;
55 bool operator== (point
const& left
, point
const& right
)
57 return (left
.m_x
- epsilon
<= right
.m_x
&& left
.m_x
+ epsilon
>= right
.m_x
) &&
58 (left
.m_y
- epsilon
<= right
.m_y
&& left
.m_y
+ epsilon
>= right
.m_y
);
61 bool operator!= (point
const& left
, point
const& right
)
63 return !(left
== right
);
66 template< typename CharT
, typename TraitsT
>
67 std::basic_ostream
< CharT
, TraitsT
>& operator<< (std::basic_ostream
< CharT
, TraitsT
>& strm
, point
const& p
)
70 strm
<< "(" << p
.m_x
<< ", " << p
.m_y
<< ")";
74 template< typename CharT
, typename TraitsT
>
75 std::basic_istream
< CharT
, TraitsT
>& operator>> (std::basic_istream
< CharT
, TraitsT
>& strm
, point
& p
)
79 CharT left_brace
= static_cast< CharT
>(0), comma
= static_cast< CharT
>(0), right_brace
= static_cast< CharT
>(0);
80 strm
.setf(std::ios_base::skipws
);
81 strm
>> left_brace
>> p
.m_x
>> comma
>> p
.m_y
>> right_brace
;
82 if (left_brace
!= '(' || comma
!= ',' || right_brace
!= ')')
83 strm
.setstate(std::ios_base::failbit
);
89 //[ example_extension_simple_filter_factory
93 logging::register_simple_formatter_factory
< point
, char >("Coordinates");
95 logging::register_simple_filter_factory
< point
, char >("Coordinates");
100 //[ example_extension_custom_filter_factory
101 // Custom point filter factory
102 class point_filter_factory
:
103 public logging::filter_factory
< char >
106 logging::filter
on_exists_test(logging::attribute_name
const& name
)
108 return expr::has_attr
< point
>(name
);
111 logging::filter
on_equality_relation(logging::attribute_name
const& name
, string_type
const& arg
)
113 return expr::attr
< point
>(name
) == boost::lexical_cast
< point
>(arg
);
116 logging::filter
on_inequality_relation(logging::attribute_name
const& name
, string_type
const& arg
)
118 return expr::attr
< point
>(name
) != boost::lexical_cast
< point
>(arg
);
122 void init_factories()
125 logging::register_simple_formatter_factory
< point
, char >("Coordinates");
127 logging::register_filter_factory("Coordinates", boost::make_shared
< point_filter_factory
>());
135 logging::add_console_log
138 keywords::filter
= "%Coordinates% = \"(10, 10)\"",
139 keywords::format
= "%TimeStamp% %Coordinates% %Message%"
142 logging::add_common_attributes();
145 int main(int, char*[])
151 // We have to use scoped attributes in order coordinates to be passed to filters
153 BOOST_LOG_SCOPED_LOGGER_TAG(lg
, "Coordinates", point(10, 10));
154 BOOST_LOG(lg
) << "Hello, world with coordinates (10, 10)!";
157 BOOST_LOG_SCOPED_LOGGER_TAG(lg
, "Coordinates", point(20, 20));
158 BOOST_LOG(lg
) << "Hello, world with coordinates (20, 20)!"; // this message will be suppressed by filter