main.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. /*!
  8. * \file main.cpp
  9. * \author Andrey Semashev
  10. * \date 30.08.2009
  11. *
  12. * \brief An example of asynchronous logging with bounded log record queue in multiple threads.
  13. */
  14. // #define BOOST_LOG_DYN_LINK 1
  15. #include <stdexcept>
  16. #include <string>
  17. #include <iostream>
  18. #include <fstream>
  19. #include <functional>
  20. #include <boost/ref.hpp>
  21. #include <boost/bind.hpp>
  22. #include <boost/smart_ptr/shared_ptr.hpp>
  23. #include <boost/date_time/posix_time/posix_time.hpp>
  24. #include <boost/thread/thread.hpp>
  25. #include <boost/thread/barrier.hpp>
  26. #include <boost/log/common.hpp>
  27. #include <boost/log/expressions.hpp>
  28. #include <boost/log/attributes.hpp>
  29. #include <boost/log/sinks.hpp>
  30. #include <boost/log/sources/logger.hpp>
  31. #include <boost/log/utility/record_ordering.hpp>
  32. namespace logging = boost::log;
  33. namespace attrs = boost::log::attributes;
  34. namespace src = boost::log::sources;
  35. namespace sinks = boost::log::sinks;
  36. namespace expr = boost::log::expressions;
  37. namespace keywords = boost::log::keywords;
  38. using boost::shared_ptr;
  39. enum
  40. {
  41. LOG_RECORDS_TO_WRITE = 10000,
  42. THREAD_COUNT = 2
  43. };
  44. BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(test_lg, src::logger_mt)
  45. //! This function is executed in multiple threads
  46. void thread_fun(boost::barrier& bar)
  47. {
  48. // Wait until all threads are created
  49. bar.wait();
  50. // Here we go. First, identify the thread.
  51. BOOST_LOG_SCOPED_THREAD_TAG("ThreadID", boost::this_thread::get_id());
  52. // Now, do some logging
  53. for (unsigned int i = 0; i < LOG_RECORDS_TO_WRITE; ++i)
  54. {
  55. BOOST_LOG(test_lg::get()) << "Log record " << i;
  56. }
  57. }
  58. int main(int argc, char* argv[])
  59. {
  60. try
  61. {
  62. // Open a rotating text file
  63. shared_ptr< std::ostream > strm(new std::ofstream("test.log"));
  64. if (!strm->good())
  65. throw std::runtime_error("Failed to open a text log file");
  66. // Create a text file sink
  67. typedef sinks::text_ostream_backend backend_t;
  68. typedef sinks::asynchronous_sink<
  69. backend_t,
  70. sinks::bounded_ordering_queue<
  71. logging::attribute_value_ordering< unsigned int, std::less< unsigned int > >,
  72. 128, // queue no more than 128 log records
  73. sinks::block_on_overflow // wait until records are processed
  74. >
  75. > sink_t;
  76. shared_ptr< sink_t > sink(new sink_t(
  77. boost::make_shared< backend_t >(),
  78. // We'll apply record ordering to ensure that records from different threads go sequentially in the file
  79. keywords::order = logging::make_attr_ordering("RecordID", std::less< unsigned int >())));
  80. sink->locked_backend()->add_stream(strm);
  81. sink->set_formatter
  82. (
  83. expr::format("%1%: [%2%] [%3%] - %4%")
  84. % expr::attr< unsigned int >("RecordID")
  85. % expr::attr< boost::posix_time::ptime >("TimeStamp")
  86. % expr::attr< boost::thread::id >("ThreadID")
  87. % expr::smessage
  88. );
  89. // Add it to the core
  90. logging::core::get()->add_sink(sink);
  91. // Add some attributes too
  92. logging::core::get()->add_global_attribute("TimeStamp", attrs::local_clock());
  93. logging::core::get()->add_global_attribute("RecordID", attrs::counter< unsigned int >());
  94. // Create logging threads
  95. boost::barrier bar(THREAD_COUNT);
  96. boost::thread_group threads;
  97. for (unsigned int i = 0; i < THREAD_COUNT; ++i)
  98. threads.create_thread(boost::bind(&thread_fun, boost::ref(bar)));
  99. // Wait until all action ends
  100. threads.join_all();
  101. // Flush all buffered records
  102. sink->stop();
  103. sink->flush();
  104. return 0;
  105. }
  106. catch (std::exception& e)
  107. {
  108. std::cout << "FAILURE: " << e.what() << std::endl;
  109. return 1;
  110. }
  111. }