]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/log/example/settings_file_formatter_factory/main.cpp
Add patch for failing prerm scripts
[ceph.git] / ceph / src / boost / libs / log / example / settings_file_formatter_factory / main.cpp
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 12.05.2010
11 *
12 * \brief An example of initializing the library from a settings file,
13 * with a custom formatter for an attribute.
14 */
15
16 // #define BOOST_ALL_DYN_LINK 1
17
18 #include <string>
19 #include <iostream>
20 #include <fstream>
21 #include <stdexcept>
22 #include <boost/ref.hpp>
23 #include <boost/bind.hpp>
24 #include <boost/smart_ptr/make_shared_object.hpp>
25 #include <boost/log/core.hpp>
26 #include <boost/log/common.hpp>
27 #include <boost/log/attributes.hpp>
28 #include <boost/log/core/record.hpp>
29 #include <boost/log/attributes/value_visitation.hpp>
30 #include <boost/log/utility/setup/from_stream.hpp>
31 #include <boost/log/utility/setup/common_attributes.hpp>
32 #include <boost/log/utility/setup/formatter_parser.hpp>
33
34 namespace logging = boost::log;
35 namespace attrs = boost::log::attributes;
36 namespace src = boost::log::sources;
37
38 enum severity_level
39 {
40 normal,
41 notification,
42 warning,
43 error,
44 critical
45 };
46
47 BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(test_lg, src::severity_logger< >)
48
49 //! Our custom formatter for the scope list
50 struct scope_list_formatter
51 {
52 typedef void result_type;
53 typedef attrs::named_scope::value_type scope_stack;
54
55 explicit scope_list_formatter(logging::attribute_name const& name) :
56 name_(name)
57 {
58 }
59 void operator()(logging::record_view const& rec, logging::formatting_ostream& strm) const
60 {
61 // We need to acquire the attribute value from the log record
62 logging::visit< scope_stack >
63 (
64 name_,
65 rec.attribute_values(),
66 boost::bind(&scope_list_formatter::format, _1, boost::ref(strm))
67 );
68 }
69
70 private:
71 //! This is where our custom formatting takes place
72 static void format(scope_stack const& scopes, logging::formatting_ostream& strm)
73 {
74 scope_stack::const_iterator it = scopes.begin(), end = scopes.end();
75 for (; it != end; ++it)
76 {
77 strm << "\t" << it->scope_name << " [" << it->file_name << ":" << it->line << "]\n";
78 }
79 }
80
81 private:
82 logging::attribute_name name_;
83 };
84
85 class my_scopes_formatter_factory :
86 public logging::formatter_factory< char >
87 {
88 public:
89 /*!
90 * This function creates a formatter for the MyScopes attribute.
91 * It effectively associates the attribute with the scope_list_formatter class
92 */
93 formatter_type create_formatter(
94 logging::attribute_name const& attr_name, args_map const& args)
95 {
96 return formatter_type(scope_list_formatter(attr_name));
97 }
98 };
99
100 //! The function initializes the logging library
101 void init_logging()
102 {
103 // First thing - register the custom formatter for MyScopes
104 logging::register_formatter_factory("MyScopes", boost::make_shared< my_scopes_formatter_factory >());
105
106 // Then load the settings from the file
107 std::ifstream settings("settings.txt");
108 if (!settings.is_open())
109 throw std::runtime_error("Could not open settings.txt file");
110 logging::init_from_stream(settings);
111
112 // Add some attributes
113 logging::add_common_attributes();
114
115 logging::core::get()->add_global_attribute("MyScopes", attrs::named_scope());
116 }
117
118 //! The function tests logging
119 void try_logging()
120 {
121 BOOST_LOG_FUNCTION();
122
123 src::severity_logger< >& lg = test_lg::get();
124
125 BOOST_LOG_SEV(lg, critical) << "This is a critical severity record";
126
127 BOOST_LOG_NAMED_SCOPE("random name");
128 BOOST_LOG_SEV(lg, error) << "This is a error severity record";
129 }
130
131 int main(int argc, char* argv[])
132 {
133 try
134 {
135 init_logging();
136 try_logging();
137 }
138 catch (std::exception& e)
139 {
140 std::cout << "FAILURE: " << e.what() << std::endl;
141 return -1;
142 }
143
144 return 0;
145 }