extension_filter_parser.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 <string>
  8. #include <iostream>
  9. #include <stdexcept>
  10. #include <boost/smart_ptr/shared_ptr.hpp>
  11. #include <boost/smart_ptr/make_shared_object.hpp>
  12. #include <boost/lexical_cast.hpp>
  13. #include <boost/phoenix.hpp>
  14. #include <boost/log/core.hpp>
  15. #include <boost/log/expressions.hpp>
  16. #include <boost/log/attributes/attribute_name.hpp>
  17. #include <boost/log/attributes/scoped_attribute.hpp>
  18. #include <boost/log/sources/logger.hpp>
  19. #include <boost/log/sources/record_ostream.hpp>
  20. #include <boost/log/utility/value_ref.hpp>
  21. #include <boost/log/utility/formatting_ostream.hpp>
  22. #include <boost/log/utility/manipulators/add_value.hpp>
  23. #include <boost/log/utility/setup/filter_parser.hpp>
  24. #include <boost/log/utility/setup/common_attributes.hpp>
  25. #include <boost/log/utility/setup/console.hpp>
  26. namespace logging = boost::log;
  27. namespace attrs = boost::log::attributes;
  28. namespace src = boost::log::sources;
  29. namespace expr = boost::log::expressions;
  30. namespace sinks = boost::log::sinks;
  31. namespace keywords = boost::log::keywords;
  32. //[ example_extension_filter_parser_point_definition
  33. struct point
  34. {
  35. float m_x, m_y;
  36. point() : m_x(0.0f), m_y(0.0f) {}
  37. point(float x, float y) : m_x(x), m_y(y) {}
  38. };
  39. bool operator== (point const& left, point const& right);
  40. bool operator!= (point const& left, point const& right);
  41. template< typename CharT, typename TraitsT >
  42. std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, point const& p);
  43. template< typename CharT, typename TraitsT >
  44. std::basic_istream< CharT, TraitsT >& operator>> (std::basic_istream< CharT, TraitsT >& strm, point& p);
  45. //]
  46. const float epsilon = 0.0001f;
  47. bool operator== (point const& left, point const& right)
  48. {
  49. return (left.m_x - epsilon <= right.m_x && left.m_x + epsilon >= right.m_x) &&
  50. (left.m_y - epsilon <= right.m_y && left.m_y + epsilon >= right.m_y);
  51. }
  52. bool operator!= (point const& left, point const& right)
  53. {
  54. return !(left == right);
  55. }
  56. template< typename CharT, typename TraitsT >
  57. std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, point const& p)
  58. {
  59. if (strm.good())
  60. strm << "(" << p.m_x << ", " << p.m_y << ")";
  61. return strm;
  62. }
  63. template< typename CharT, typename TraitsT >
  64. std::basic_istream< CharT, TraitsT >& operator>> (std::basic_istream< CharT, TraitsT >& strm, point& p)
  65. {
  66. if (strm.good())
  67. {
  68. CharT left_brace = static_cast< CharT >(0), comma = static_cast< CharT >(0), right_brace = static_cast< CharT >(0);
  69. strm.setf(std::ios_base::skipws);
  70. strm >> left_brace >> p.m_x >> comma >> p.m_y >> right_brace;
  71. if (left_brace != '(' || comma != ',' || right_brace != ')')
  72. strm.setstate(std::ios_base::failbit);
  73. }
  74. return strm;
  75. }
  76. #if 0
  77. //[ example_extension_simple_filter_factory
  78. void init_factories()
  79. {
  80. //<-
  81. logging::register_simple_formatter_factory< point, char >("Coordinates");
  82. //->
  83. logging::register_simple_filter_factory< point, char >("Coordinates");
  84. }
  85. //]
  86. #endif
  87. //[ example_extension_custom_filter_factory
  88. // Custom point filter factory
  89. class point_filter_factory :
  90. public logging::filter_factory< char >
  91. {
  92. public:
  93. logging::filter on_exists_test(logging::attribute_name const& name)
  94. {
  95. return expr::has_attr< point >(name);
  96. }
  97. logging::filter on_equality_relation(logging::attribute_name const& name, string_type const& arg)
  98. {
  99. return expr::attr< point >(name) == boost::lexical_cast< point >(arg);
  100. }
  101. logging::filter on_inequality_relation(logging::attribute_name const& name, string_type const& arg)
  102. {
  103. return expr::attr< point >(name) != boost::lexical_cast< point >(arg);
  104. }
  105. };
  106. void init_factories()
  107. {
  108. //<-
  109. logging::register_simple_formatter_factory< point, char >("Coordinates");
  110. //->
  111. logging::register_filter_factory("Coordinates", boost::make_shared< point_filter_factory >());
  112. }
  113. //]
  114. void init_logging()
  115. {
  116. init_factories();
  117. logging::add_console_log
  118. (
  119. std::clog,
  120. keywords::filter = "%Coordinates% = \"(10, 10)\"",
  121. keywords::format = "%TimeStamp% %Coordinates% %Message%"
  122. );
  123. logging::add_common_attributes();
  124. }
  125. int main(int, char*[])
  126. {
  127. init_logging();
  128. src::logger lg;
  129. // We have to use scoped attributes in order coordinates to be passed to filters
  130. {
  131. BOOST_LOG_SCOPED_LOGGER_TAG(lg, "Coordinates", point(10, 10));
  132. BOOST_LOG(lg) << "Hello, world with coordinates (10, 10)!";
  133. }
  134. {
  135. BOOST_LOG_SCOPED_LOGGER_TAG(lg, "Coordinates", point(20, 20));
  136. BOOST_LOG(lg) << "Hello, world with coordinates (20, 20)!"; // this message will be suppressed by filter
  137. }
  138. return 0;
  139. }