main.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 26.04.2008
  11. *
  12. * \brief This example shows how to perform logging to several files simultaneously,
  13. * with files being created on an attribute value basis - thread identifier in this case.
  14. * In the example the application creates a number of threads and registers thread
  15. * identifiers as attributes. Every thread performs logging, and the sink separates
  16. * log records from different threads into different files.
  17. */
  18. // #define BOOST_LOG_DYN_LINK 1
  19. #include <stdexcept>
  20. #include <string>
  21. #include <iostream>
  22. #include <boost/smart_ptr/shared_ptr.hpp>
  23. #include <boost/thread/thread.hpp>
  24. #include <boost/date_time/posix_time/posix_time.hpp>
  25. #include <boost/log/common.hpp>
  26. #include <boost/log/expressions.hpp>
  27. #include <boost/log/attributes.hpp>
  28. #include <boost/log/sources/logger.hpp>
  29. #include <boost/log/sinks/sync_frontend.hpp>
  30. #include <boost/log/sinks/text_multifile_backend.hpp>
  31. namespace logging = boost::log;
  32. namespace attrs = boost::log::attributes;
  33. namespace src = boost::log::sources;
  34. namespace sinks = boost::log::sinks;
  35. namespace expr = boost::log::expressions;
  36. namespace keywords = boost::log::keywords;
  37. using boost::shared_ptr;
  38. enum
  39. {
  40. THREAD_COUNT = 5,
  41. LOG_RECORDS_TO_WRITE = 10
  42. };
  43. // Global logger declaration
  44. BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(my_logger, src::logger_mt)
  45. // This function is executed in a separate thread
  46. void thread_foo()
  47. {
  48. BOOST_LOG_SCOPED_THREAD_TAG("ThreadID", boost::this_thread::get_id());
  49. for (unsigned int i = 0; i < LOG_RECORDS_TO_WRITE; ++i)
  50. {
  51. BOOST_LOG(my_logger::get()) << "Log record " << i;
  52. }
  53. }
  54. int main(int argc, char* argv[])
  55. {
  56. try
  57. {
  58. // Create a text file sink
  59. typedef sinks::synchronous_sink< sinks::text_multifile_backend > file_sink;
  60. shared_ptr< file_sink > sink(new file_sink);
  61. // Set up how the file names will be generated
  62. sink->locked_backend()->set_file_name_composer(sinks::file::as_file_name_composer(
  63. expr::stream << "logs/" << expr::attr< boost::thread::id >("ThreadID") << ".log"));
  64. // Set the log record formatter
  65. sink->set_formatter
  66. (
  67. expr::format("%1%: [%2%] - %3%")
  68. % expr::attr< unsigned int >("RecordID")
  69. % expr::attr< boost::posix_time::ptime >("TimeStamp")
  70. % expr::smessage
  71. );
  72. // Add it to the core
  73. logging::core::get()->add_sink(sink);
  74. // Add some attributes too
  75. logging::core::get()->add_global_attribute("TimeStamp", attrs::local_clock());
  76. logging::core::get()->add_global_attribute("RecordID", attrs::counter< unsigned int >());
  77. // Create threads and make some logs
  78. boost::thread_group threads;
  79. for (unsigned int i = 0; i < THREAD_COUNT; ++i)
  80. threads.create_thread(&thread_foo);
  81. threads.join_all();
  82. return 0;
  83. }
  84. catch (std::exception& e)
  85. {
  86. std::cout << "FAILURE: " << e.what() << std::endl;
  87. return 1;
  88. }
  89. }