]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/log/example/doc/tutorial_fmt_custom.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / log / example / doc / tutorial_fmt_custom.cpp
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 <ostream>
9 #include <fstream>
10 #include <boost/smart_ptr/shared_ptr.hpp>
11 #include <boost/smart_ptr/make_shared_object.hpp>
12 #include <boost/optional.hpp>
13 #include <boost/log/core.hpp>
14 #include <boost/log/trivial.hpp>
15 #include <boost/log/expressions.hpp>
16 #include <boost/log/sinks/sync_frontend.hpp>
17 #include <boost/log/sinks/text_ostream_backend.hpp>
18 #include <boost/log/sources/severity_logger.hpp>
19 #include <boost/log/sources/record_ostream.hpp>
20 #include <boost/log/utility/formatting_ostream.hpp>
21 #include <boost/log/utility/setup/common_attributes.hpp>
22 #include <boost/log/attributes/value_extraction.hpp>
23
24 namespace logging = boost::log;
25 namespace src = boost::log::sources;
26 namespace expr = boost::log::expressions;
27 namespace sinks = boost::log::sinks;
28
29 //[ example_tutorial_formatters_custom
30 void my_formatter(logging::record_view const& rec, logging::formatting_ostream& strm)
31 {
32 // Get the LineID attribute value and put it into the stream
33 strm << logging::extract< unsigned int >("LineID", rec) << ": ";
34
35 // The same for the severity level.
36 // The simplified syntax is possible if attribute keywords are used.
37 strm << "<" << rec[logging::trivial::severity] << "> ";
38
39 // Finally, put the record message to the stream
40 strm << rec[expr::smessage];
41 }
42
43 void init()
44 {
45 typedef sinks::synchronous_sink< sinks::text_ostream_backend > text_sink;
46 boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >();
47
48 sink->locked_backend()->add_stream(
49 boost::make_shared< std::ofstream >("sample.log"));
50
51 sink->set_formatter(&my_formatter);
52
53 logging::core::get()->add_sink(sink);
54 }
55 //]
56
57 int main(int, char*[])
58 {
59 init();
60 logging::add_common_attributes();
61
62 using namespace logging::trivial;
63 src::severity_logger< severity_level > lg;
64
65 BOOST_LOG_SEV(lg, trace) << "A trace severity message";
66 BOOST_LOG_SEV(lg, debug) << "A debug severity message";
67 BOOST_LOG_SEV(lg, info) << "An informational severity message";
68 BOOST_LOG_SEV(lg, warning) << "A warning severity message";
69 BOOST_LOG_SEV(lg, error) << "An error severity message";
70 BOOST_LOG_SEV(lg, fatal) << "A fatal severity message";
71
72 return 0;
73 }