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)
9 * \author Andrey Semashev
12 * \brief An example of initializing the library from a settings file,
13 * with a custom formatter for an attribute.
16 // #define BOOST_ALL_DYN_LINK 1
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>
34 namespace logging
= boost::log
;
35 namespace attrs
= boost::log::attributes
;
36 namespace src
= boost::log::sources
;
47 BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(test_lg
, src::severity_logger
< >)
49 //! Our custom formatter for the scope list
50 struct scope_list_formatter
52 typedef void result_type
;
53 typedef attrs::named_scope::value_type scope_stack
;
55 explicit scope_list_formatter(logging::attribute_name
const& name
) :
59 void operator()(logging::record_view
const& rec
, logging::formatting_ostream
& strm
) const
61 // We need to acquire the attribute value from the log record
62 logging::visit
< scope_stack
>
65 rec
.attribute_values(),
66 boost::bind(&scope_list_formatter::format
, _1
, boost::ref(strm
))
71 //! This is where our custom formatting takes place
72 static void format(scope_stack
const& scopes
, logging::formatting_ostream
& strm
)
74 scope_stack::const_iterator it
= scopes
.begin(), end
= scopes
.end();
75 for (; it
!= end
; ++it
)
77 strm
<< "\t" << it
->scope_name
<< " [" << it
->file_name
<< ":" << it
->line
<< "]\n";
82 logging::attribute_name name_
;
85 class my_scopes_formatter_factory
:
86 public logging::formatter_factory
< char >
90 * This function creates a formatter for the MyScopes attribute.
91 * It effectively associates the attribute with the scope_list_formatter class
93 formatter_type
create_formatter(
94 logging::attribute_name
const& attr_name
, args_map
const& args
)
96 return formatter_type(scope_list_formatter(attr_name
));
100 //! The function initializes the logging library
103 // First thing - register the custom formatter for MyScopes
104 logging::register_formatter_factory("MyScopes", boost::make_shared
< my_scopes_formatter_factory
>());
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
);
112 // Add some attributes
113 logging::add_common_attributes();
115 logging::core::get()->add_global_attribute("MyScopes", attrs::named_scope());
118 //! The function tests logging
121 BOOST_LOG_FUNCTION();
123 src::severity_logger
< >& lg
= test_lg::get();
125 BOOST_LOG_SEV(lg
, critical
) << "This is a critical severity record";
127 BOOST_LOG_NAMED_SCOPE("random name");
128 BOOST_LOG_SEV(lg
, error
) << "This is a error severity record";
131 int main(int argc
, char* argv
[])
138 catch (std::exception
& e
)
140 std::cout
<< "FAILURE: " << e
.what() << std::endl
;