• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
9 #if !defined(BOOST_LOG_WITHOUT_IPC)
10 
11 #include <iostream>
12 #include <sstream>
13 #include <exception>
14 #include <boost/smart_ptr/shared_ptr.hpp>
15 #include <boost/smart_ptr/make_shared_object.hpp>
16 #include <boost/date_time/posix_time/posix_time.hpp>
17 #include <boost/log/core.hpp>
18 #include <boost/log/attributes.hpp>
19 #include <boost/log/expressions.hpp>
20 #include <boost/log/sources/logger.hpp>
21 #include <boost/log/sources/record_ostream.hpp>
22 #include <boost/log/sinks/sync_frontend.hpp>
23 #include <boost/log/sinks/text_ipc_message_queue_backend.hpp>
24 #include <boost/log/utility/ipc/reliable_message_queue.hpp>
25 #include <boost/log/utility/ipc/object_name.hpp>
26 #include <boost/log/utility/open_mode.hpp>
27 #include <boost/log/utility/setup/common_attributes.hpp>
28 
29 namespace logging = boost::log;
30 namespace expr = boost::log::expressions;
31 namespace attrs = boost::log::attributes;
32 namespace src = boost::log::sources;
33 namespace sinks = boost::log::sinks;
34 namespace keywords = boost::log::keywords;
35 
36 //[ example_sinks_ipc_logger
37 BOOST_LOG_ATTRIBUTE_KEYWORD(a_timestamp, "TimeStamp", attrs::local_clock::value_type)
38 BOOST_LOG_ATTRIBUTE_KEYWORD(a_process_id, "ProcessID", attrs::current_process_id::value_type)
39 BOOST_LOG_ATTRIBUTE_KEYWORD(a_thread_id, "ThreadID", attrs::current_thread_id::value_type)
40 
main()41 int main()
42 {
43     try
44     {
45         typedef logging::ipc::reliable_message_queue queue_t;
46         typedef sinks::text_ipc_message_queue_backend< queue_t > backend_t;
47         typedef sinks::synchronous_sink< backend_t > sink_t;
48 
49         // Create a sink that is associated with the interprocess message queue
50         // named "ipc_message_queue".
51         boost::shared_ptr< sink_t > sink = boost::make_shared< sink_t >
52         (
53             keywords::name = logging::ipc::object_name(logging::ipc::object_name::user, "ipc_message_queue"),
54             keywords::open_mode = logging::open_mode::open_or_create,
55             keywords::capacity = 256,
56             keywords::block_size = 1024,
57             keywords::overflow_policy = queue_t::block_on_overflow
58         );
59 
60         // Set the formatter
61         sink->set_formatter
62         (
63             expr::stream << "[" << a_timestamp << "] [" << a_process_id << ":" << a_thread_id << "] " << expr::smessage
64         );
65 
66         logging::core::get()->add_sink(sink);
67 
68         // Add the commonly used attributes, including TimeStamp, ProcessID and ThreadID
69         logging::add_common_attributes();
70 
71         // Do some logging
72         src::logger logger;
73         for (unsigned int i = 1; i <= 10; ++i)
74         {
75             BOOST_LOG(logger) << "Message #" << i;
76         }
77     }
78     catch (std::exception& e)
79     {
80         std::cout << "Failure: " << e.what() << std::endl;
81     }
82 
83     return 0;
84 }
85 //]
86 
87 #else // !defined(BOOST_LOG_WITHOUT_IPC)
88 
main()89 int main()
90 {
91     return 0;
92 }
93 
94 #endif // !defined(BOOST_LOG_WITHOUT_IPC)
95