]>
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_attr_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 | // The operator is used for regular stream formatting | |
35 | std::ostream& operator<< (std::ostream& strm, severity_level level) | |
36 | { | |
37 | static const char* strings[] = | |
38 | { | |
39 | "normal", | |
40 | "notification", | |
41 | "warning", | |
42 | "error", | |
43 | "critical" | |
44 | }; | |
45 | ||
46 | if (static_cast< std::size_t >(level) < sizeof(strings) / sizeof(*strings)) | |
47 | strm << strings[level]; | |
48 | else | |
49 | strm << static_cast< int >(level); | |
50 | ||
51 | return strm; | |
52 | } | |
53 | ||
54 | // Attribute value tag type | |
55 | struct severity_tag; | |
56 | ||
57 | // The operator is used when putting the severity level to log | |
58 | logging::formatting_ostream& operator<< | |
59 | ( | |
60 | logging::formatting_ostream& strm, | |
61 | logging::to_log_manip< severity_level, severity_tag > const& manip | |
62 | ) | |
63 | { | |
64 | static const char* strings[] = | |
65 | { | |
66 | "NORM", | |
67 | "NTFY", | |
68 | "WARN", | |
69 | "ERRR", | |
70 | "CRIT" | |
71 | }; | |
72 | ||
73 | severity_level level = manip.get(); | |
74 | if (static_cast< std::size_t >(level) < sizeof(strings) / sizeof(*strings)) | |
75 | strm << strings[level]; | |
76 | else | |
77 | strm << static_cast< int >(level); | |
78 | ||
79 | return strm; | |
80 | } | |
81 | ||
82 | void init() | |
83 | { | |
84 | logging::add_console_log | |
85 | ( | |
86 | std::clog, | |
87 | // This makes the sink to write log records that look like this: | |
88 | // 1: <NORM> A normal severity message | |
89 | // 2: <ERRR> An error severity message | |
90 | keywords::format = | |
91 | ( | |
92 | expr::stream | |
93 | << expr::attr< unsigned int >("LineID") | |
94 | << ": <" << expr::attr< severity_level, severity_tag >("Severity") | |
95 | << "> " << expr::smessage | |
96 | ) | |
97 | ); | |
98 | } | |
99 | //] | |
100 | ||
101 | int main(int, char*[]) | |
102 | { | |
103 | init(); | |
104 | logging::add_common_attributes(); | |
105 | ||
106 | src::severity_logger< severity_level > lg; | |
107 | ||
108 | // These messages will be written with CAPS severity levels | |
109 | BOOST_LOG_SEV(lg, normal) << "A normal severity message"; | |
110 | BOOST_LOG_SEV(lg, notification) << "A notification severity message"; | |
111 | BOOST_LOG_SEV(lg, warning) << "A warning severity message"; | |
112 | BOOST_LOG_SEV(lg, error) << "An error severity message"; | |
113 | BOOST_LOG_SEV(lg, critical) << "A critical severity message"; | |
114 | ||
115 | // This line will still use lower-case severity levels | |
116 | std::cout << "The regular output still uses lower-case formatting: " << normal << std::endl; | |
117 | ||
118 | return 0; | |
119 | } |