• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <fstream>
9 #include <iomanip>
10 #include <boost/smart_ptr/shared_ptr.hpp>
11 #include <boost/smart_ptr/make_shared_object.hpp>
12 #include <boost/log/core.hpp>
13 #include <boost/log/trivial.hpp>
14 #include <boost/log/expressions.hpp>
15 #include <boost/log/sinks/sync_frontend.hpp>
16 #include <boost/log/sinks/text_ostream_backend.hpp>
17 #include <boost/log/sources/severity_logger.hpp>
18 #include <boost/log/sources/record_ostream.hpp>
19 #include <boost/log/utility/setup/common_attributes.hpp>
20 
21 namespace logging = boost::log;
22 namespace src = boost::log::sources;
23 namespace expr = boost::log::expressions;
24 namespace sinks = boost::log::sinks;
25 namespace keywords = boost::log::keywords;
26 
27 //[ example_tutorial_formatters_stream_manual
init()28 void init()
29 {
30     typedef sinks::synchronous_sink< sinks::text_ostream_backend > text_sink;
31     boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >();
32 
33     sink->locked_backend()->add_stream(
34         boost::make_shared< std::ofstream >("sample.log"));
35 
36     sink->set_formatter
37     (
38         expr::stream
39                // line id will be written in hex, 8-digits, zero-filled
40             << std::hex << std::setw(8) << std::setfill('0') << expr::attr< unsigned int >("LineID")
41             << ": <" << logging::trivial::severity
42             << "> " << expr::smessage
43     );
44 
45     logging::core::get()->add_sink(sink);
46 }
47 //]
48 
main(int,char * [])49 int main(int, char*[])
50 {
51     init();
52     logging::add_common_attributes();
53 
54     using namespace logging::trivial;
55     src::severity_logger< severity_level > lg;
56 
57     BOOST_LOG_SEV(lg, trace) << "A trace severity message";
58     BOOST_LOG_SEV(lg, debug) << "A debug severity message";
59     BOOST_LOG_SEV(lg, info) << "An informational severity message";
60     BOOST_LOG_SEV(lg, warning) << "A warning severity message";
61     BOOST_LOG_SEV(lg, error) << "An error severity message";
62     BOOST_LOG_SEV(lg, fatal) << "A fatal severity message";
63 
64     return 0;
65 }
66