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)
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>
16 namespace logging
= boost::log
;
17 namespace src
= boost::log::sources
;
18 namespace expr
= boost::log::expressions
;
19 namespace keywords
= boost::log::keywords
;
21 //[ example_expressions_channel_severity_filter
22 // We define our own severity levels
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
)
38 std::ostream
& operator<< (std::ostream
& strm
, severity_level level
)
40 static const char* strings
[] =
49 if (static_cast< std::size_t >(level
) < sizeof(strings
) / sizeof(*strings
))
50 strm
<< strings
[level
];
52 strm
<< static_cast< int >(level
);
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
);
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
;
69 logging::add_console_log
72 keywords::filter
= min_severity
|| severity
>= critical
,
78 << "> [" << channel
<< "] "
84 // Define our logger type
85 typedef src::severity_channel_logger
< severity_level
, std::string
> logger_type
;
87 void test_logging(logger_type
& lg
, std::string
const& channel_name
)
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";
97 int main(int, char*[])
100 logging::add_common_attributes();
103 test_logging(lg
, "general");
104 test_logging(lg
, "network");
105 test_logging(lg
, "gui");
106 test_logging(lg
, "filesystem");