main.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 10.06.2008
  11. *
  12. * \brief An example of logging in multiple threads.
  13. * See the library tutorial for expanded comments on this code.
  14. * It may also be worthwhile reading the Wiki requirements page:
  15. * http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl?Boost.Logging
  16. */
  17. // #define BOOST_LOG_DYN_LINK 1
  18. #include <stdexcept>
  19. #include <string>
  20. #include <iostream>
  21. #include <fstream>
  22. #include <boost/ref.hpp>
  23. #include <boost/bind.hpp>
  24. #include <boost/smart_ptr/shared_ptr.hpp>
  25. #include <boost/date_time/posix_time/posix_time.hpp>
  26. #include <boost/thread/thread.hpp>
  27. #include <boost/thread/barrier.hpp>
  28. #include <boost/log/common.hpp>
  29. #include <boost/log/expressions.hpp>
  30. #include <boost/log/attributes.hpp>
  31. #include <boost/log/sinks.hpp>
  32. #include <boost/log/sources/logger.hpp>
  33. namespace logging = boost::log;
  34. namespace attrs = boost::log::attributes;
  35. namespace src = boost::log::sources;
  36. namespace sinks = boost::log::sinks;
  37. namespace expr = boost::log::expressions;
  38. namespace keywords = boost::log::keywords;
  39. using boost::shared_ptr;
  40. enum
  41. {
  42. LOG_RECORDS_TO_WRITE = 10000,
  43. THREAD_COUNT = 2
  44. };
  45. BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(test_lg, src::logger_mt)
  46. //! This function is executed in multiple threads
  47. void thread_fun(boost::barrier& bar)
  48. {
  49. // Wait until all threads are created
  50. bar.wait();
  51. // Now, do some logging
  52. for (unsigned int i = 0; i < LOG_RECORDS_TO_WRITE; ++i)
  53. {
  54. BOOST_LOG(test_lg::get()) << "Log record " << i;
  55. }
  56. }
  57. int main(int argc, char* argv[])
  58. {
  59. try
  60. {
  61. // Open a rotating text file
  62. shared_ptr< std::ostream > strm(new std::ofstream("test.log"));
  63. if (!strm->good())
  64. throw std::runtime_error("Failed to open a text log file");
  65. // Create a text file sink
  66. shared_ptr< sinks::synchronous_sink< sinks::text_ostream_backend > > sink(
  67. new sinks::synchronous_sink< sinks::text_ostream_backend >);
  68. sink->locked_backend()->add_stream(strm);
  69. sink->set_formatter
  70. (
  71. expr::format("%1%: [%2%] [%3%] - %4%")
  72. % expr::attr< unsigned int >("RecordID")
  73. % expr::attr< boost::posix_time::ptime >("TimeStamp")
  74. % expr::attr< attrs::current_thread_id::value_type >("ThreadID")
  75. % expr::smessage
  76. );
  77. // Add it to the core
  78. logging::core::get()->add_sink(sink);
  79. // Add some attributes too
  80. logging::core::get()->add_global_attribute("TimeStamp", attrs::local_clock());
  81. logging::core::get()->add_global_attribute("RecordID", attrs::counter< unsigned int >());
  82. logging::core::get()->add_global_attribute("ThreadID", attrs::current_thread_id());
  83. // Create logging threads
  84. boost::barrier bar(THREAD_COUNT);
  85. boost::thread_group threads;
  86. for (unsigned int i = 0; i < THREAD_COUNT; ++i)
  87. threads.create_thread(boost::bind(&thread_fun, boost::ref(bar)));
  88. // Wait until all action ends
  89. threads.join_all();
  90. return 0;
  91. }
  92. catch (std::exception& e)
  93. {
  94. std::cout << "FAILURE: " << e.what() << std::endl;
  95. return 1;
  96. }
  97. }