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)
9 * \author Andrey Semashev
12 * \brief An example of asynchronous logging with bounded log record queue in multiple threads.
15 // #define BOOST_LOG_DYN_LINK 1
22 #include <boost/core/ref.hpp>
23 #include <boost/bind/bind.hpp>
24 #include <boost/smart_ptr/shared_ptr.hpp>
25 #include <boost/date_time/posix_time/posix_time.hpp>
26 #include <boost/thread/thread.hpp>
27 #include <boost/thread/barrier.hpp>
29 #include <boost/log/common.hpp>
30 #include <boost/log/expressions.hpp>
31 #include <boost/log/attributes.hpp>
32 #include <boost/log/sinks.hpp>
33 #include <boost/log/sources/logger.hpp>
34 #include <boost/log/utility/record_ordering.hpp>
36 namespace logging
= boost::log
;
37 namespace attrs
= boost::log::attributes
;
38 namespace src
= boost::log::sources
;
39 namespace sinks
= boost::log::sinks
;
40 namespace expr
= boost::log::expressions
;
41 namespace keywords
= boost::log::keywords
;
43 using boost::shared_ptr
;
47 LOG_RECORDS_TO_WRITE
= 10000,
51 BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(test_lg
, src::logger_mt
)
53 //! This function is executed in multiple threads
54 void thread_fun(boost::barrier
& bar
)
56 // Wait until all threads are created
59 // Here we go. First, identify the thread.
60 BOOST_LOG_SCOPED_THREAD_TAG("ThreadID", boost::this_thread::get_id());
62 // Now, do some logging
63 for (unsigned int i
= 0; i
< LOG_RECORDS_TO_WRITE
; ++i
)
65 BOOST_LOG(test_lg::get()) << "Log record " << i
;
69 int main(int argc
, char* argv
[])
73 // Open a rotating text file
74 shared_ptr
< std::ostream
> strm(new std::ofstream("test.log"));
76 throw std::runtime_error("Failed to open a text log file");
78 // Create a text file sink
79 typedef sinks::text_ostream_backend backend_t
;
80 typedef sinks::asynchronous_sink
<
82 sinks::bounded_ordering_queue
<
83 logging::attribute_value_ordering
< unsigned int, std::less
< unsigned int > >,
84 128, // queue no more than 128 log records
85 sinks::block_on_overflow
// wait until records are processed
88 shared_ptr
< sink_t
> sink(new sink_t(
89 boost::make_shared
< backend_t
>(),
90 // We'll apply record ordering to ensure that records from different threads go sequentially in the file
91 keywords::order
= logging::make_attr_ordering
< unsigned int >("RecordID", std::less
< unsigned int >())));
93 sink
->locked_backend()->add_stream(strm
);
97 expr::format("%1%: [%2%] [%3%] - %4%")
98 % expr::attr
< unsigned int >("RecordID")
99 % expr::attr
< boost::posix_time::ptime
>("TimeStamp")
100 % expr::attr
< boost::thread::id
>("ThreadID")
104 // Add it to the core
105 logging::core::get()->add_sink(sink
);
107 // Add some attributes too
108 logging::core::get()->add_global_attribute("TimeStamp", attrs::local_clock());
109 logging::core::get()->add_global_attribute("RecordID", attrs::counter
< unsigned int >());
111 // Create logging threads
112 boost::barrier
bar(THREAD_COUNT
);
113 boost::thread_group threads
;
114 for (unsigned int i
= 0; i
< THREAD_COUNT
; ++i
)
115 threads
.create_thread(boost::bind(&thread_fun
, boost::ref(bar
)));
117 // Wait until all action ends
120 // Flush all buffered records
126 catch (std::exception
& e
)
128 std::cout
<< "FAILURE: " << e
.what() << std::endl
;