• 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 <cstddef>
9 #include <string>
10 #include <ostream>
11 #include <fstream>
12 #include <boost/move/utility_core.hpp>
13 #include <boost/smart_ptr/shared_ptr.hpp>
14 #include <boost/smart_ptr/make_shared_object.hpp>
15 #include <boost/log/core.hpp>
16 #include <boost/log/expressions.hpp>
17 #include <boost/log/sources/severity_logger.hpp>
18 #include <boost/log/sources/record_ostream.hpp>
19 #include <boost/log/sinks/sync_frontend.hpp>
20 #include <boost/log/sinks/text_ostream_backend.hpp>
21 #include <boost/log/utility/setup/common_attributes.hpp>
22 
23 namespace logging = boost::log;
24 namespace src = boost::log::sources;
25 namespace expr = boost::log::expressions;
26 namespace sinks = boost::log::sinks;
27 namespace attrs = boost::log::attributes;
28 namespace keywords = boost::log::keywords;
29 
30 //[ example_sources_severity
31 // We define our own severity levels
32 enum severity_level
33 {
34     normal,
35     notification,
36     warning,
37     error,
38     critical
39 };
40 
logging_function()41 void logging_function()
42 {
43     // The logger implicitly adds a source-specific attribute 'Severity'
44     // of type 'severity_level' on construction
45     src::severity_logger< severity_level > slg;
46 
47     BOOST_LOG_SEV(slg, normal) << "A regular message";
48     BOOST_LOG_SEV(slg, warning) << "Something bad is going on but I can handle it";
49     BOOST_LOG_SEV(slg, critical) << "Everything crumbles, shoot me now!";
50 }
51 //]
52 
53 //[ example_sources_default_severity
default_severity()54 void default_severity()
55 {
56     // The default severity can be specified in constructor.
57     src::severity_logger< severity_level > error_lg(keywords::severity = error);
58 
59     BOOST_LOG(error_lg) << "An error level log record (by default)";
60 
61     // The explicitly specified level overrides the default
62     BOOST_LOG_SEV(error_lg, warning) << "A warning level log record (overrode the default)";
63 }
64 //]
65 
66 //[ example_sources_severity_manual
manual_logging()67 void manual_logging()
68 {
69     src::severity_logger< severity_level > slg;
70 
71     logging::record rec = slg.open_record(keywords::severity = normal);
72     if (rec)
73     {
74         logging::record_ostream strm(rec);
75         strm << "A regular message";
76         strm.flush();
77         slg.push_record(boost::move(rec));
78     }
79 }
80 //]
81 
82 // The operator puts a human-friendly representation of the severity level to the stream
operator <<(std::ostream & strm,severity_level level)83 std::ostream& operator<< (std::ostream& strm, severity_level level)
84 {
85     static const char* strings[] =
86     {
87         "normal",
88         "notification",
89         "warning",
90         "error",
91         "critical"
92     };
93 
94     if (static_cast< std::size_t >(level) < sizeof(strings) / sizeof(*strings))
95         strm << strings[level];
96     else
97         strm << static_cast< int >(level);
98 
99     return strm;
100 }
101 
init()102 void init()
103 {
104     typedef sinks::synchronous_sink< sinks::text_ostream_backend > text_sink;
105     boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >();
106 
107     sink->locked_backend()->add_stream(
108         boost::make_shared< std::ofstream >("sample.log"));
109 
110     sink->set_formatter
111     (
112         expr::stream
113             << expr::attr< unsigned int >("LineID")
114             << ": <" << expr::attr< severity_level >("Severity")
115             << ">\t"
116             << expr::smessage
117     );
118 
119     logging::core::get()->add_sink(sink);
120 
121     // Add attributes
122     logging::add_common_attributes();
123 }
124 
main(int,char * [])125 int main(int, char*[])
126 {
127     init();
128 
129     logging_function();
130     default_severity();
131     manual_logging();
132 
133     return 0;
134 }
135