1 /*
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)
6  */
7 /*!
8  * \file   main.cpp
9  * \author Andrey Semashev
10  * \date   30.08.2009
11  *
12  * \brief  An example of asynchronous logging in multiple threads.
13  */
14 
15 // #define BOOST_LOG_DYN_LINK 1
16 
17 #include <stdexcept>
18 #include <string>
19 #include <iostream>
20 #include <fstream>
21 #include <functional>
22 #include <boost/ref.hpp>
23 #include <boost/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>
28 
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>
35 
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;
42 
43 using boost::shared_ptr;
44 
45 enum
46 {
47     LOG_RECORDS_TO_WRITE = 10000,
48     THREAD_COUNT = 2
49 };
50 
BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(test_lg,src::logger_mt)51 BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(test_lg, src::logger_mt)
52 
53 //! This function is executed in multiple threads
54 void thread_fun(boost::barrier& bar)
55 {
56     // Wait until all threads are created
57     bar.wait();
58 
59     // Here we go. First, identify the thread.
60     BOOST_LOG_SCOPED_THREAD_TAG("ThreadID", boost::this_thread::get_id());
61 
62     // Now, do some logging
63     for (unsigned int i = 0; i < LOG_RECORDS_TO_WRITE; ++i)
64     {
65         BOOST_LOG(test_lg::get()) << "Log record " << i;
66     }
67 }
68 
main(int argc,char * argv[])69 int main(int argc, char* argv[])
70 {
71     try
72     {
73         // Open a rotating text file
74         shared_ptr< std::ostream > strm(new std::ofstream("test.log"));
75         if (!strm->good())
76             throw std::runtime_error("Failed to open a text log file");
77 
78         // Create a text file sink
79         typedef sinks::text_ostream_backend backend_t;
80         typedef sinks::asynchronous_sink<
81             backend_t,
82             sinks::unbounded_ordering_queue<
83                 logging::attribute_value_ordering< unsigned int, std::less< unsigned int > >
84             >
85         > sink_t;
86         shared_ptr< sink_t > sink(new sink_t(
87             boost::make_shared< backend_t >(),
88             // We'll apply record ordering to ensure that records from different threads go sequentially in the file
89             keywords::order = logging::make_attr_ordering("RecordID", std::less< unsigned int >())));
90 
91         sink->locked_backend()->add_stream(strm);
92 
93         sink->set_formatter
94         (
95             expr::format("%1%: [%2%] [%3%] - %4%")
96                 % expr::attr< unsigned int >("RecordID")
97                 % expr::attr< boost::posix_time::ptime >("TimeStamp")
98                 % expr::attr< boost::thread::id >("ThreadID")
99                 % expr::smessage
100         );
101 
102         // Add it to the core
103         logging::core::get()->add_sink(sink);
104 
105         // Add some attributes too
106         logging::core::get()->add_global_attribute("TimeStamp", attrs::local_clock());
107         logging::core::get()->add_global_attribute("RecordID", attrs::counter< unsigned int >());
108 
109         // Create logging threads
110         boost::barrier bar(THREAD_COUNT);
111         boost::thread_group threads;
112         for (unsigned int i = 0; i < THREAD_COUNT; ++i)
113             threads.create_thread(boost::bind(&thread_fun, boost::ref(bar)));
114 
115         // Wait until all action ends
116         threads.join_all();
117 
118         // Flush all buffered records
119         sink->stop();
120         sink->flush();
121 
122         return 0;
123     }
124     catch (std::exception& e)
125     {
126         std::cout << "FAILURE: " << e.what() << std::endl;
127         return 1;
128     }
129 }
130