sources_net_connection_chan.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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/log/core.hpp>
  13. #include <boost/log/expressions.hpp>
  14. #include <boost/log/attributes/constant.hpp>
  15. #include <boost/log/attributes/scoped_attribute.hpp>
  16. #include <boost/log/sources/channel_logger.hpp>
  17. #include <boost/log/sources/record_ostream.hpp>
  18. #include <boost/log/sinks/sync_frontend.hpp>
  19. #include <boost/log/sinks/text_ostream_backend.hpp>
  20. #include <boost/log/utility/setup/common_attributes.hpp>
  21. #include <boost/log/utility/manipulators/add_value.hpp>
  22. namespace logging = boost::log;
  23. namespace src = boost::log::sources;
  24. namespace expr = boost::log::expressions;
  25. namespace sinks = boost::log::sinks;
  26. namespace attrs = boost::log::attributes;
  27. namespace keywords = boost::log::keywords;
  28. BOOST_LOG_ATTRIBUTE_KEYWORD(line_id, "LineID", unsigned int)
  29. BOOST_LOG_ATTRIBUTE_KEYWORD(channel, "Channel", std::string)
  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_channels
  34. class network_connection
  35. {
  36. src::channel_logger< > m_net, m_stat;
  37. logging::attribute_set::iterator m_net_remote_addr, m_stat_remote_addr;
  38. public:
  39. network_connection() :
  40. // We can dump network-related messages through this logger
  41. // and be able to filter them later
  42. m_net(keywords::channel = "net"),
  43. // We also can separate statistic records in a different channel
  44. // in order to route them to a different sink
  45. m_stat(keywords::channel = "stat")
  46. {
  47. }
  48. void on_connected(std::string const& remote_addr)
  49. {
  50. // Add the remote address to both channels
  51. attrs::constant< std::string > addr(remote_addr);
  52. m_net_remote_addr = m_net.add_attribute("RemoteAddress", addr).first;
  53. m_stat_remote_addr = m_stat.add_attribute("RemoteAddress", addr).first;
  54. // Put message to the "net" channel
  55. BOOST_LOG(m_net) << "Connection established";
  56. }
  57. void on_disconnected()
  58. {
  59. // Put message to the "net" channel
  60. BOOST_LOG(m_net) << "Connection shut down";
  61. // Remove the attribute with the remote address
  62. m_net.remove_attribute(m_net_remote_addr);
  63. m_stat.remove_attribute(m_stat_remote_addr);
  64. }
  65. void on_data_received(std::size_t size)
  66. {
  67. BOOST_LOG(m_stat) << logging::add_value("ReceivedSize", size) << "Some data received";
  68. }
  69. void on_data_sent(std::size_t size)
  70. {
  71. BOOST_LOG(m_stat) << logging::add_value("SentSize", size) << "Some data sent";
  72. }
  73. };
  74. //]
  75. int main(int, char*[])
  76. {
  77. // Construct the sink for the "net" channel
  78. typedef sinks::synchronous_sink< sinks::text_ostream_backend > text_sink;
  79. boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >();
  80. sink->locked_backend()->add_stream(
  81. boost::make_shared< std::ofstream >("net.log"));
  82. sink->set_formatter
  83. (
  84. expr::stream << line_id << ": [" << remote_address << "] " << expr::smessage
  85. );
  86. sink->set_filter(channel == "net");
  87. logging::core::get()->add_sink(sink);
  88. // Construct the sink for the "stat" channel
  89. sink = boost::make_shared< text_sink >();
  90. sink->locked_backend()->add_stream(
  91. boost::make_shared< std::ofstream >("stat.log"));
  92. sink->set_formatter
  93. (
  94. expr::stream
  95. << remote_address
  96. << expr::if_(expr::has_attr(received_size))
  97. [
  98. expr::stream << " -> " << received_size << " bytes: "
  99. ]
  100. << expr::if_(expr::has_attr(sent_size))
  101. [
  102. expr::stream << " <- " << sent_size << " bytes: "
  103. ]
  104. << expr::smessage
  105. );
  106. sink->set_filter(channel == "stat");
  107. logging::core::get()->add_sink(sink);
  108. // Register other common attributes, such as time stamp and record counter
  109. logging::add_common_attributes();
  110. // Emulate network activity
  111. network_connection conn;
  112. conn.on_connected("11.22.33.44");
  113. conn.on_data_received(123);
  114. conn.on_data_sent(321);
  115. conn.on_disconnected();
  116. return 0;
  117. }