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 26.04.2008
11 *
12 * \brief This example shows how to perform logging to several files simultaneously,
13 * with files being created on an attribute value basis - thread identifier in this case.
14 * In the example the application creates a number of threads and registers thread
15 * identifiers as attributes. Every thread performs logging, and the sink separates
16 * log records from different threads into different files.
17 */
18
19 // #define BOOST_LOG_DYN_LINK 1
20
21 #include <stdexcept>
22 #include <string>
23 #include <iostream>
24 #include <boost/smart_ptr/shared_ptr.hpp>
25 #include <boost/thread/thread.hpp>
26 #include <boost/date_time/posix_time/posix_time.hpp>
27
28 #include <boost/log/common.hpp>
29 #include <boost/log/expressions.hpp>
30 #include <boost/log/attributes.hpp>
31 #include <boost/log/sources/logger.hpp>
32 #include <boost/log/sinks/sync_frontend.hpp>
33 #include <boost/log/sinks/text_multifile_backend.hpp>
34
35 namespace logging = boost::log;
36 namespace attrs = boost::log::attributes;
37 namespace src = boost::log::sources;
38 namespace sinks = boost::log::sinks;
39 namespace expr = boost::log::expressions;
40 namespace keywords = boost::log::keywords;
41
42 using boost::shared_ptr;
43
44 enum
45 {
46 THREAD_COUNT = 5,
47 LOG_RECORDS_TO_WRITE = 10
48 };
49
50 // Global logger declaration
BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(my_logger,src::logger_mt)51 BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(my_logger, src::logger_mt)
52
53 // This function is executed in a separate thread
54 void thread_foo()
55 {
56 BOOST_LOG_SCOPED_THREAD_TAG("ThreadID", boost::this_thread::get_id());
57 for (unsigned int i = 0; i < LOG_RECORDS_TO_WRITE; ++i)
58 {
59 BOOST_LOG(my_logger::get()) << "Log record " << i;
60 }
61 }
62
main(int argc,char * argv[])63 int main(int argc, char* argv[])
64 {
65 try
66 {
67 // Create a text file sink
68 typedef sinks::synchronous_sink< sinks::text_multifile_backend > file_sink;
69 shared_ptr< file_sink > sink(new file_sink);
70
71 // Set up how the file names will be generated
72 sink->locked_backend()->set_file_name_composer(sinks::file::as_file_name_composer(
73 expr::stream << "logs/" << expr::attr< boost::thread::id >("ThreadID") << ".log"));
74
75 // Set the log record formatter
76 sink->set_formatter
77 (
78 expr::format("%1%: [%2%] - %3%")
79 % expr::attr< unsigned int >("RecordID")
80 % expr::attr< boost::posix_time::ptime >("TimeStamp")
81 % expr::smessage
82 );
83
84 // Add it to the core
85 logging::core::get()->add_sink(sink);
86
87 // Add some attributes too
88 logging::core::get()->add_global_attribute("TimeStamp", attrs::local_clock());
89 logging::core::get()->add_global_attribute("RecordID", attrs::counter< unsigned int >());
90
91 // Create threads and make some logs
92 boost::thread_group threads;
93 for (unsigned int i = 0; i < THREAD_COUNT; ++i)
94 threads.create_thread(&thread_foo);
95
96 threads.join_all();
97
98 return 0;
99 }
100 catch (std::exception& e)
101 {
102 std::cout << "FAILURE: " << e.what() << std::endl;
103 return 1;
104 }
105 }
106