]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/log/example/doc/sources_severity.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)
12 #include <boost/move/utility_core.hpp>
13 #include <boost/smart_ptr/shared_ptr.hpp>
14 #include <boost/smart_ptr/make_shared_object.hpp>
15 #include <boost/log/core.hpp>
16 #include <boost/log/expressions.hpp>
17 #include <boost/log/sources/severity_logger.hpp>
18 #include <boost/log/sources/record_ostream.hpp>
19 #include <boost/log/sinks/sync_frontend.hpp>
20 #include <boost/log/sinks/text_ostream_backend.hpp>
21 #include <boost/log/utility/setup/common_attributes.hpp>
23 namespace logging
= boost::log
;
24 namespace src
= boost::log::sources
;
25 namespace expr
= boost::log::expressions
;
26 namespace sinks
= boost::log::sinks
;
27 namespace attrs
= boost::log::attributes
;
28 namespace keywords
= boost::log::keywords
;
30 //[ example_sources_severity
31 // We define our own severity levels
41 void logging_function()
43 // The logger implicitly adds a source-specific attribute 'Severity'
44 // of type 'severity_level' on construction
45 src::severity_logger
< severity_level
> slg
;
47 BOOST_LOG_SEV(slg
, normal
) << "A regular message";
48 BOOST_LOG_SEV(slg
, warning
) << "Something bad is going on but I can handle it";
49 BOOST_LOG_SEV(slg
, critical
) << "Everything crumbles, shoot me now!";
53 //[ example_sources_default_severity
54 void default_severity()
56 // The default severity can be specified in constructor.
57 src::severity_logger
< severity_level
> error_lg(keywords::severity
= error
);
59 BOOST_LOG(error_lg
) << "An error level log record (by default)";
61 // The explicitly specified level overrides the default
62 BOOST_LOG_SEV(error_lg
, warning
) << "A warning level log record (overrode the default)";
66 //[ example_sources_severity_manual
69 src::severity_logger
< severity_level
> slg
;
71 logging::record rec
= slg
.open_record(keywords::severity
= normal
);
74 logging::record_ostream
strm(rec
);
75 strm
<< "A regular message";
77 slg
.push_record(boost::move(rec
));
82 // The operator puts a human-friendly representation of the severity level to the stream
83 std::ostream
& operator<< (std::ostream
& strm
, severity_level level
)
85 static const char* strings
[] =
94 if (static_cast< std::size_t >(level
) < sizeof(strings
) / sizeof(*strings
))
95 strm
<< strings
[level
];
97 strm
<< static_cast< int >(level
);
104 typedef sinks::synchronous_sink
< sinks::text_ostream_backend
> text_sink
;
105 boost::shared_ptr
< text_sink
> sink
= boost::make_shared
< text_sink
>();
107 sink
->locked_backend()->add_stream(
108 boost::make_shared
< std::ofstream
>("sample.log"));
113 << expr::attr
< unsigned int >("LineID")
114 << ": <" << expr::attr
< severity_level
>("Severity")
119 logging::core::get()->add_sink(sink
);
122 logging::add_common_attributes();
125 int main(int, char*[])