]>
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 <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/move/utility_core.hpp> | |
14 | #include <boost/log/core.hpp> | |
15 | #include <boost/log/expressions.hpp> | |
16 | #include <boost/log/attributes/constant.hpp> | |
17 | #include <boost/log/attributes/scoped_attribute.hpp> | |
18 | #include <boost/log/attributes/attribute_value_impl.hpp> | |
19 | #include <boost/log/sources/logger.hpp> | |
20 | #include <boost/log/sources/record_ostream.hpp> | |
21 | #include <boost/log/sinks/sync_frontend.hpp> | |
22 | #include <boost/log/sinks/text_ostream_backend.hpp> | |
23 | #include <boost/log/utility/setup/common_attributes.hpp> | |
24 | #include <boost/log/utility/manipulators/add_value.hpp> | |
25 | ||
26 | namespace logging = boost::log; | |
27 | namespace src = boost::log::sources; | |
28 | namespace expr = boost::log::expressions; | |
29 | namespace sinks = boost::log::sinks; | |
30 | namespace attrs = boost::log::attributes; | |
31 | ||
32 | BOOST_LOG_ATTRIBUTE_KEYWORD(line_id, "LineID", unsigned int) | |
33 | BOOST_LOG_ATTRIBUTE_KEYWORD(remote_address, "RemoteAddress", std::string) | |
34 | BOOST_LOG_ATTRIBUTE_KEYWORD(received_size, "ReceivedSize", std::size_t) | |
35 | BOOST_LOG_ATTRIBUTE_KEYWORD(sent_size, "SentSize", std::size_t) | |
36 | ||
37 | //[ example_sources_network_connection | |
38 | class network_connection | |
39 | { | |
40 | src::logger m_logger; | |
41 | logging::attribute_set::iterator m_remote_addr; | |
42 | ||
43 | public: | |
44 | void on_connected(std::string const& remote_addr) | |
45 | { | |
46 | // Put the remote address into the logger to automatically attach it | |
47 | // to every log record written through the logger | |
48 | m_remote_addr = m_logger.add_attribute("RemoteAddress", | |
49 | attrs::constant< std::string >(remote_addr)).first; | |
50 | ||
51 | // The straightforward way of logging | |
52 | if (logging::record rec = m_logger.open_record()) | |
53 | { | |
54 | rec.attribute_values().insert("Message", | |
55 | attrs::make_attribute_value(std::string("Connection established"))); | |
56 | m_logger.push_record(boost::move(rec)); | |
57 | } | |
58 | } | |
59 | void on_disconnected() | |
60 | { | |
61 | // The simpler way of logging: the above "if" condition is wrapped into a neat macro | |
62 | BOOST_LOG(m_logger) << "Connection shut down"; | |
63 | ||
64 | // Remove the attribute with the remote address | |
65 | m_logger.remove_attribute(m_remote_addr); | |
66 | } | |
67 | void on_data_received(std::size_t size) | |
68 | { | |
69 | // Put the size as an additional attribute | |
70 | // so it can be collected and accumulated later if needed. | |
71 | // The attribute will be attached only to this log record. | |
72 | BOOST_LOG(m_logger) << logging::add_value("ReceivedSize", size) << "Some data received"; | |
73 | } | |
74 | void on_data_sent(std::size_t size) | |
75 | { | |
76 | BOOST_LOG(m_logger) << logging::add_value("SentSize", size) << "Some data sent"; | |
77 | } | |
78 | }; | |
79 | //] | |
80 | ||
81 | int main(int, char*[]) | |
82 | { | |
83 | // Construct the sink | |
84 | typedef sinks::synchronous_sink< sinks::text_ostream_backend > text_sink; | |
85 | boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >(); | |
86 | ||
87 | // Add a stream to write log to | |
88 | sink->locked_backend()->add_stream( | |
89 | boost::make_shared< std::ofstream >("sample.log")); | |
90 | ||
91 | // Set the formatter | |
92 | sink->set_formatter | |
93 | ( | |
94 | expr::stream | |
95 | << line_id | |
96 | << ": [" << remote_address << "] " | |
97 | << expr::if_(expr::has_attr(received_size)) | |
98 | [ | |
99 | expr::stream << "[Received: " << received_size << "] " | |
100 | ] | |
101 | << expr::if_(expr::has_attr(sent_size)) | |
102 | [ | |
103 | expr::stream << "[Sent: " << sent_size << "] " | |
104 | ] | |
105 | << expr::smessage | |
106 | ); | |
107 | ||
108 | // Register the sink in the logging core | |
109 | logging::core::get()->add_sink(sink); | |
110 | ||
111 | // Register other common attributes, such as time stamp and record counter | |
112 | logging::add_common_attributes(); | |
113 | ||
114 | // Emulate network activity | |
115 | network_connection conn; | |
116 | ||
117 | conn.on_connected("11.22.33.44"); | |
118 | conn.on_data_received(123); | |
119 | conn.on_data_sent(321); | |
120 | conn.on_disconnected(); | |
121 | ||
122 | return 0; | |
123 | } |