]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/log/example/multiple_threads/main.cpp
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 logging in multiple threads.
13 * See the library tutorial for expanded comments on this code.
14 * It may also be worthwhile reading the Wiki requirements page:
15 * http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl?Boost.Logging
18 // #define BOOST_LOG_DYN_LINK 1
24 #include <boost/ref.hpp>
25 #include <boost/bind.hpp>
26 #include <boost/smart_ptr/shared_ptr.hpp>
27 #include <boost/date_time/posix_time/posix_time.hpp>
28 #include <boost/thread/thread.hpp>
29 #include <boost/thread/barrier.hpp>
31 #include <boost/log/common.hpp>
32 #include <boost/log/expressions.hpp>
33 #include <boost/log/attributes.hpp>
34 #include <boost/log/sinks.hpp>
35 #include <boost/log/sources/logger.hpp>
37 namespace logging
= boost::log
;
38 namespace attrs
= boost::log::attributes
;
39 namespace src
= boost::log::sources
;
40 namespace sinks
= boost::log::sinks
;
41 namespace expr
= boost::log::expressions
;
42 namespace keywords
= boost::log::keywords
;
44 using boost::shared_ptr
;
48 LOG_RECORDS_TO_WRITE
= 10000,
52 BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(test_lg
, src::logger_mt
)
54 //! This function is executed in multiple threads
55 void thread_fun(boost::barrier
& bar
)
57 // Wait until all threads are created
60 // Now, do some logging
61 for (unsigned int i
= 0; i
< LOG_RECORDS_TO_WRITE
; ++i
)
63 BOOST_LOG(test_lg::get()) << "Log record " << i
;
67 int main(int argc
, char* argv
[])
71 // Open a rotating text file
72 shared_ptr
< std::ostream
> strm(new std::ofstream("test.log"));
74 throw std::runtime_error("Failed to open a text log file");
76 // Create a text file sink
77 shared_ptr
< sinks::synchronous_sink
< sinks::text_ostream_backend
> > sink(
78 new sinks::synchronous_sink
< sinks::text_ostream_backend
>);
80 sink
->locked_backend()->add_stream(strm
);
84 expr::format("%1%: [%2%] [%3%] - %4%")
85 % expr::attr
< unsigned int >("RecordID")
86 % expr::attr
< boost::posix_time::ptime
>("TimeStamp")
87 % expr::attr
< attrs::current_thread_id::value_type
>("ThreadID")
92 logging::core::get()->add_sink(sink
);
94 // Add some attributes too
95 logging::core::get()->add_global_attribute("TimeStamp", attrs::local_clock());
96 logging::core::get()->add_global_attribute("RecordID", attrs::counter
< unsigned int >());
97 logging::core::get()->add_global_attribute("ThreadID", attrs::current_thread_id());
99 // Create logging threads
100 boost::barrier
bar(THREAD_COUNT
);
101 boost::thread_group threads
;
102 for (unsigned int i
= 0; i
< THREAD_COUNT
; ++i
)
103 threads
.create_thread(boost::bind(&thread_fun
, boost::ref(bar
)));
105 // Wait until all action ends
110 catch (std::exception
& e
)
112 std::cout
<< "FAILURE: " << e
.what() << std::endl
;