extension_formatter_parser.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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/format.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/sources/logger.hpp>
  18. #include <boost/log/sources/record_ostream.hpp>
  19. #include <boost/log/utility/value_ref.hpp>
  20. #include <boost/log/utility/formatting_ostream.hpp>
  21. #include <boost/log/utility/manipulators/add_value.hpp>
  22. #include <boost/log/utility/setup/formatter_parser.hpp>
  23. #include <boost/log/utility/setup/common_attributes.hpp>
  24. #include <boost/log/utility/setup/console.hpp>
  25. namespace logging = boost::log;
  26. namespace attrs = boost::log::attributes;
  27. namespace src = boost::log::sources;
  28. namespace expr = boost::log::expressions;
  29. namespace sinks = boost::log::sinks;
  30. namespace keywords = boost::log::keywords;
  31. //[ example_extension_formatter_parser_point_definition
  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. template< typename CharT, typename TraitsT >
  39. std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, point const& p)
  40. {
  41. strm << "(" << p.m_x << ", " << p.m_y << ")";
  42. return strm;
  43. }
  44. //]
  45. #if 0
  46. //[ example_extension_simple_formatter_factory
  47. void init_factories()
  48. {
  49. logging::register_simple_formatter_factory< point, char >("Coordinates");
  50. }
  51. //]
  52. #endif
  53. //[ example_extension_custom_formatter_factory
  54. // Custom point formatter
  55. class point_formatter
  56. {
  57. public:
  58. typedef void result_type;
  59. public:
  60. explicit point_formatter(std::string const& fmt) : m_format(fmt)
  61. {
  62. }
  63. void operator() (logging::formatting_ostream& strm, logging::value_ref< point > const& value) const
  64. {
  65. if (value)
  66. {
  67. point const& p = value.get();
  68. m_format % p.m_x % p.m_y;
  69. strm << m_format;
  70. m_format.clear();
  71. }
  72. }
  73. private:
  74. mutable boost::format m_format;
  75. };
  76. // Custom point formatter factory
  77. class point_formatter_factory :
  78. public logging::basic_formatter_factory< char, point >
  79. {
  80. public:
  81. formatter_type create_formatter(logging::attribute_name const& name, args_map const& args)
  82. {
  83. args_map::const_iterator it = args.find("format");
  84. if (it != args.end())
  85. return boost::phoenix::bind(point_formatter(it->second), expr::stream, expr::attr< point >(name));
  86. else
  87. return expr::stream << expr::attr< point >(name);
  88. }
  89. };
  90. void init_factories()
  91. {
  92. logging::register_formatter_factory("Coordinates", boost::make_shared< point_formatter_factory >());
  93. }
  94. //]
  95. void init_logging()
  96. {
  97. init_factories();
  98. logging::add_console_log(std::clog, keywords::format = "%TimeStamp% %Coordinates(format=\"{%0.3f; %0.3f}\")% %Message%");
  99. logging::add_common_attributes();
  100. }
  101. int main(int, char*[])
  102. {
  103. init_logging();
  104. src::logger lg;
  105. BOOST_LOG(lg) << logging::add_value("Coordinates", point(10.5f, 20.2f)) << "Hello, world with coordinates!";
  106. return 0;
  107. }