• 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 <cstddef>
9 #include <string>
10 #include <fstream>
11 #include <boost/smart_ptr/shared_ptr.hpp>
12 #include <boost/smart_ptr/make_shared_object.hpp>
13 #include <boost/log/core.hpp>
14 #include <boost/log/expressions.hpp>
15 #include <boost/log/attributes/constant.hpp>
16 #include <boost/log/attributes/scoped_attribute.hpp>
17 #include <boost/log/sources/channel_logger.hpp>
18 #include <boost/log/sources/record_ostream.hpp>
19 #include <boost/log/sources/global_logger_storage.hpp>
20 #include <boost/log/sinks/sync_frontend.hpp>
21 #include <boost/log/sinks/text_ostream_backend.hpp>
22 #include <boost/log/utility/setup/common_attributes.hpp>
23 #include <boost/log/utility/manipulators/add_value.hpp>
24 
25 namespace logging = boost::log;
26 namespace src = boost::log::sources;
27 namespace expr = boost::log::expressions;
28 namespace sinks = boost::log::sinks;
29 namespace attrs = boost::log::attributes;
30 namespace keywords = boost::log::keywords;
31 
32 BOOST_LOG_ATTRIBUTE_KEYWORD(line_id, "LineID", unsigned int)
33 BOOST_LOG_ATTRIBUTE_KEYWORD(channel, "Channel", std::string)
34 BOOST_LOG_ATTRIBUTE_KEYWORD(remote_address, "RemoteAddress", std::string)
35 BOOST_LOG_ATTRIBUTE_KEYWORD(received_size, "ReceivedSize", std::size_t)
36 BOOST_LOG_ATTRIBUTE_KEYWORD(sent_size, "SentSize", std::size_t)
37 
38 //[ example_sources_network_connection_dynamic_channels
39 // Define a global logger
40 BOOST_LOG_INLINE_GLOBAL_LOGGER_CTOR_ARGS(my_logger, src::channel_logger_mt< >, (keywords::channel = "general"))
41 
42 class network_connection
43 {
44     std::string m_remote_addr;
45 
46 public:
on_connected(std::string const & remote_addr)47     void on_connected(std::string const& remote_addr)
48     {
49         m_remote_addr = remote_addr;
50 
51         // Put message to the "net" channel
52         BOOST_LOG_CHANNEL(my_logger::get(), "net")
53             << logging::add_value("RemoteAddress", m_remote_addr)
54             << "Connection established";
55     }
56 
on_disconnected()57     void on_disconnected()
58     {
59         // Put message to the "net" channel
60         BOOST_LOG_CHANNEL(my_logger::get(), "net")
61             << logging::add_value("RemoteAddress", m_remote_addr)
62             << "Connection shut down";
63 
64         m_remote_addr.clear();
65     }
66 
on_data_received(std::size_t size)67     void on_data_received(std::size_t size)
68     {
69         BOOST_LOG_CHANNEL(my_logger::get(), "stat")
70             << logging::add_value("RemoteAddress", m_remote_addr)
71             << logging::add_value("ReceivedSize", size)
72             << "Some data received";
73     }
74 
on_data_sent(std::size_t size)75     void on_data_sent(std::size_t size)
76     {
77         BOOST_LOG_CHANNEL(my_logger::get(), "stat")
78             << logging::add_value("RemoteAddress", m_remote_addr)
79             << logging::add_value("SentSize", size)
80             << "Some data sent";
81     }
82 };
83 //]
84 
main(int,char * [])85 int main(int, char*[])
86 {
87     // Construct the sink for the "net" channel
88     typedef sinks::synchronous_sink< sinks::text_ostream_backend > text_sink;
89     boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >();
90 
91     sink->locked_backend()->add_stream(
92         boost::make_shared< std::ofstream >("net.log"));
93 
94     sink->set_formatter
95     (
96         expr::stream << line_id << ": [" << remote_address << "] " << expr::smessage
97     );
98 
99     sink->set_filter(channel == "net");
100 
101     logging::core::get()->add_sink(sink);
102 
103     // Construct the sink for the "stat" channel
104     sink = boost::make_shared< text_sink >();
105 
106     sink->locked_backend()->add_stream(
107         boost::make_shared< std::ofstream >("stat.log"));
108 
109     sink->set_formatter
110     (
111         expr::stream
112             << remote_address
113             << expr::if_(expr::has_attr(received_size))
114                [
115                     expr::stream << " -> " << received_size << " bytes: "
116                ]
117             << expr::if_(expr::has_attr(sent_size))
118                [
119                     expr::stream << " <- " << sent_size << " bytes: "
120                ]
121             << expr::smessage
122     );
123 
124     sink->set_filter(channel == "stat");
125 
126     logging::core::get()->add_sink(sink);
127 
128     // Register other common attributes, such as time stamp and record counter
129     logging::add_common_attributes();
130 
131     // Emulate network activity
132     network_connection conn;
133 
134     conn.on_connected("11.22.33.44");
135     conn.on_data_received(123);
136     conn.on_data_sent(321);
137     conn.on_disconnected();
138 
139     return 0;
140 }
141