]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/log/example/doc/expressions_keyword_fmt_tag.cpp
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_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>
18 namespace logging
= boost::log
;
19 namespace src
= boost::log::sources
;
20 namespace expr
= boost::log::expressions
;
21 namespace keywords
= boost::log::keywords
;
23 //[ example_expressions_keyword_formatter_stream_tag
24 // We define our own severity levels
34 // Define the attribute keywords
35 BOOST_LOG_ATTRIBUTE_KEYWORD(line_id
, "LineID", unsigned int)
36 BOOST_LOG_ATTRIBUTE_KEYWORD(severity
, "Severity", severity_level
)
38 // The operator is used for regular stream formatting
39 std::ostream
& operator<< (std::ostream
& strm
, severity_level level
)
41 static const char* strings
[] =
50 if (static_cast< std::size_t >(level
) < sizeof(strings
) / sizeof(*strings
))
51 strm
<< strings
[level
];
53 strm
<< static_cast< int >(level
);
58 // The operator is used when putting the severity level to log
59 logging::formatting_ostream
& operator<<
61 logging::formatting_ostream
& strm
,
62 logging::to_log_manip
< severity_level
, tag::severity
> const& manip
65 static const char* strings
[] =
74 severity_level level
= manip
.get();
75 if (static_cast< std::size_t >(level
) < sizeof(strings
) / sizeof(*strings
))
76 strm
<< strings
[level
];
78 strm
<< static_cast< int >(level
);
85 logging::add_console_log
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
96 << "> " << expr::smessage
102 //[ example_expressions_keyword_lookup
103 void print_severity(logging::record_view
const& rec
)
105 logging::value_ref
< severity_level
, tag::severity
> level
= rec
[severity
];
106 std::cout
<< level
<< std::endl
;
111 int main(int, char*[])
114 logging::add_common_attributes();
116 src::severity_logger
< severity_level
> lg
;
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";
125 // This line will still use lower-case severity levels
126 std::cout
<< "The regular output still uses lower-case formatting: " << normal
<< std::endl
;