sinks_ipc_receiver.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 <string>
  11. #include <exception>
  12. #include <boost/log/utility/ipc/reliable_message_queue.hpp>
  13. #include <boost/log/utility/ipc/object_name.hpp>
  14. #include <boost/log/utility/open_mode.hpp>
  15. namespace logging = boost::log;
  16. namespace keywords = boost::log::keywords;
  17. //[ example_sinks_ipc_receiver
  18. int main()
  19. {
  20. try
  21. {
  22. typedef logging::ipc::reliable_message_queue queue_t;
  23. // Create a message_queue_type object that is associated with the interprocess
  24. // message queue named "ipc_message_queue".
  25. queue_t queue
  26. (
  27. keywords::name = logging::ipc::object_name(logging::ipc::object_name::user, "ipc_message_queue"),
  28. keywords::open_mode = logging::open_mode::open_or_create,
  29. keywords::capacity = 256,
  30. keywords::block_size = 1024,
  31. keywords::overflow_policy = queue_t::block_on_overflow
  32. );
  33. std::cout << "Viewer process running..." << std::endl;
  34. // Keep reading log messages from the associated message queue and print them on the console.
  35. // queue.receive() will block if the queue is empty.
  36. std::string message;
  37. while (queue.receive(message) == queue_t::succeeded)
  38. {
  39. std::cout << message << std::endl;
  40. // Clear the buffer for the next message
  41. message.clear();
  42. }
  43. }
  44. catch (std::exception& e)
  45. {
  46. std::cout << "Failure: " << e.what() << std::endl;
  47. }
  48. return 0;
  49. }
  50. //]
  51. #else // !defined(BOOST_LOG_WITHOUT_IPC)
  52. int main()
  53. {
  54. return 0;
  55. }
  56. #endif // !defined(BOOST_LOG_WITHOUT_IPC)