]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/log/example/doc/tutorial_filtering.cpp
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)
13 #include <boost/smart_ptr/shared_ptr.hpp>
14 #include <boost/smart_ptr/make_shared_object.hpp>
15 #include <boost/phoenix/bind.hpp>
16 #include <boost/date_time/posix_time/posix_time.hpp>
17 #include <boost/log/core.hpp>
18 #include <boost/log/expressions.hpp>
19 #include <boost/log/attributes.hpp>
20 #include <boost/log/sources/basic_logger.hpp>
21 #include <boost/log/sources/severity_logger.hpp>
22 #include <boost/log/sources/severity_channel_logger.hpp>
23 #include <boost/log/sources/record_ostream.hpp>
24 #include <boost/log/sinks/sync_frontend.hpp>
25 #include <boost/log/sinks/text_ostream_backend.hpp>
26 #include <boost/log/attributes/scoped_attribute.hpp>
27 #include <boost/log/utility/value_ref.hpp>
28 #include <boost/log/utility/setup/common_attributes.hpp>
30 namespace logging
= boost::log
;
31 namespace src
= boost::log::sources
;
32 namespace expr
= boost::log::expressions
;
33 namespace sinks
= boost::log::sinks
;
34 namespace attrs
= boost::log::attributes
;
35 namespace keywords
= boost::log::keywords
;
37 // We define our own severity levels
47 // The operator puts a human-friendly representation of the severity level to the stream
48 std::ostream
& operator<< (std::ostream
& strm
, severity_level level
)
50 static const char* strings
[] =
59 if (static_cast< std::size_t >(level
) < sizeof(strings
) / sizeof(*strings
))
60 strm
<< strings
[level
];
62 strm
<< static_cast< int >(level
);
67 //[ example_tutorial_filtering
68 BOOST_LOG_ATTRIBUTE_KEYWORD(line_id
, "LineID", unsigned int)
69 BOOST_LOG_ATTRIBUTE_KEYWORD(severity
, "Severity", severity_level
)
70 BOOST_LOG_ATTRIBUTE_KEYWORD(tag_attr
, "Tag", std::string
)
74 // Setup the common formatter for all sinks
75 logging::formatter fmt
= expr::stream
76 << std::setw(6) << std::setfill('0') << line_id
<< std::setfill(' ')
77 << ": <" << severity
<< ">\t"
78 << expr::if_(expr::has_attr(tag_attr
))
80 expr::stream
<< "[" << tag_attr
<< "] "
85 typedef sinks::synchronous_sink
< sinks::text_ostream_backend
> text_sink
;
86 boost::shared_ptr
< text_sink
> sink
= boost::make_shared
< text_sink
>();
88 sink
->locked_backend()->add_stream(
89 boost::make_shared
< std::ofstream
>("full.log"));
91 sink
->set_formatter(fmt
);
93 logging::core::get()->add_sink(sink
);
95 sink
= boost::make_shared
< text_sink
>();
97 sink
->locked_backend()->add_stream(
98 boost::make_shared
< std::ofstream
>("important.log"));
100 sink
->set_formatter(fmt
);
102 sink
->set_filter(severity
>= warning
|| (expr::has_attr(tag_attr
) && tag_attr
== "IMPORTANT_MESSAGE"));
104 logging::core::get()->add_sink(sink
);
107 logging::add_common_attributes();
113 //[ example_tutorial_filtering_bind
114 bool my_filter(logging::value_ref
< severity_level
, tag::severity
> const& level
,
115 logging::value_ref
< std::string
, tag::tag_attr
> const& tag
)
117 return level
>= warning
|| tag
== "IMPORTANT_MESSAGE";
124 // Setup the common formatter for all sinks
125 logging::formatter fmt
= expr::stream
126 << std::setw(6) << std::setfill('0') << line_id
<< std::setfill(' ')
127 << ": <" << severity
<< ">\t"
128 << expr::if_(expr::has_attr(tag_attr
))
130 expr::stream
<< "[" << tag_attr
<< "] "
135 typedef sinks::synchronous_sink
< sinks::text_ostream_backend
> text_sink
;
136 boost::shared_ptr
< text_sink
> sink
= boost::make_shared
< text_sink
>();
138 sink
->locked_backend()->add_stream(
139 boost::make_shared
< std::ofstream
>("full.log"));
141 sink
->set_formatter(fmt
);
143 logging::core::get()->add_sink(sink
);
145 sink
= boost::make_shared
< text_sink
>();
147 sink
->locked_backend()->add_stream(
148 boost::make_shared
< std::ofstream
>("important.log"));
150 sink
->set_formatter(fmt
);
155 namespace phoenix
= boost::phoenix
;
156 sink
->set_filter(phoenix::bind(&my_filter
, severity
.or_none(), tag_attr
.or_none()));
161 logging::core::get()->add_sink(sink
);
164 logging::add_common_attributes();
172 void logging_function()
174 src::severity_logger
< severity_level
> slg
;
176 BOOST_LOG_SEV(slg
, normal
) << "A regular message";
177 BOOST_LOG_SEV(slg
, warning
) << "Something bad is going on but I can handle it";
178 BOOST_LOG_SEV(slg
, critical
) << "Everything crumbles, shoot me now!";
181 BOOST_LOG_SCOPED_THREAD_TAG("Tag", "IMPORTANT_MESSAGE");
182 BOOST_LOG_SEV(slg
, normal
) << "An important message";
186 int main(int, char*[])