sinks_ipc_logger.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright Lingxi Li 2015.
  3. * Copyright Andrey Semashev 2016.
  4. * Distributed under the Boost Software License, Version 1.0.
  5. * (See accompanying file LICENSE_1_0.txt or copy at
  6. * http://www.boost.org/LICENSE_1_0.txt)
  7. */
  8. #if !defined(BOOST_LOG_WITHOUT_IPC)
  9. #include <iostream>
  10. #include <sstream>
  11. #include <exception>
  12. #include <boost/smart_ptr/shared_ptr.hpp>
  13. #include <boost/smart_ptr/make_shared_object.hpp>
  14. #include <boost/date_time/posix_time/posix_time.hpp>
  15. #include <boost/log/core.hpp>
  16. #include <boost/log/attributes.hpp>
  17. #include <boost/log/expressions.hpp>
  18. #include <boost/log/sources/logger.hpp>
  19. #include <boost/log/sources/record_ostream.hpp>
  20. #include <boost/log/sinks/sync_frontend.hpp>
  21. #include <boost/log/sinks/text_ipc_message_queue_backend.hpp>
  22. #include <boost/log/utility/ipc/reliable_message_queue.hpp>
  23. #include <boost/log/utility/ipc/object_name.hpp>
  24. #include <boost/log/utility/open_mode.hpp>
  25. #include <boost/log/utility/setup/common_attributes.hpp>
  26. namespace logging = boost::log;
  27. namespace expr = boost::log::expressions;
  28. namespace attrs = boost::log::attributes;
  29. namespace src = boost::log::sources;
  30. namespace sinks = boost::log::sinks;
  31. namespace keywords = boost::log::keywords;
  32. //[ example_sinks_ipc_logger
  33. BOOST_LOG_ATTRIBUTE_KEYWORD(a_timestamp, "TimeStamp", attrs::local_clock::value_type)
  34. BOOST_LOG_ATTRIBUTE_KEYWORD(a_process_id, "ProcessID", attrs::current_process_id::value_type)
  35. BOOST_LOG_ATTRIBUTE_KEYWORD(a_thread_id, "ThreadID", attrs::current_thread_id::value_type)
  36. int main()
  37. {
  38. try
  39. {
  40. typedef logging::ipc::reliable_message_queue queue_t;
  41. typedef sinks::text_ipc_message_queue_backend< queue_t > backend_t;
  42. typedef sinks::synchronous_sink< backend_t > sink_t;
  43. // Create a sink that is associated with the interprocess message queue
  44. // named "ipc_message_queue".
  45. boost::shared_ptr< sink_t > sink = boost::make_shared< sink_t >
  46. (
  47. keywords::name = logging::ipc::object_name(logging::ipc::object_name::user, "ipc_message_queue"),
  48. keywords::open_mode = logging::open_mode::open_or_create,
  49. keywords::capacity = 256,
  50. keywords::block_size = 1024,
  51. keywords::overflow_policy = queue_t::block_on_overflow
  52. );
  53. // Set the formatter
  54. sink->set_formatter
  55. (
  56. expr::stream << "[" << a_timestamp << "] [" << a_process_id << ":" << a_thread_id << "] " << expr::smessage
  57. );
  58. logging::core::get()->add_sink(sink);
  59. // Add the commonly used attributes, including TimeStamp, ProcessID and ThreadID
  60. logging::add_common_attributes();
  61. // Do some logging
  62. src::logger logger;
  63. for (unsigned int i = 1; i <= 10; ++i)
  64. {
  65. BOOST_LOG(logger) << "Message #" << i;
  66. }
  67. }
  68. catch (std::exception& e)
  69. {
  70. std::cout << "Failure: " << e.what() << std::endl;
  71. }
  72. return 0;
  73. }
  74. //]
  75. #else // !defined(BOOST_LOG_WITHOUT_IPC)
  76. int main()
  77. {
  78. return 0;
  79. }
  80. #endif // !defined(BOOST_LOG_WITHOUT_IPC)