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/smart_ptr/shared_ptr.hpp>
13 #include <boost/smart_ptr/make_shared_object.hpp>
14 #include <boost/log/core.hpp>
15 #include <boost/log/expressions.hpp>
16 #include <boost/log/sources/severity_channel_logger.hpp>
17 #include <boost/log/sources/record_ostream.hpp>
18 #include <boost/log/sources/global_logger_storage.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_channel
31 enum severity_level
32 {
33 normal,
34 notification,
35 warning,
36 error,
37 critical
38 };
39
40 typedef src::severity_channel_logger_mt<
41 severity_level, // the type of the severity level
42 std::string // the type of the channel name
43 > my_logger_mt;
44
45
BOOST_LOG_INLINE_GLOBAL_LOGGER_INIT(my_logger,my_logger_mt)46 BOOST_LOG_INLINE_GLOBAL_LOGGER_INIT(my_logger, my_logger_mt)
47 {
48 // Specify the channel name on construction, similarly as with the channel_logger
49 return my_logger_mt(keywords::channel = "my_logger");
50 }
51
logging_function()52 void logging_function()
53 {
54 // Do logging with the severity level. The record will have both
55 // the severity level and the channel name attached.
56 BOOST_LOG_SEV(my_logger::get(), normal) << "Hello, world!";
57 }
58 //]
59
60 // The operator puts a human-friendly representation of the severity level to the stream
operator <<(std::ostream & strm,severity_level level)61 std::ostream& operator<< (std::ostream& strm, severity_level level)
62 {
63 static const char* strings[] =
64 {
65 "normal",
66 "notification",
67 "warning",
68 "error",
69 "critical"
70 };
71
72 if (static_cast< std::size_t >(level) < sizeof(strings) / sizeof(*strings))
73 strm << strings[level];
74 else
75 strm << static_cast< int >(level);
76
77 return strm;
78 }
79
init()80 void init()
81 {
82 typedef sinks::synchronous_sink< sinks::text_ostream_backend > text_sink;
83 boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >();
84
85 sink->locked_backend()->add_stream(
86 boost::make_shared< std::ofstream >("sample.log"));
87
88 sink->set_formatter
89 (
90 expr::stream
91 << expr::attr< unsigned int >("LineID")
92 << ": <" << expr::attr< severity_level >("Severity")
93 << ">\t"
94 << "[" << expr::attr< std::string >("Channel") << "] "
95 << expr::smessage
96 );
97
98 logging::core::get()->add_sink(sink);
99
100 // Add attributes
101 logging::add_common_attributes();
102 }
103
main(int,char * [])104 int main(int, char*[])
105 {
106 init();
107 logging_function();
108
109 return 0;
110 }
111