tutorial_filtering.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 <ostream>
  10. #include <fstream>
  11. #include <iomanip>
  12. #include <boost/smart_ptr/shared_ptr.hpp>
  13. #include <boost/smart_ptr/make_shared_object.hpp>
  14. #include <boost/phoenix/bind.hpp>
  15. #include <boost/date_time/posix_time/posix_time.hpp>
  16. #include <boost/log/core.hpp>
  17. #include <boost/log/expressions.hpp>
  18. #include <boost/log/attributes.hpp>
  19. #include <boost/log/sources/basic_logger.hpp>
  20. #include <boost/log/sources/severity_logger.hpp>
  21. #include <boost/log/sources/severity_channel_logger.hpp>
  22. #include <boost/log/sources/record_ostream.hpp>
  23. #include <boost/log/sinks/sync_frontend.hpp>
  24. #include <boost/log/sinks/text_ostream_backend.hpp>
  25. #include <boost/log/attributes/scoped_attribute.hpp>
  26. #include <boost/log/utility/value_ref.hpp>
  27. #include <boost/log/utility/setup/common_attributes.hpp>
  28. namespace logging = boost::log;
  29. namespace src = boost::log::sources;
  30. namespace expr = boost::log::expressions;
  31. namespace sinks = boost::log::sinks;
  32. namespace attrs = boost::log::attributes;
  33. namespace keywords = boost::log::keywords;
  34. // We define our own severity levels
  35. enum severity_level
  36. {
  37. normal,
  38. notification,
  39. warning,
  40. error,
  41. critical
  42. };
  43. // The operator puts a human-friendly representation of the severity level to the stream
  44. std::ostream& operator<< (std::ostream& strm, severity_level level)
  45. {
  46. static const char* strings[] =
  47. {
  48. "normal",
  49. "notification",
  50. "warning",
  51. "error",
  52. "critical"
  53. };
  54. if (static_cast< std::size_t >(level) < sizeof(strings) / sizeof(*strings))
  55. strm << strings[level];
  56. else
  57. strm << static_cast< int >(level);
  58. return strm;
  59. }
  60. //[ example_tutorial_filtering
  61. BOOST_LOG_ATTRIBUTE_KEYWORD(line_id, "LineID", unsigned int)
  62. BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", severity_level)
  63. BOOST_LOG_ATTRIBUTE_KEYWORD(tag_attr, "Tag", std::string)
  64. void init()
  65. {
  66. // Setup the common formatter for all sinks
  67. logging::formatter fmt = expr::stream
  68. << std::setw(6) << std::setfill('0') << line_id << std::setfill(' ')
  69. << ": <" << severity << ">\t"
  70. << expr::if_(expr::has_attr(tag_attr))
  71. [
  72. expr::stream << "[" << tag_attr << "] "
  73. ]
  74. << expr::smessage;
  75. // Initialize sinks
  76. typedef sinks::synchronous_sink< sinks::text_ostream_backend > text_sink;
  77. boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >();
  78. sink->locked_backend()->add_stream(
  79. boost::make_shared< std::ofstream >("full.log"));
  80. sink->set_formatter(fmt);
  81. logging::core::get()->add_sink(sink);
  82. sink = boost::make_shared< text_sink >();
  83. sink->locked_backend()->add_stream(
  84. boost::make_shared< std::ofstream >("important.log"));
  85. sink->set_formatter(fmt);
  86. sink->set_filter(severity >= warning || (expr::has_attr(tag_attr) && tag_attr == "IMPORTANT_MESSAGE"));
  87. logging::core::get()->add_sink(sink);
  88. // Add attributes
  89. logging::add_common_attributes();
  90. }
  91. //]
  92. #if 0
  93. //[ example_tutorial_filtering_bind
  94. bool my_filter(logging::value_ref< severity_level, tag::severity > const& level,
  95. logging::value_ref< std::string, tag::tag_attr > const& tag)
  96. {
  97. return level >= warning || tag == "IMPORTANT_MESSAGE";
  98. }
  99. void init()
  100. {
  101. //<-
  102. // Setup the common formatter for all sinks
  103. logging::formatter fmt = expr::stream
  104. << std::setw(6) << std::setfill('0') << line_id << std::setfill(' ')
  105. << ": <" << severity << ">\t"
  106. << expr::if_(expr::has_attr(tag_attr))
  107. [
  108. expr::stream << "[" << tag_attr << "] "
  109. ]
  110. << expr::smessage;
  111. // Initialize sinks
  112. typedef sinks::synchronous_sink< sinks::text_ostream_backend > text_sink;
  113. boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >();
  114. sink->locked_backend()->add_stream(
  115. boost::make_shared< std::ofstream >("full.log"));
  116. sink->set_formatter(fmt);
  117. logging::core::get()->add_sink(sink);
  118. sink = boost::make_shared< text_sink >();
  119. sink->locked_backend()->add_stream(
  120. boost::make_shared< std::ofstream >("important.log"));
  121. sink->set_formatter(fmt);
  122. //->
  123. // ...
  124. namespace phoenix = boost::phoenix;
  125. sink->set_filter(phoenix::bind(&my_filter, severity.or_none(), tag_attr.or_none()));
  126. // ...
  127. //<-
  128. logging::core::get()->add_sink(sink);
  129. // Add attributes
  130. logging::add_common_attributes();
  131. //->
  132. }
  133. //]
  134. #endif
  135. void logging_function()
  136. {
  137. src::severity_logger< severity_level > slg;
  138. BOOST_LOG_SEV(slg, normal) << "A regular message";
  139. BOOST_LOG_SEV(slg, warning) << "Something bad is going on but I can handle it";
  140. BOOST_LOG_SEV(slg, critical) << "Everything crumbles, shoot me now!";
  141. {
  142. BOOST_LOG_SCOPED_THREAD_TAG("Tag", "IMPORTANT_MESSAGE");
  143. BOOST_LOG_SEV(slg, normal) << "An important message";
  144. }
  145. }
  146. int main(int, char*[])
  147. {
  148. init();
  149. logging_function();
  150. return 0;
  151. }