1 /*
2 * Copyright Andrey Semashev 2019.
3 * Distributed under the Boost Software License, Version 1.0.
4 * (See accompanying file LICENSE_1_0.txt or copy at
5 * https://www.boost.org/LICENSE_1_0.txt)
6 */
7
8 #include <cstddef>
9 #include <string>
10 #include <iostream>
11 #include <boost/log/sources/severity_logger.hpp>
12 #include <boost/log/sources/record_ostream.hpp>
13 #include <boost/log/utility/setup/common_attributes.hpp>
14 #include <boost/log/utility/setup/settings.hpp>
15 #include <boost/log/utility/setup/from_settings.hpp>
16 #include <boost/log/utility/setup/filter_parser.hpp>
17 #include <boost/log/utility/setup/formatter_parser.hpp>
18
19 namespace logging = boost::log;
20 namespace src = boost::log::sources;
21
22 // Let's define our own severity levels
23 enum severity_level
24 {
25 normal,
26 notification,
27 warning,
28 error,
29 critical
30 };
31
32 const char* const severity_strings[] =
33 {
34 "normal",
35 "notification",
36 "warning",
37 "error",
38 "critical"
39 };
40
41 // The operator puts a human-friendly representation of the severity level to the stream
operator <<(std::ostream & strm,severity_level level)42 std::ostream& operator<< (std::ostream& strm, severity_level level)
43 {
44 if (static_cast< std::size_t >(level) < sizeof(severity_strings) / sizeof(*severity_strings))
45 strm << severity_strings[level];
46 else
47 strm << static_cast< int >(level);
48
49 return strm;
50 }
51
52 // The operator parses the severity level from the stream
operator >>(std::istream & strm,severity_level & level)53 std::istream& operator>> (std::istream& strm, severity_level& level)
54 {
55 if (strm.good())
56 {
57 std::string str;
58 strm >> str;
59
60 for (unsigned int i = 0; i < sizeof(severity_strings) / sizeof(*severity_strings); ++i)
61 {
62 if (str == severity_strings[i])
63 {
64 level = static_cast< severity_level >(i);
65 return strm;
66 }
67 }
68
69 strm.setstate(std::ios_base::failbit);
70 }
71
72 return strm;
73 }
74
init_logging()75 void init_logging()
76 {
77 // Before initializing the library from settings, we need to register any custom filter and formatter factories
78 logging::register_simple_filter_factory< severity_level >("Severity");
79 logging::register_simple_formatter_factory< severity_level, char >("Severity");
80
81 //[ example_util_setup_settings
82 logging::settings setts;
83
84 setts["Core"]["Filter"] = "%Severity% >= warning";
85 setts["Core"]["DisableLogging"] = false;
86
87 // Subsections can be referred to with a single path
88 setts["Sinks.Console"]["Destination"] = "Console";
89 setts["Sinks.Console"]["Filter"] = "%Severity% >= critical";
90 setts["Sinks.Console"]["Format"] = "%TimeStamp% [%Severity%] %Message%";
91 setts["Sinks.Console"]["AutoFlush"] = true;
92
93 // ...as well as the individual parameters
94 setts["Sinks.File.Destination"] = "TextFile";
95 setts["Sinks.File.FileName"] = "MyApp_%3N.log";
96 setts["Sinks.File.AutoFlush"] = true;
97 setts["Sinks.File.RotationSize"] = 10 * 1024 * 1024; // 10 MiB
98 setts["Sinks.File.Format"] = "%TimeStamp% [%Severity%] %Message%";
99
100 logging::init_from_settings(setts);
101 //]
102
103 // Add attributes
104 logging::add_common_attributes();
105 }
106
main(int,char * [])107 int main(int, char*[])
108 {
109 init_logging();
110
111 src::severity_logger< severity_level > lg;
112
113 BOOST_LOG_SEV(lg, normal) << "A regular message";
114 BOOST_LOG_SEV(lg, warning) << "Something bad is going on but I can handle it";
115 BOOST_LOG_SEV(lg, critical) << "Everything crumbles, shoot me now!";
116
117 return 0;
118 }
119