sinks_async_bounded.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 <boost/smart_ptr/shared_ptr.hpp>
  11. #include <boost/core/null_deleter.hpp>
  12. #include <boost/log/core.hpp>
  13. #include <boost/log/expressions.hpp>
  14. #include <boost/log/sinks/async_frontend.hpp>
  15. #include <boost/log/sinks/text_ostream_backend.hpp>
  16. #include <boost/log/sinks/bounded_fifo_queue.hpp>
  17. #include <boost/log/sinks/drop_on_overflow.hpp>
  18. #include <boost/log/sources/severity_channel_logger.hpp>
  19. #include <boost/log/sources/record_ostream.hpp>
  20. namespace logging = boost::log;
  21. namespace src = boost::log::sources;
  22. namespace expr = boost::log::expressions;
  23. namespace sinks = boost::log::sinks;
  24. namespace keywords = boost::log::keywords;
  25. enum severity_level
  26. {
  27. normal,
  28. warning,
  29. error
  30. };
  31. //[ example_sinks_bounded_async_init
  32. // Complete sink type
  33. typedef sinks::asynchronous_sink<
  34. sinks::text_ostream_backend,
  35. sinks::bounded_fifo_queue< /*< log record queueing strategy >*/
  36. 100, /*< record queue capacity >*/
  37. sinks::drop_on_overflow /*< overflow handling policy >*/
  38. >
  39. > sink_t;
  40. boost::shared_ptr< sink_t > init_logging()
  41. {
  42. boost::shared_ptr< logging::core > core = logging::core::get();
  43. // Create a backend and initialize it with a stream
  44. boost::shared_ptr< sinks::text_ostream_backend > backend =
  45. boost::make_shared< sinks::text_ostream_backend >();
  46. backend->add_stream(
  47. boost::shared_ptr< std::ostream >(&std::clog, boost::null_deleter()));
  48. // Wrap it into the frontend and register in the core
  49. boost::shared_ptr< sink_t > sink(new sink_t(backend));
  50. core->add_sink(sink);
  51. // ...
  52. //<-
  53. // You can manage filtering and formatting through the sink interface
  54. sink->set_filter(expr::attr< severity_level >("Severity") >= warning);
  55. sink->set_formatter
  56. (
  57. expr::stream
  58. << "Level: " << expr::attr< severity_level >("Severity")
  59. << " Message: " << expr::message
  60. );
  61. // You can also manage backend in a thread-safe manner
  62. {
  63. sink_t::locked_backend_ptr p = sink->locked_backend();
  64. p->add_stream(boost::make_shared< std::ofstream >("sample.log"));
  65. }
  66. //->
  67. return sink;
  68. }
  69. //]
  70. void stop_logging(boost::shared_ptr< sink_t >& sink)
  71. {
  72. boost::shared_ptr< logging::core > core = logging::core::get();
  73. // Remove the sink from the core, so that no records are passed to it
  74. core->remove_sink(sink);
  75. // Break the feeding loop
  76. sink->stop();
  77. // Flush all log records that may have left buffered
  78. sink->flush();
  79. sink.reset();
  80. }
  81. int main(int, char*[])
  82. {
  83. boost::shared_ptr< sink_t > sink = init_logging();
  84. src::severity_channel_logger< severity_level > lg(keywords::channel = "net");
  85. BOOST_LOG_SEV(lg, warning) << "Hello world!";
  86. stop_logging(sink);
  87. return 0;
  88. }