]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/log/example/doc/extension_filter_parser.cpp
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / log / example / doc / extension_filter_parser.cpp
CommitLineData
7c673cae
FG
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#include <string>
9#include <iostream>
10#include <stdexcept>
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>
27
28namespace logging = boost::log;
29namespace attrs = boost::log::attributes;
30namespace src = boost::log::sources;
31namespace expr = boost::log::expressions;
32namespace sinks = boost::log::sinks;
33namespace keywords = boost::log::keywords;
34
35//[ example_extension_filter_parser_point_definition
36struct point
37{
38 float m_x, m_y;
39
40 point() : m_x(0.0f), m_y(0.0f) {}
41 point(float x, float y) : m_x(x), m_y(y) {}
42};
43
44bool operator== (point const& left, point const& right);
45bool operator!= (point const& left, point const& right);
46
47template< typename CharT, typename TraitsT >
48std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, point const& p);
49template< typename CharT, typename TraitsT >
50std::basic_istream< CharT, TraitsT >& operator>> (std::basic_istream< CharT, TraitsT >& strm, point& p);
51//]
52
53const float epsilon = 0.0001f;
54
55bool operator== (point const& left, point const& right)
56{
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);
59}
60
61bool operator!= (point const& left, point const& right)
62{
63 return !(left == right);
64}
65
66template< typename CharT, typename TraitsT >
67std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, point const& p)
68{
69 if (strm.good())
70 strm << "(" << p.m_x << ", " << p.m_y << ")";
71 return strm;
72}
73
74template< typename CharT, typename TraitsT >
75std::basic_istream< CharT, TraitsT >& operator>> (std::basic_istream< CharT, TraitsT >& strm, point& p)
76{
77 if (strm.good())
78 {
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);
84 }
85 return strm;
86}
87
88#if 0
89//[ example_extension_simple_filter_factory
90void init_factories()
91{
92 //<-
93 logging::register_simple_formatter_factory< point, char >("Coordinates");
94 //->
95 logging::register_simple_filter_factory< point, char >("Coordinates");
96}
97//]
98#endif
99
100//[ example_extension_custom_filter_factory
101// Custom point filter factory
102class point_filter_factory :
103 public logging::filter_factory< char >
104{
105public:
106 logging::filter on_exists_test(logging::attribute_name const& name)
107 {
108 return expr::has_attr< point >(name);
109 }
110
111 logging::filter on_equality_relation(logging::attribute_name const& name, string_type const& arg)
112 {
113 return expr::attr< point >(name) == boost::lexical_cast< point >(arg);
114 }
115
116 logging::filter on_inequality_relation(logging::attribute_name const& name, string_type const& arg)
117 {
118 return expr::attr< point >(name) != boost::lexical_cast< point >(arg);
119 }
120};
121
122void init_factories()
123{
124 //<-
125 logging::register_simple_formatter_factory< point, char >("Coordinates");
126 //->
127 logging::register_filter_factory("Coordinates", boost::make_shared< point_filter_factory >());
128}
129//]
130
131void init_logging()
132{
133 init_factories();
134
135 logging::add_console_log
136 (
137 std::clog,
138 keywords::filter = "%Coordinates% = \"(10, 10)\"",
139 keywords::format = "%TimeStamp% %Coordinates% %Message%"
140 );
141
142 logging::add_common_attributes();
143}
144
145int main(int, char*[])
146{
147 init_logging();
148
149 src::logger lg;
150
151 // We have to use scoped attributes in order coordinates to be passed to filters
152 {
153 BOOST_LOG_SCOPED_LOGGER_TAG(lg, "Coordinates", point(10, 10));
154 BOOST_LOG(lg) << "Hello, world with coordinates (10, 10)!";
155 }
156 {
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
159 }
160
161 return 0;
162}