2 * Copyright Andrey Semashev 2007 - 2014.
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)
11 #include <boost/smart_ptr/shared_ptr.hpp>
12 #include <boost/smart_ptr/make_shared_object.hpp>
13 #include <boost/move/utility.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>
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;
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)
37 //[ example_sources_network_connection
38 class network_connection
41 logging::attribute_set::iterator m_remote_addr;
44 void on_connected(std::string const& remote_addr)
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;
51 // The straightforward way of logging
52 if (logging::record rec = m_logger.open_record())
54 rec.attribute_values().insert("Message",
55 attrs::make_attribute_value(std::string("Connection established")));
56 m_logger.push_record(boost::move(rec));
59 void on_disconnected()
61 // The simpler way of logging: the above "if" condition is wrapped into a neat macro
62 BOOST_LOG(m_logger) << "Connection shut down";
64 // Remove the attribute with the remote address
65 m_logger.remove_attribute(m_remote_addr);
67 void on_data_received(std::size_t size)
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";
74 void on_data_sent(std::size_t size)
76 BOOST_LOG(m_logger) << logging::add_value("SentSize", size) << "Some data sent";
81 int main(int, char*[])
84 typedef sinks::synchronous_sink< sinks::text_ostream_backend > text_sink;
85 boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >();
87 // Add a stream to write log to
88 sink->locked_backend()->add_stream(
89 boost::make_shared< std::ofstream >("sample.log"));
96 << ": [" << remote_address << "] "
97 << expr::if_(expr::has_attr(received_size))
99 expr::stream << "[Received: " << received_size << "] "
101 << expr::if_(expr::has_attr(sent_size))
103 expr::stream << "[Sent: " << sent_size << "] "
108 // Register the sink in the logging core
109 logging::core::get()->add_sink(sink);
111 // Register other common attributes, such as time stamp and record counter
112 logging::add_common_attributes();
114 // Emulate network activity
115 network_connection conn;
117 conn.on_connected("11.22.33.44");
118 conn.on_data_received(123);
119 conn.on_data_sent(321);
120 conn.on_disconnected();