• 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  * \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 custom filter and formatter factories for attributes.
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/core/ref.hpp>
23 #include <boost/bind/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/filter_parser.hpp>
33 #include <boost/log/utility/setup/formatter_parser.hpp>
34 
35 namespace logging = boost::log;
36 namespace attrs = boost::log::attributes;
37 namespace src = boost::log::sources;
38 
39 //! Enum for our custom severity levels
40 enum severity_level
41 {
42     normal,
43     notification,
44     warning,
45     error,
46     critical
47 };
48 
49 //! Formatting operator for severity levels
operator <<(std::ostream & strm,severity_level level)50 inline std::ostream& operator<< (std::ostream& strm, severity_level level)
51 {
52     switch (level)
53     {
54     case normal:
55         strm << "normal";
56         break;
57     case notification:
58         strm << "notification";
59         break;
60     case warning:
61         strm << "warning";
62         break;
63     case error:
64         strm << "error";
65         break;
66     case critical:
67         strm << "critical";
68         break;
69     default:
70         strm << static_cast< int >(level);
71         break;
72     }
73 
74     return strm;
75 }
76 
77 //! Parsing operator for severity levels
operator >>(std::istream & strm,severity_level & level)78 inline std::istream& operator>> (std::istream& strm, severity_level& level)
79 {
80     if (strm.good())
81     {
82         std::string str;
83         strm >> str;
84         if (str == "normal")
85             level = normal;
86         else if (str == "notification")
87             level = notification;
88         else if (str == "warning")
89             level = warning;
90         else if (str == "error")
91             level = error;
92         else if (str == "critical")
93             level = critical;
94         else
95             strm.setstate(std::ios_base::failbit);
96     }
97 
98     return strm;
99 }
100 
101 //! Our custom formatter for the scope list
102 struct scope_list_formatter
103 {
104     typedef void result_type;
105     typedef attrs::named_scope::value_type scope_stack;
106 
scope_list_formatterscope_list_formatter107     explicit scope_list_formatter(logging::attribute_name const& name) :
108         name_(name)
109     {
110     }
operator ()scope_list_formatter111     void operator()(logging::record_view const& rec, logging::formatting_ostream& strm) const
112     {
113         // We need to acquire the attribute value from the log record
114         logging::visit< scope_stack >
115         (
116             name_,
117             rec.attribute_values(),
118             boost::bind(&scope_list_formatter::format, boost::placeholders::_1, boost::ref(strm))
119         );
120     }
121 
122 private:
123     //! This is where our custom formatting takes place
formatscope_list_formatter124     static void format(scope_stack const& scopes, logging::formatting_ostream& strm)
125     {
126         scope_stack::const_iterator it = scopes.begin(), end = scopes.end();
127         for (; it != end; ++it)
128         {
129             strm << "\t" << it->scope_name << " [" << it->file_name << ":" << it->line << "]\n";
130         }
131     }
132 
133 private:
134     logging::attribute_name name_;
135 };
136 
137 class my_scopes_formatter_factory :
138     public logging::formatter_factory< char >
139 {
140 public:
141     /*!
142      * This function creates a formatter for the MyScopes attribute.
143      * It effectively associates the attribute with the scope_list_formatter class
144      */
create_formatter(logging::attribute_name const & attr_name,args_map const & args)145     formatter_type create_formatter(
146         logging::attribute_name const& attr_name, args_map const& args)
147     {
148         return formatter_type(scope_list_formatter(attr_name));
149     }
150 };
151 
152 //! The function initializes the logging library
init_logging()153 void init_logging()
154 {
155     // First thing - register the custom formatter for MyScopes
156     logging::register_formatter_factory("MyScopes", boost::make_shared< my_scopes_formatter_factory >());
157 
158     // Also register filter and formatter factories for our custom severity level enum. Since our operator<< and operator>> implement
159     // all required behavior, simple factories provided by Boost.Log will do.
160     logging::register_simple_filter_factory< severity_level >("Severity");
161     logging::register_simple_formatter_factory< severity_level, char >("Severity");
162 
163     // Then load the settings from the file
164     std::ifstream settings("settings.txt");
165     if (!settings.is_open())
166         throw std::runtime_error("Could not open settings.txt file");
167     logging::init_from_stream(settings);
168 
169     // Add some attributes. Note that severity level will be provided by the logger, so we don't need to add it here.
170     logging::add_common_attributes();
171 
172     logging::core::get()->add_global_attribute("MyScopes", attrs::named_scope());
173 }
174 
175 //! Global logger, which we will use to write log messages
BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(test_lg,src::severity_logger<severity_level>)176 BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(test_lg, src::severity_logger< severity_level >)
177 
178 //! The function tests logging
179 void try_logging()
180 {
181     BOOST_LOG_FUNCTION();
182 
183     src::severity_logger< severity_level >& lg = test_lg::get();
184 
185     BOOST_LOG_SEV(lg, critical) << "This is a critical severity record";
186 
187     BOOST_LOG_NAMED_SCOPE("random name");
188     BOOST_LOG_SEV(lg, error) << "This is a error severity record";
189 }
190 
main(int argc,char * argv[])191 int main(int argc, char* argv[])
192 {
193     try
194     {
195         init_logging();
196         try_logging();
197     }
198     catch (std::exception& e)
199     {
200         std::cout << "FAILURE: " << e.what() << std::endl;
201         return -1;
202     }
203 
204     return 0;
205 }
206