]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | #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 | ||
27 | namespace logging = boost::log; | |
28 | namespace expr = boost::log::expressions; | |
29 | namespace attrs = boost::log::attributes; | |
30 | namespace src = boost::log::sources; | |
31 | namespace sinks = boost::log::sinks; | |
32 | namespace keywords = boost::log::keywords; | |
33 | ||
34 | //[ example_sinks_ipc_logger | |
35 | BOOST_LOG_ATTRIBUTE_KEYWORD(a_timestamp, "TimeStamp", attrs::local_clock::value_type) | |
36 | BOOST_LOG_ATTRIBUTE_KEYWORD(a_process_id, "ProcessID", attrs::current_process_id::value_type) | |
37 | BOOST_LOG_ATTRIBUTE_KEYWORD(a_thread_id, "ThreadID", attrs::current_thread_id::value_type) | |
38 | ||
39 | int main() | |
40 | { | |
41 | try | |
42 | { | |
43 | typedef logging::ipc::reliable_message_queue queue_t; | |
44 | typedef sinks::text_ipc_message_queue_backend< queue_t > backend_t; | |
45 | typedef sinks::synchronous_sink< backend_t > sink_t; | |
46 | ||
47 | // Create a sink that is associated with the interprocess message queue | |
48 | // named "ipc_message_queue". | |
49 | boost::shared_ptr< sink_t > sink = boost::make_shared< sink_t > | |
50 | ( | |
51 | keywords::name = logging::ipc::object_name(logging::ipc::object_name::user, "ipc_message_queue"), | |
52 | keywords::open_mode = logging::open_mode::open_or_create, | |
53 | keywords::capacity = 256, | |
54 | keywords::block_size = 1024, | |
55 | keywords::overflow_policy = queue_t::block_on_overflow | |
56 | ); | |
57 | ||
58 | // Set the formatter | |
59 | sink->set_formatter | |
60 | ( | |
61 | expr::stream << "[" << a_timestamp << "] [" << a_process_id << ":" << a_thread_id << "] " << expr::smessage | |
62 | ); | |
63 | ||
64 | logging::core::get()->add_sink(sink); | |
65 | ||
66 | // Add the commonly used attributes, including TimeStamp, ProcessID and ThreadID | |
67 | logging::add_common_attributes(); | |
68 | ||
69 | // Do some logging | |
70 | src::logger logger; | |
71 | for (unsigned int i = 1; i <= 10; ++i) | |
72 | { | |
73 | BOOST_LOG(logger) << "Message #" << i; | |
74 | } | |
75 | } | |
76 | catch (std::exception& e) | |
77 | { | |
78 | std::cout << "Failure: " << e.what() << std::endl; | |
79 | } | |
80 | ||
81 | return 0; | |
82 | } | |
83 | //] |