extension_filter_parser_custom_rel.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. struct point
  33. {
  34. float m_x, m_y;
  35. point() : m_x(0.0f), m_y(0.0f) {}
  36. point(float x, float y) : m_x(x), m_y(y) {}
  37. };
  38. bool operator== (point const& left, point const& right);
  39. bool operator!= (point const& left, point const& right);
  40. template< typename CharT, typename TraitsT >
  41. std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, point const& p);
  42. template< typename CharT, typename TraitsT >
  43. std::basic_istream< CharT, TraitsT >& operator>> (std::basic_istream< CharT, TraitsT >& strm, point& p);
  44. const float epsilon = 0.0001f;
  45. bool operator== (point const& left, point const& right)
  46. {
  47. return (left.m_x - epsilon <= right.m_x && left.m_x + epsilon >= right.m_x) &&
  48. (left.m_y - epsilon <= right.m_y && left.m_y + epsilon >= right.m_y);
  49. }
  50. bool operator!= (point const& left, point const& right)
  51. {
  52. return !(left == right);
  53. }
  54. template< typename CharT, typename TraitsT >
  55. std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, point const& p)
  56. {
  57. if (strm.good())
  58. strm << "(" << p.m_x << ", " << p.m_y << ")";
  59. return strm;
  60. }
  61. template< typename CharT, typename TraitsT >
  62. std::basic_istream< CharT, TraitsT >& operator>> (std::basic_istream< CharT, TraitsT >& strm, point& p)
  63. {
  64. if (strm.good())
  65. {
  66. CharT left_brace = static_cast< CharT >(0), comma = static_cast< CharT >(0), right_brace = static_cast< CharT >(0);
  67. strm.setf(std::ios_base::skipws);
  68. strm >> left_brace >> p.m_x >> comma >> p.m_y >> right_brace;
  69. if (left_brace != '(' || comma != ',' || right_brace != ')')
  70. strm.setstate(std::ios_base::failbit);
  71. }
  72. return strm;
  73. }
  74. //[ example_extension_filter_parser_rectangle_definition
  75. struct rectangle
  76. {
  77. point m_top_left, m_bottom_right;
  78. };
  79. template< typename CharT, typename TraitsT >
  80. std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, rectangle const& r);
  81. template< typename CharT, typename TraitsT >
  82. std::basic_istream< CharT, TraitsT >& operator>> (std::basic_istream< CharT, TraitsT >& strm, rectangle& r);
  83. //]
  84. template< typename CharT, typename TraitsT >
  85. std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, rectangle const& r)
  86. {
  87. if (strm.good())
  88. strm << "{" << r.m_top_left << " - " << r.m_bottom_right << "}";
  89. return strm;
  90. }
  91. template< typename CharT, typename TraitsT >
  92. std::basic_istream< CharT, TraitsT >& operator>> (std::basic_istream< CharT, TraitsT >& strm, rectangle& r)
  93. {
  94. if (strm.good())
  95. {
  96. CharT left_brace = static_cast< CharT >(0), dash = static_cast< CharT >(0), right_brace = static_cast< CharT >(0);
  97. strm.setf(std::ios_base::skipws);
  98. strm >> left_brace >> r.m_top_left >> dash >> r.m_bottom_right >> right_brace;
  99. if (left_brace != '{' || dash != '-' || right_brace != '}')
  100. strm.setstate(std::ios_base::failbit);
  101. }
  102. return strm;
  103. }
  104. //[ example_extension_custom_filter_factory_with_custom_rel
  105. // The function checks if the point is inside the rectangle
  106. bool is_in_rectangle(logging::value_ref< point > const& p, rectangle const& r)
  107. {
  108. if (p)
  109. {
  110. return p->m_x >= r.m_top_left.m_x && p->m_x <= r.m_bottom_right.m_x &&
  111. p->m_y >= r.m_top_left.m_y && p->m_y <= r.m_bottom_right.m_y;
  112. }
  113. return false;
  114. }
  115. // Custom point filter factory
  116. class point_filter_factory :
  117. public logging::filter_factory< char >
  118. {
  119. public:
  120. logging::filter on_exists_test(logging::attribute_name const& name)
  121. {
  122. return expr::has_attr< point >(name);
  123. }
  124. logging::filter on_equality_relation(logging::attribute_name const& name, string_type const& arg)
  125. {
  126. return expr::attr< point >(name) == boost::lexical_cast< point >(arg);
  127. }
  128. logging::filter on_inequality_relation(logging::attribute_name const& name, string_type const& arg)
  129. {
  130. return expr::attr< point >(name) != boost::lexical_cast< point >(arg);
  131. }
  132. logging::filter on_custom_relation(logging::attribute_name const& name, string_type const& rel, string_type const& arg)
  133. {
  134. if (rel == "is_in_rectangle")
  135. {
  136. return boost::phoenix::bind(&is_in_rectangle, expr::attr< point >(name), boost::lexical_cast< rectangle >(arg));
  137. }
  138. throw std::runtime_error("Unsupported filter relation: " + rel);
  139. }
  140. };
  141. void init_factories()
  142. {
  143. //<-
  144. logging::register_simple_formatter_factory< point, char >("Coordinates");
  145. //->
  146. logging::register_filter_factory("Coordinates", boost::make_shared< point_filter_factory >());
  147. }
  148. //]
  149. void init_logging()
  150. {
  151. init_factories();
  152. logging::add_console_log
  153. (
  154. std::clog,
  155. keywords::filter = "%Coordinates% is_in_rectangle \"{(0, 0) - (20, 20)}\"",
  156. keywords::format = "%TimeStamp% %Coordinates% %Message%"
  157. );
  158. logging::add_common_attributes();
  159. }
  160. int main(int, char*[])
  161. {
  162. init_logging();
  163. src::logger lg;
  164. // We have to use scoped attributes in order coordinates to be passed to filters
  165. {
  166. BOOST_LOG_SCOPED_LOGGER_TAG(lg, "Coordinates", point(10, 10));
  167. BOOST_LOG(lg) << "Hello, world with coordinates (10, 10)!";
  168. }
  169. {
  170. BOOST_LOG_SCOPED_LOGGER_TAG(lg, "Coordinates", point(50, 50));
  171. BOOST_LOG(lg) << "Hello, world with coordinates (50, 50)!"; // this message will be suppressed by filter
  172. }
  173. return 0;
  174. }