expressions_has_attr_stat_accum.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 <map>
  10. #include <iostream>
  11. #include <fstream>
  12. #include <boost/smart_ptr/shared_ptr.hpp>
  13. #include <boost/smart_ptr/make_shared_object.hpp>
  14. #include <boost/log/sinks.hpp>
  15. #include <boost/log/expressions.hpp>
  16. #include <boost/log/attributes/scoped_attribute.hpp>
  17. #include <boost/log/sources/logger.hpp>
  18. #include <boost/log/sources/record_ostream.hpp>
  19. #include <boost/log/utility/value_ref.hpp>
  20. #include <boost/log/utility/manipulators/add_value.hpp>
  21. namespace logging = boost::log;
  22. namespace src = boost::log::sources;
  23. namespace expr = boost::log::expressions;
  24. namespace sinks = boost::log::sinks;
  25. namespace keywords = boost::log::keywords;
  26. //[ example_expressions_has_attr_stat_accumulator
  27. // Declare attribute keywords
  28. BOOST_LOG_ATTRIBUTE_KEYWORD(stat_stream, "StatisticStream", std::string)
  29. BOOST_LOG_ATTRIBUTE_KEYWORD(change, "Change", int)
  30. // A simple sink backend to accumulate statistic information
  31. class my_stat_accumulator :
  32. public sinks::basic_sink_backend< sinks::synchronized_feeding >
  33. {
  34. // A map of accumulated statistic values,
  35. // ordered by the statistic information stream name
  36. typedef std::map< std::string, int > stat_info_map;
  37. stat_info_map m_stat_info;
  38. public:
  39. // Destructor
  40. ~my_stat_accumulator()
  41. {
  42. // Display the accumulated data
  43. stat_info_map::const_iterator it = m_stat_info.begin(), end = m_stat_info.end();
  44. for (; it != end; ++it)
  45. {
  46. std::cout << "Statistic stream: " << it->first
  47. << ", accumulated value: " << it->second << "\n";
  48. }
  49. std::cout.flush();
  50. }
  51. // The method is called for every log record being put into the sink backend
  52. void consume(logging::record_view const& rec)
  53. {
  54. // First, acquire statistic information stream name
  55. logging::value_ref< std::string, tag::stat_stream > name = rec[stat_stream];
  56. if (name)
  57. {
  58. // Next, get the statistic value change
  59. logging::value_ref< int, tag::change > change_amount = rec[change];
  60. if (change_amount)
  61. {
  62. // Accumulate the statistic data
  63. m_stat_info[name.get()] += change_amount.get();
  64. }
  65. }
  66. }
  67. };
  68. // The function registers two sinks - one for statistic information,
  69. // and another one for other records
  70. void init()
  71. {
  72. boost::shared_ptr< logging::core > core = logging::core::get();
  73. // Create a backend and attach a stream to it
  74. boost::shared_ptr< sinks::text_ostream_backend > backend =
  75. boost::make_shared< sinks::text_ostream_backend >();
  76. backend->add_stream(
  77. boost::shared_ptr< std::ostream >(new std::ofstream("test.log")));
  78. // Create a frontend and setup filtering
  79. typedef sinks::synchronous_sink< sinks::text_ostream_backend > log_sink_type;
  80. boost::shared_ptr< log_sink_type > log_sink(new log_sink_type(backend));
  81. // All records that don't have a "StatisticStream" attribute attached
  82. // will go to the "test.log" file
  83. log_sink->set_filter(!expr::has_attr(stat_stream));
  84. core->add_sink(log_sink);
  85. // Create another sink that will receive all statistic data
  86. typedef sinks::synchronous_sink< my_stat_accumulator > stat_sink_type;
  87. boost::shared_ptr< stat_sink_type > stat_sink(new stat_sink_type());
  88. // All records with a "StatisticStream" string attribute attached
  89. // will go to the my_stat_accumulator sink
  90. stat_sink->set_filter(expr::has_attr(stat_stream));
  91. core->add_sink(stat_sink);
  92. }
  93. // This simple macro will simplify putting statistic data into a logger
  94. #define PUT_STAT(lg, stat_stream_name, change)\
  95. if (true) {\
  96. BOOST_LOG_SCOPED_LOGGER_TAG(lg, "StatisticStream", stat_stream_name);\
  97. BOOST_LOG(lg) << logging::add_value("Change", (int)(change));\
  98. } else ((void)0)
  99. void logging_function()
  100. {
  101. src::logger lg;
  102. // Put a regular log record, it will go to the "test.log" file
  103. BOOST_LOG(lg) << "A regular log record";
  104. // Put some statistic data
  105. PUT_STAT(lg, "StreamOne", 10);
  106. PUT_STAT(lg, "StreamTwo", 20);
  107. PUT_STAT(lg, "StreamOne", -5);
  108. }
  109. //]
  110. int main(int, char*[])
  111. {
  112. init();
  113. logging_function();
  114. return 0;
  115. }