]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 <string> | |
9 | #include <iostream> | |
10 | #include <boost/smart_ptr/shared_ptr.hpp> | |
11 | #include <boost/log/core.hpp> | |
12 | #include <boost/log/expressions.hpp> | |
13 | #include <boost/log/sinks/unlocked_frontend.hpp> | |
14 | #include <boost/log/sinks/basic_sink_backend.hpp> | |
15 | #include <boost/log/sinks/frontend_requirements.hpp> | |
16 | #include <boost/log/sources/severity_channel_logger.hpp> | |
17 | #include <boost/log/sources/record_ostream.hpp> | |
18 | ||
19 | namespace logging = boost::log; | |
20 | namespace src = boost::log::sources; | |
21 | namespace expr = boost::log::expressions; | |
22 | namespace sinks = boost::log::sinks; | |
23 | namespace keywords = boost::log::keywords; | |
24 | ||
25 | //[ example_sinks_unlocked | |
26 | enum severity_level | |
27 | { | |
28 | normal, | |
29 | warning, | |
30 | error | |
31 | }; | |
32 | ||
33 | // A trivial sink backend that requires no thread synchronization | |
34 | class my_backend : | |
35 | public sinks::basic_sink_backend< sinks::concurrent_feeding > | |
36 | { | |
37 | public: | |
38 | // The function is called for every log record to be written to log | |
39 | void consume(logging::record_view const& rec) | |
40 | { | |
41 | // We skip the actual synchronization code for brevity | |
42 | std::cout << rec[expr::smessage] << std::endl; | |
43 | } | |
44 | }; | |
45 | ||
46 | // Complete sink type | |
47 | typedef sinks::unlocked_sink< my_backend > sink_t; | |
48 | ||
49 | void init_logging() | |
50 | { | |
51 | boost::shared_ptr< logging::core > core = logging::core::get(); | |
52 | ||
53 | // The simplest way, the backend is default-constructed | |
54 | boost::shared_ptr< sink_t > sink1(new sink_t()); | |
55 | core->add_sink(sink1); | |
56 | ||
57 | // One can construct backend separately and pass it to the frontend | |
58 | boost::shared_ptr< my_backend > backend(new my_backend()); | |
59 | boost::shared_ptr< sink_t > sink2(new sink_t(backend)); | |
60 | core->add_sink(sink2); | |
61 | ||
62 | // You can manage filtering through the sink interface | |
63 | sink1->set_filter(expr::attr< severity_level >("Severity") >= warning); | |
64 | sink2->set_filter(expr::attr< std::string >("Channel") == "net"); | |
65 | } | |
66 | //] | |
67 | ||
68 | int main(int, char*[]) | |
69 | { | |
70 | init_logging(); | |
71 | ||
72 | src::severity_channel_logger< severity_level > lg(keywords::channel = "net"); | |
73 | BOOST_LOG_SEV(lg, normal) << "Hello world!"; | |
74 | ||
75 | return 0; | |
76 | } |