2 * Copyright Andrey Semashev 2007 - 2014.
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/core.hpp>
11 #include <boost/log/expressions.hpp>
12 #include <boost/log/attributes.hpp>
13 #include <boost/log/utility/value_ref.hpp>
15 namespace logging = boost::log;
16 namespace expr = boost::log::expressions;
17 namespace sinks = boost::log::sinks;
18 namespace attrs = boost::log::attributes;
19 namespace keywords = boost::log::keywords;
21 // We define our own severity levels
31 // The operator puts a human-friendly representation of the severity level to the stream
32 std::ostream& operator<< (std::ostream& strm, severity_level level)
34 static const char* strings[] =
43 if (static_cast< std::size_t >(level) < sizeof(strings) / sizeof(*strings))
44 strm << strings[level];
46 strm << static_cast< int >(level);
51 //[ example_core_record_visitation_extraction
52 //=enum severity_level { ... };
53 //=std::ostream& operator<< (std::ostream& strm, severity_level level);
57 typedef void result_type;
58 result_type operator() (severity_level level) const
60 std::cout << level << std::endl;
64 // Prints severity level through visitation API
65 void print_severity_visitation(logging::record const& rec)
67 logging::visit< severity_level >("Severity", rec, print_visitor());
70 // Prints severity level through extraction API
71 void print_severity_extraction(logging::record const& rec)
73 logging::value_ref< severity_level > level = logging::extract< severity_level >("Severity", rec);
74 std::cout << level << std::endl;
78 //[ example_core_record_attr_value_lookup
79 // Prints severity level by searching the attribute values
80 void print_severity_lookup(logging::record const& rec)
82 logging::attribute_value_set const& values = rec.attribute_values();
83 logging::attribute_value_set::const_iterator it = values.find("Severity");
84 if (it != values.end())
86 logging::attribute_value const& value = it->second;
88 // A single attribute value can also be visited or extracted
89 std::cout << value.extract< severity_level >() << std::endl;
94 //[ example_core_record_subscript
95 BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", severity_level)
97 // Prints severity level by using the subscript operator
98 void print_severity_subscript(logging::record const& rec)
100 // Use the attribute keyword to communicate the name and type of the value
101 logging::value_ref< severity_level, tag::severity > level = rec[severity];
102 std::cout << level << std::endl;
107 int main(int, char*[])
109 logging::attribute_set attrs;
110 attrs.insert("Severity", attrs::make_constant(notification));
112 logging::record rec = logging::core::get()->open_record(attrs);
115 print_severity_visitation(rec);
116 print_severity_extraction(rec);
117 print_severity_lookup(rec);
118 print_severity_subscript(rec);