sinks_async_ordering.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 <fstream>
  9. #include <iostream>
  10. #include <functional>
  11. #include <boost/smart_ptr/shared_ptr.hpp>
  12. #include <boost/core/null_deleter.hpp>
  13. #include <boost/date_time/posix_time/posix_time_types.hpp>
  14. #include <boost/log/core.hpp>
  15. #include <boost/log/expressions.hpp>
  16. #include <boost/log/sinks/async_frontend.hpp>
  17. #include <boost/log/sinks/unbounded_ordering_queue.hpp>
  18. #include <boost/log/sinks/text_ostream_backend.hpp>
  19. #include <boost/log/sources/severity_channel_logger.hpp>
  20. #include <boost/log/sources/record_ostream.hpp>
  21. #include <boost/log/utility/record_ordering.hpp>
  22. #include <boost/log/utility/setup/common_attributes.hpp>
  23. namespace logging = boost::log;
  24. namespace src = boost::log::sources;
  25. namespace expr = boost::log::expressions;
  26. namespace sinks = boost::log::sinks;
  27. namespace keywords = boost::log::keywords;
  28. enum severity_level
  29. {
  30. normal,
  31. warning,
  32. error
  33. };
  34. //[ example_sinks_ordering_async_init
  35. // Complete sink type
  36. typedef sinks::asynchronous_sink<
  37. sinks::text_ostream_backend,
  38. sinks::unbounded_ordering_queue< /*< log record queueing strategy >*/
  39. logging::attribute_value_ordering< /*< log record ordering predicate type >*/
  40. unsigned int, /*< attribute value type >*/
  41. std::less< unsigned int > /*< optional, attribute value comparison predicate; `std::less` equivalent is used by default >*/
  42. >
  43. >
  44. > sink_t;
  45. boost::shared_ptr< sink_t > init_logging()
  46. {
  47. boost::shared_ptr< logging::core > core = logging::core::get();
  48. // Create a backend and initialize it with a stream
  49. boost::shared_ptr< sinks::text_ostream_backend > backend =
  50. boost::make_shared< sinks::text_ostream_backend >();
  51. backend->add_stream(
  52. boost::shared_ptr< std::ostream >(&std::clog, boost::null_deleter()));
  53. // Wrap it into the frontend and register in the core
  54. boost::shared_ptr< sink_t > sink(new sink_t(
  55. backend, /*< pointer to the pre-initialized backend >*/
  56. keywords::order =
  57. logging::make_attr_ordering("LineID", std::less< unsigned int >()), /*< log record ordering predicate >*/
  58. keywords::ordering_window = boost::posix_time::seconds(1) /*< latency of log record processing >*/
  59. ));
  60. core->add_sink(sink);
  61. // You can manage filtering and formatting through the sink interface
  62. sink->set_filter(expr::attr< severity_level >("Severity") >= warning);
  63. sink->set_formatter
  64. (
  65. expr::stream
  66. << "Level: " << expr::attr< severity_level >("Severity")
  67. << " Message: " << expr::smessage
  68. );
  69. // You can also manage backend in a thread-safe manner
  70. {
  71. sink_t::locked_backend_ptr p = sink->locked_backend();
  72. p->add_stream(boost::make_shared< std::ofstream >("sample.log"));
  73. } // the backend gets released here
  74. return sink;
  75. }
  76. //]
  77. //[ example_sinks_ordering_async_stop
  78. void stop_logging(boost::shared_ptr< sink_t >& sink)
  79. {
  80. boost::shared_ptr< logging::core > core = logging::core::get();
  81. // Remove the sink from the core, so that no records are passed to it
  82. core->remove_sink(sink);
  83. // Break the feeding loop
  84. sink->stop();
  85. // Flush all log records that may have left buffered
  86. sink->flush();
  87. sink.reset();
  88. }
  89. //]
  90. int main(int, char*[])
  91. {
  92. boost::shared_ptr< sink_t > sink = init_logging();
  93. logging::add_common_attributes();
  94. src::severity_channel_logger< severity_level > lg(keywords::channel = "net");
  95. BOOST_LOG_SEV(lg, warning) << "Hello world!";
  96. stop_logging(sink);
  97. return 0;
  98. }