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 with bounded log record queue 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::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
86             >
87         > sink_t;
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("RecordID", std::less< unsigned int >())));
92 
93         sink->locked_backend()->add_stream(strm);
94 
95         sink->set_formatter
96         (
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")
101                 % expr::smessage
102         );
103 
104         // Add it to the core
105         logging::core::get()->add_sink(sink);
106 
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 >());
110 
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)));
116 
117         // Wait until all action ends
118         threads.join_all();
119 
120         // Flush all buffered records
121         sink->stop();
122         sink->flush();
123 
124         return 0;
125     }
126     catch (std::exception& e)
127     {
128         std::cout << "FAILURE: " << e.what() << std::endl;
129         return 1;
130     }
131 }
132