sources_net_connection.cpp 4.1 KB

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