• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  An example of logging into a rotating text file.
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 <boost/smart_ptr/shared_ptr.hpp>
24 #include <boost/date_time/posix_time/posix_time.hpp>
25 
26 #include <boost/log/common.hpp>
27 #include <boost/log/expressions.hpp>
28 #include <boost/log/attributes.hpp>
29 #include <boost/log/sources/logger.hpp>
30 #include <boost/log/sinks/sync_frontend.hpp>
31 #include <boost/log/sinks/text_file_backend.hpp>
32 
33 namespace logging = boost::log;
34 namespace attrs = boost::log::attributes;
35 namespace src = boost::log::sources;
36 namespace sinks = boost::log::sinks;
37 namespace expr = boost::log::expressions;
38 namespace keywords = boost::log::keywords;
39 
40 using boost::shared_ptr;
41 
42 enum { LOG_RECORDS_TO_WRITE = 10000 };
43 
main(int argc,char * argv[])44 int main(int argc, char* argv[])
45 {
46     try
47     {
48         // Create a text file sink
49         typedef sinks::synchronous_sink< sinks::text_file_backend > file_sink;
50         shared_ptr< file_sink > sink(new file_sink(
51             keywords::file_name = "file.log",                       // file name pattern
52             keywords::target_file_name = "%Y%m%d_%H%M%S_%5N.log",   // file name pattern
53             keywords::rotation_size = 16384                         // rotation size, in characters
54             ));
55 
56         // Set up where the rotated files will be stored
57         sink->locked_backend()->set_file_collector(sinks::file::make_collector(
58             keywords::target = "logs",                              // where to store rotated files
59             keywords::max_size = 16 * 1024 * 1024,                  // maximum total size of the stored files, in bytes
60             keywords::min_free_space = 100 * 1024 * 1024,           // minimum free space on the drive, in bytes
61             keywords::max_files = 512                               // maximum number of stored files
62             ));
63 
64         // Upon restart, scan the target directory for files matching the file_name pattern
65         sink->locked_backend()->scan_for_files();
66 
67         sink->set_formatter
68         (
69             expr::format("%1%: [%2%] - %3%")
70                 % expr::attr< unsigned int >("RecordID")
71                 % expr::attr< boost::posix_time::ptime >("TimeStamp")
72                 % expr::smessage
73         );
74 
75         // Add it to the core
76         logging::core::get()->add_sink(sink);
77 
78         // Add some attributes too
79         logging::core::get()->add_global_attribute("TimeStamp", attrs::local_clock());
80         logging::core::get()->add_global_attribute("RecordID", attrs::counter< unsigned int >());
81 
82         // Do some logging
83         src::logger lg;
84         for (unsigned int i = 0; i < LOG_RECORDS_TO_WRITE; ++i)
85         {
86             BOOST_LOG(lg) << "Some log record";
87         }
88 
89         return 0;
90     }
91     catch (std::exception& e)
92     {
93         std::cout << "FAILURE: " << e.what() << std::endl;
94         return 1;
95     }
96 }
97