]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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_logger.hpp> | |
12 | #include <boost/log/sources/record_ostream.hpp> | |
13 | #include <boost/log/utility/formatting_ostream.hpp> | |
14 | #include <boost/log/utility/manipulators/to_log.hpp> | |
15 | #include <boost/log/utility/setup/console.hpp> | |
16 | #include <boost/log/utility/setup/common_attributes.hpp> | |
17 | ||
18 | namespace logging = boost::log; | |
19 | namespace src = boost::log::sources; | |
20 | namespace expr = boost::log::expressions; | |
21 | namespace keywords = boost::log::keywords; | |
22 | ||
23 | //[ example_expressions_keyword_formatter_stream_tag | |
24 | // We define our own severity levels | |
25 | enum severity_level | |
26 | { | |
27 | normal, | |
28 | notification, | |
29 | warning, | |
30 | error, | |
31 | critical | |
32 | }; | |
33 | ||
34 | // Define the attribute keywords | |
35 | BOOST_LOG_ATTRIBUTE_KEYWORD(line_id, "LineID", unsigned int) | |
36 | BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", severity_level) | |
37 | ||
38 | // The operator is used for regular stream formatting | |
39 | std::ostream& operator<< (std::ostream& strm, severity_level level) | |
40 | { | |
41 | static const char* strings[] = | |
42 | { | |
43 | "normal", | |
44 | "notification", | |
45 | "warning", | |
46 | "error", | |
47 | "critical" | |
48 | }; | |
49 | ||
50 | if (static_cast< std::size_t >(level) < sizeof(strings) / sizeof(*strings)) | |
51 | strm << strings[level]; | |
52 | else | |
53 | strm << static_cast< int >(level); | |
54 | ||
55 | return strm; | |
56 | } | |
57 | ||
58 | // The operator is used when putting the severity level to log | |
59 | logging::formatting_ostream& operator<< | |
60 | ( | |
61 | logging::formatting_ostream& strm, | |
62 | logging::to_log_manip< severity_level, tag::severity > const& manip | |
63 | ) | |
64 | { | |
65 | static const char* strings[] = | |
66 | { | |
67 | "NORM", | |
68 | "NTFY", | |
69 | "WARN", | |
70 | "ERRR", | |
71 | "CRIT" | |
72 | }; | |
73 | ||
74 | severity_level level = manip.get(); | |
75 | if (static_cast< std::size_t >(level) < sizeof(strings) / sizeof(*strings)) | |
76 | strm << strings[level]; | |
77 | else | |
78 | strm << static_cast< int >(level); | |
79 | ||
80 | return strm; | |
81 | } | |
82 | ||
83 | void init() | |
84 | { | |
85 | logging::add_console_log | |
86 | ( | |
87 | std::clog, | |
88 | // This makes the sink to write log records that look like this: | |
89 | // 1: <NORM> A normal severity message | |
90 | // 2: <ERRR> An error severity message | |
91 | keywords::format = | |
92 | ( | |
93 | expr::stream | |
94 | << line_id | |
95 | << ": <" << severity | |
96 | << "> " << expr::smessage | |
97 | ) | |
98 | ); | |
99 | } | |
100 | //] | |
101 | ||
102 | //[ example_expressions_keyword_lookup | |
103 | void print_severity(logging::record_view const& rec) | |
104 | { | |
105 | logging::value_ref< severity_level, tag::severity > level = rec[severity]; | |
106 | std::cout << level << std::endl; | |
107 | } | |
108 | //] | |
109 | ||
110 | ||
111 | int main(int, char*[]) | |
112 | { | |
113 | init(); | |
114 | logging::add_common_attributes(); | |
115 | ||
116 | src::severity_logger< severity_level > lg; | |
117 | ||
118 | // These messages will be written with CAPS severity levels | |
119 | BOOST_LOG_SEV(lg, normal) << "A normal severity message"; | |
120 | BOOST_LOG_SEV(lg, notification) << "A notification severity message"; | |
121 | BOOST_LOG_SEV(lg, warning) << "A warning severity message"; | |
122 | BOOST_LOG_SEV(lg, error) << "An error severity message"; | |
123 | BOOST_LOG_SEV(lg, critical) << "A critical severity message"; | |
124 | ||
125 | // This line will still use lower-case severity levels | |
126 | std::cout << "The regular output still uses lower-case formatting: " << normal << std::endl; | |
127 | ||
128 | return 0; | |
129 | } |