]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/log/example/doc/tutorial_fmt_custom.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/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>
24 namespace logging
= boost::log
;
25 namespace src
= boost::log::sources
;
26 namespace expr
= boost::log::expressions
;
27 namespace sinks
= boost::log::sinks
;
29 //[ example_tutorial_formatters_custom
30 void my_formatter(logging::record_view
const& rec
, logging::formatting_ostream
& strm
)
32 // Get the LineID attribute value and put it into the stream
33 strm
<< logging::extract
< unsigned int >("LineID", rec
) << ": ";
35 // The same for the severity level.
36 // The simplified syntax is possible if attribute keywords are used.
37 strm
<< "<" << rec
[logging::trivial::severity
] << "> ";
39 // Finally, put the record message to the stream
40 strm
<< rec
[expr::smessage
];
45 typedef sinks::synchronous_sink
< sinks::text_ostream_backend
> text_sink
;
46 boost::shared_ptr
< text_sink
> sink
= boost::make_shared
< text_sink
>();
48 sink
->locked_backend()->add_stream(
49 boost::make_shared
< std::ofstream
>("sample.log"));
51 sink
->set_formatter(&my_formatter
);
53 logging::core::get()->add_sink(sink
);
57 int main(int, char*[])
60 logging::add_common_attributes();
62 using namespace logging::trivial
;
63 src::severity_logger
< severity_level
> lg
;
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";