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   10.06.2008
11  *
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
16  */
17 
18 // #define BOOST_LOG_DYN_LINK 1
19 
20 #include <stdexcept>
21 #include <string>
22 #include <iostream>
23 #include <fstream>
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>
30 
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>
36 
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;
43 
44 using boost::shared_ptr;
45 
46 enum
47 {
48     LOG_RECORDS_TO_WRITE = 10000,
49     THREAD_COUNT = 2
50 };
51 
BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(test_lg,src::logger_mt)52 BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(test_lg, src::logger_mt)
53 
54 //! This function is executed in multiple threads
55 void thread_fun(boost::barrier& bar)
56 {
57     // Wait until all threads are created
58     bar.wait();
59 
60     // Now, do some logging
61     for (unsigned int i = 0; i < LOG_RECORDS_TO_WRITE; ++i)
62     {
63         BOOST_LOG(test_lg::get()) << "Log record " << i;
64     }
65 }
66 
main(int argc,char * argv[])67 int main(int argc, char* argv[])
68 {
69     try
70     {
71         // Open a rotating text file
72         shared_ptr< std::ostream > strm(new std::ofstream("test.log"));
73         if (!strm->good())
74             throw std::runtime_error("Failed to open a text log file");
75 
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 >);
79 
80         sink->locked_backend()->add_stream(strm);
81 
82         sink->set_formatter
83         (
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")
88                 % expr::smessage
89         );
90 
91         // Add it to the core
92         logging::core::get()->add_sink(sink);
93 
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());
98 
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)));
104 
105         // Wait until all action ends
106         threads.join_all();
107 
108         return 0;
109     }
110     catch (std::exception& e)
111     {
112         std::cout << "FAILURE: " << e.what() << std::endl;
113         return 1;
114     }
115 }
116