Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / log / example / doc / core_record.cpp
1 /*
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)
6  */
7
8 #include <cstddef>
9 #include <iostream>
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>
14
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;
20
21 // We define our own severity levels
22 enum severity_level
23 {
24     normal,
25     notification,
26     warning,
27     error,
28     critical
29 };
30
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)
33 {
34     static const char* strings[] =
35     {
36         "normal",
37         "notification",
38         "warning",
39         "error",
40         "critical"
41     };
42
43     if (static_cast< std::size_t >(level) < sizeof(strings) / sizeof(*strings))
44         strm << strings[level];
45     else
46         strm << static_cast< int >(level);
47
48     return strm;
49 }
50
51 //[ example_core_record_visitation_extraction
52 //=enum severity_level { ... };
53 //=std::ostream& operator<< (std::ostream& strm, severity_level level);
54
55 struct print_visitor
56 {
57     typedef void result_type;
58     result_type operator() (severity_level level) const
59     {
60         std::cout << level << std::endl;
61     };
62 };
63
64 // Prints severity level through visitation API
65 void print_severity_visitation(logging::record const& rec)
66 {
67     logging::visit< severity_level >("Severity", rec, print_visitor());
68 }
69
70 // Prints severity level through extraction API
71 void print_severity_extraction(logging::record const& rec)
72 {
73     logging::value_ref< severity_level > level = logging::extract< severity_level >("Severity", rec);
74     std::cout << level << std::endl;
75 }
76 //]
77
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)
81 {
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())
85     {
86         logging::attribute_value const& value = it->second;
87
88         // A single attribute value can also be visited or extracted
89         std::cout << value.extract< severity_level >() << std::endl;
90     }
91 }
92 //]
93
94 //[ example_core_record_subscript
95 BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", severity_level)
96
97 // Prints severity level by using the subscript operator
98 void print_severity_subscript(logging::record const& rec)
99 {
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;
103 }
104 //]
105
106
107 int main(int, char*[])
108 {
109     logging::attribute_set attrs;
110     attrs.insert("Severity", attrs::make_constant(notification));
111
112     logging::record rec = logging::core::get()->open_record(attrs);
113     if (rec)
114     {
115         print_severity_visitation(rec);
116         print_severity_extraction(rec);
117         print_severity_lookup(rec);
118         print_severity_subscript(rec);
119     }
120
121     return 0;
122 }