• 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 #include <boost/log/core.hpp>
9 #include <boost/log/trivial.hpp>
10 #include <boost/log/expressions.hpp>
11 #include <boost/log/sinks/text_file_backend.hpp>
12 #include <boost/log/utility/setup/file.hpp>
13 #include <boost/log/utility/setup/common_attributes.hpp>
14 #include <boost/log/sources/severity_logger.hpp>
15 #include <boost/log/sources/record_ostream.hpp>
16 
17 namespace logging = boost::log;
18 namespace src = boost::log::sources;
19 namespace sinks = boost::log::sinks;
20 namespace keywords = boost::log::keywords;
21 
22 #if 0
23 
24 //[ example_tutorial_file_simple
25 void init()
26 {
27     logging::add_file_log("sample.log");
28 
29     logging::core::get()->set_filter
30     (
31         logging::trivial::severity >= logging::trivial::info
32     );
33 }
34 //]
35 
36 // We need this due to this bug: https://svn.boost.org/trac/boost/ticket/4416
37 //[ example_tutorial_file_advanced_no_callouts
38 void init()
39 {
40     logging::add_file_log
41     (
42         keywords::file_name = "sample_%N.log",
43         keywords::rotation_size = 10 * 1024 * 1024,
44         keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0),
45         keywords::format = "[%TimeStamp%]: %Message%"
46     );
47 
48     logging::core::get()->set_filter
49     (
50         logging::trivial::severity >= logging::trivial::info
51     );
52 }
53 //]
54 
55 #else
56 
57 //[ example_tutorial_file_advanced
init()58 void init()
59 {
60     logging::add_file_log
61     (
62         keywords::file_name = "sample_%N.log",                                        /*< file name pattern >*/
63         keywords::rotation_size = 10 * 1024 * 1024,                                   /*< rotate files every 10 MiB... >*/
64         keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0), /*< ...or at midnight >*/
65         keywords::format = "[%TimeStamp%]: %Message%"                                 /*< log record format >*/
66     );
67 
68     logging::core::get()->set_filter
69     (
70         logging::trivial::severity >= logging::trivial::info
71     );
72 }
73 //]
74 
75 #endif
76 
main(int,char * [])77 int main(int, char*[])
78 {
79     init();
80     logging::add_common_attributes();
81 
82     using namespace logging::trivial;
83     src::severity_logger< severity_level > lg;
84 
85     BOOST_LOG_SEV(lg, trace) << "A trace severity message";
86     BOOST_LOG_SEV(lg, debug) << "A debug severity message";
87     BOOST_LOG_SEV(lg, info) << "An informational severity message";
88     BOOST_LOG_SEV(lg, warning) << "A warning severity message";
89     BOOST_LOG_SEV(lg, error) << "An error severity message";
90     BOOST_LOG_SEV(lg, fatal) << "A fatal severity message";
91 
92     return 0;
93 }
94