]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/log/example/doc/expressions_channel_severity_filter.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / log / example / doc / expressions_channel_severity_filter.cpp
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 <cstddef>
9 #include <iostream>
10 #include <boost/log/expressions.hpp>
11 #include <boost/log/sources/severity_channel_logger.hpp>
12 #include <boost/log/sources/record_ostream.hpp>
13 #include <boost/log/utility/setup/console.hpp>
14 #include <boost/log/utility/setup/common_attributes.hpp>
15
16 namespace logging = boost::log;
17 namespace src = boost::log::sources;
18 namespace expr = boost::log::expressions;
19 namespace keywords = boost::log::keywords;
20
21 //[ example_expressions_channel_severity_filter
22 // We define our own severity levels
23 enum severity_level
24 {
25 normal,
26 notification,
27 warning,
28 error,
29 critical
30 };
31
32 // Define the attribute keywords
33 BOOST_LOG_ATTRIBUTE_KEYWORD(line_id, "LineID", unsigned int)
34 BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", severity_level)
35 BOOST_LOG_ATTRIBUTE_KEYWORD(channel, "Channel", std::string)
36
37 //<-
38 std::ostream& operator<< (std::ostream& strm, severity_level level)
39 {
40 static const char* strings[] =
41 {
42 "normal",
43 "notification",
44 "warning",
45 "error",
46 "critical"
47 };
48
49 if (static_cast< std::size_t >(level) < sizeof(strings) / sizeof(*strings))
50 strm << strings[level];
51 else
52 strm << static_cast< int >(level);
53
54 return strm;
55 }
56 //->
57
58 void init()
59 {
60 // Create a minimal severity table filter
61 typedef expr::channel_severity_filter_actor< std::string, severity_level > min_severity_filter;
62 min_severity_filter min_severity = expr::channel_severity_filter(channel, severity);
63
64 // Set up the minimum severity levels for different channels
65 min_severity["general"] = notification;
66 min_severity["network"] = warning;
67 min_severity["gui"] = error;
68
69 logging::add_console_log
70 (
71 std::clog,
72 keywords::filter = min_severity || severity >= critical,
73 keywords::format =
74 (
75 expr::stream
76 << line_id
77 << ": <" << severity
78 << "> [" << channel << "] "
79 << expr::smessage
80 )
81 );
82 }
83
84 // Define our logger type
85 typedef src::severity_channel_logger< severity_level, std::string > logger_type;
86
87 void test_logging(logger_type& lg, std::string const& channel_name)
88 {
89 BOOST_LOG_CHANNEL_SEV(lg, channel_name, normal) << "A normal severity level message";
90 BOOST_LOG_CHANNEL_SEV(lg, channel_name, notification) << "A notification severity level message";
91 BOOST_LOG_CHANNEL_SEV(lg, channel_name, warning) << "A warning severity level message";
92 BOOST_LOG_CHANNEL_SEV(lg, channel_name, error) << "An error severity level message";
93 BOOST_LOG_CHANNEL_SEV(lg, channel_name, critical) << "A critical severity level message";
94 }
95 //]
96
97 int main(int, char*[])
98 {
99 init();
100 logging::add_common_attributes();
101
102 logger_type lg;
103 test_logging(lg, "general");
104 test_logging(lg, "network");
105 test_logging(lg, "gui");
106 test_logging(lg, "filesystem");
107
108 return 0;
109 }