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)
8 * \file init_from_settings.cpp
9 * \author Andrey Semashev
12 * \brief This header is the Boost.Log library implementation, see the library documentation
13 * at http://www.boost.org/doc/libs/release/libs/log/doc/html/index.html.
16 #ifndef BOOST_LOG_WITHOUT_SETTINGS_PARSERS
18 #if defined(__GNUC__) && !(defined(__INTEL_COMPILER) || defined(__ICL) || defined(__ICC) || defined(__ECC)) \
19 && (__GNUC__ * 100 + __GNUC_MINOR__) >= 407
20 // This warning is caused by a compiler bug which is exposed when boost::optional is used: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47679
21 // It has to be disabled here, before any code is included, since otherwise it doesn't help and the warning is still emitted.
22 // '*((void*)& foo +2)' may be used uninitialized in this function
23 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
26 #include <boost/log/detail/setup_config.hpp>
37 #include <boost/type.hpp>
38 #include <boost/bind.hpp>
39 #include <boost/limits.hpp>
40 #include <boost/cstdint.hpp>
41 #include <boost/smart_ptr/make_shared_object.hpp>
42 #include <boost/core/null_deleter.hpp>
43 #include <boost/optional/optional.hpp>
44 #include <boost/filesystem/path.hpp>
45 #include <boost/date_time/date_defs.hpp>
46 #include <boost/property_tree/ptree.hpp>
47 #include <boost/mpl/if.hpp>
48 #include <boost/type_traits/is_unsigned.hpp>
49 #include <boost/spirit/home/qi/numeric/numeric_utils.hpp>
50 #include <boost/log/detail/code_conversion.hpp>
51 #include <boost/log/detail/singleton.hpp>
52 #include <boost/log/detail/default_attribute_names.hpp>
53 #include <boost/log/core.hpp>
54 #include <boost/log/sinks.hpp>
55 #include <boost/log/exceptions.hpp>
56 #include <boost/log/sinks/auto_newline_mode.hpp>
57 #include <boost/log/sinks/frontend_requirements.hpp>
58 #include <boost/log/expressions/filter.hpp>
59 #include <boost/log/expressions/formatter.hpp>
60 #include <boost/log/utility/string_literal.hpp>
61 #include <boost/log/utility/functional/nop.hpp>
62 #include <boost/log/utility/setup/from_settings.hpp>
63 #include <boost/log/utility/setup/filter_parser.hpp>
64 #include <boost/log/utility/setup/formatter_parser.hpp>
65 #if !defined(BOOST_LOG_NO_ASIO)
66 #include <boost/asio/ip/address.hpp>
68 #if !defined(BOOST_LOG_NO_THREADS)
69 #include <boost/log/detail/locks.hpp>
70 #include <boost/log/detail/light_rw_mutex.hpp>
72 #include "parser_utils.hpp"
73 #include "spirit_encoding.hpp"
74 #include <boost/log/detail/header.hpp>
76 namespace qi = boost::spirit::qi;
80 BOOST_LOG_OPEN_NAMESPACE
82 BOOST_LOG_ANONYMOUS_NAMESPACE {
84 //! Throws an exception when a parameter value is not valid
85 BOOST_LOG_NORETURN void throw_invalid_value(const char* param_name)
87 std::string descr = std::string("Invalid parameter \"")
90 BOOST_LOG_THROW_DESCR(invalid_value, descr);
93 //! Extracts an integral value from parameter value
94 template< typename IntT, typename CharT >
95 inline IntT param_cast_to_int(const char* param_name, std::basic_string< CharT > const& value)
98 typedef typename mpl::if_<
100 qi::extract_uint< IntT, 10, 1, -1 >,
101 qi::extract_int< IntT, 10, 1, -1 >
103 const CharT* begin = value.c_str(), *end = begin + value.size();
104 if (extract::call(begin, end, res) && begin == end)
107 throw_invalid_value(param_name);
110 //! Case-insensitive character comparison predicate
111 struct is_case_insensitive_equal
113 typedef bool result_type;
115 template< typename CharT >
116 result_type operator() (CharT left, CharT right) const BOOST_NOEXCEPT
118 typedef typename boost::log::aux::encoding< CharT >::type encoding;
119 return encoding::tolower(left) == encoding::tolower(right);
123 //! Extracts a boolean value from parameter value
124 template< typename CharT >
125 inline bool param_cast_to_bool(const char* param_name, std::basic_string< CharT > const& value)
127 typedef CharT char_type;
128 typedef boost::log::aux::char_constants< char_type > constants;
129 typedef boost::log::basic_string_literal< char_type > literal_type;
131 const char_type* begin = value.c_str(), *end = begin + value.size();
132 std::size_t len = end - begin;
134 literal_type keyword = constants::true_keyword();
135 if (keyword.size() == len && std::equal(begin, end, keyword.c_str(), is_case_insensitive_equal()))
141 keyword = constants::false_keyword();
142 if (keyword.size() == len && std::equal(begin, end, keyword.c_str(), is_case_insensitive_equal()))
148 return param_cast_to_int< unsigned int >(param_name, value) != 0;
153 //! Extracts an \c auto_newline_mode value from parameter value
154 template< typename CharT >
155 inline sinks::auto_newline_mode param_cast_to_auto_newline_mode(const char* param_name, std::basic_string< CharT > const& value)
157 typedef CharT char_type;
158 typedef boost::log::aux::char_constants< char_type > constants;
160 if (value == constants::auto_newline_mode_disabled())
161 return sinks::disabled_auto_newline;
162 else if (value == constants::auto_newline_mode_always_insert())
163 return sinks::always_insert;
164 else if (value == constants::auto_newline_mode_insert_if_missing())
165 return sinks::insert_if_missing;
168 BOOST_LOG_THROW_DESCR(invalid_value,
169 "Auto newline mode \"" + boost::log::aux::to_narrow(value) + "\" is not supported");
173 #if !defined(BOOST_LOG_NO_ASIO)
174 //! Extracts a network address from parameter value
175 template< typename CharT >
176 inline std::string param_cast_to_address(const char* param_name, std::basic_string< CharT > const& value)
178 return log::aux::to_narrow(value);
180 #endif // !defined(BOOST_LOG_NO_ASIO)
182 template< typename CharT >
183 inline bool is_weekday(const CharT* str, std::size_t len, boost::log::basic_string_literal< CharT > const& weekday, boost::log::basic_string_literal< CharT > const& short_weekday)
185 return (len == weekday.size() && std::equal(weekday.begin(), weekday.end(), str)) ||
186 (len == short_weekday.size() && std::equal(short_weekday.begin(), short_weekday.end(), str));
189 //! The function extracts the file rotation time point predicate from the parameter
190 template< typename CharT >
191 sinks::file::rotation_at_time_point param_cast_to_rotation_time_point(const char* param_name, std::basic_string< CharT > const& value)
193 typedef CharT char_type;
194 typedef boost::log::aux::char_constants< char_type > constants;
195 typedef typename boost::log::aux::encoding< char_type >::type encoding;
196 typedef qi::extract_uint< unsigned short, 10, 1, 2 > day_extract;
197 typedef qi::extract_uint< unsigned char, 10, 2, 2 > time_component_extract;
199 const char_type colon = static_cast< char_type >(':');
200 optional< date_time::weekdays > weekday;
201 optional< unsigned short > day;
202 unsigned char hour = 0, minute = 0, second = 0;
203 const char_type* begin = value.c_str(), *end = begin + value.size();
205 if (!encoding::isalnum(*begin)) // begin is null-terminated, so we also check that the string is not empty here
206 throw_invalid_value(param_name);
208 const char_type* p = begin + 1;
209 if (encoding::isalpha(*begin))
211 // This must be a weekday
212 while (encoding::isalpha(*p))
215 std::size_t len = p - begin;
216 if (is_weekday(begin, len, constants::monday_keyword(), constants::short_monday_keyword()))
217 weekday = date_time::Monday;
218 else if (is_weekday(begin, len, constants::tuesday_keyword(), constants::short_tuesday_keyword()))
219 weekday = date_time::Tuesday;
220 else if (is_weekday(begin, len, constants::wednesday_keyword(), constants::short_wednesday_keyword()))
221 weekday = date_time::Wednesday;
222 else if (is_weekday(begin, len, constants::thursday_keyword(), constants::short_thursday_keyword()))
223 weekday = date_time::Thursday;
224 else if (is_weekday(begin, len, constants::friday_keyword(), constants::short_friday_keyword()))
225 weekday = date_time::Friday;
226 else if (is_weekday(begin, len, constants::saturday_keyword(), constants::short_saturday_keyword()))
227 weekday = date_time::Saturday;
228 else if (is_weekday(begin, len, constants::sunday_keyword(), constants::short_sunday_keyword()))
229 weekday = date_time::Sunday;
231 throw_invalid_value(param_name);
235 // This may be either a month day or an hour
236 while (encoding::isdigit(*p))
239 if (encoding::isspace(*p))
241 // This is a month day
242 unsigned short mday = 0;
243 const char_type* b = begin;
244 if (!day_extract::call(b, p, mday) || b != p)
245 throw_invalid_value(param_name);
249 else if (*p == colon)
251 // This is an hour, reset the pointer
255 throw_invalid_value(param_name);
259 while (encoding::isspace(*p))
263 if (!time_component_extract::call(p, end, hour) || *p != colon)
264 throw_invalid_value(param_name);
268 if (!time_component_extract::call(p, end, minute) || *p != colon)
269 throw_invalid_value(param_name);
273 if (!time_component_extract::call(p, end, second) || p != end)
274 throw_invalid_value(param_name);
276 // Construct the predicate
278 return sinks::file::rotation_at_time_point(weekday.get(), hour, minute, second);
280 return sinks::file::rotation_at_time_point(gregorian::greg_day(day.get()), hour, minute, second);
282 return sinks::file::rotation_at_time_point(hour, minute, second);
285 //! Base class for default sink factories
286 template< typename CharT >
287 class basic_default_sink_factory :
288 public sink_factory< CharT >
291 typedef sink_factory< CharT > base_type;
292 typedef typename base_type::char_type char_type;
293 typedef typename base_type::string_type string_type;
294 typedef typename base_type::settings_section settings_section;
295 typedef boost::log::aux::char_constants< char_type > constants;
298 //! Sink backend character selection function
299 template< typename InitializerT >
300 static shared_ptr< sinks::sink > select_backend_character_type(settings_section const& params, InitializerT initializer)
302 #if defined(BOOST_LOG_USE_CHAR) && defined(BOOST_LOG_USE_WCHAR_T)
303 if (optional< string_type > wide_param = params["Wide"])
305 if (param_cast_to_bool("Wide", wide_param.get()))
306 return initializer(params, type< wchar_t >());
309 return initializer(params, type< char >());
310 #elif defined(BOOST_LOG_USE_CHAR)
311 return initializer(params, type< char >());
312 #elif defined(BOOST_LOG_USE_WCHAR_T)
313 return initializer(params, type< wchar_t >());
317 //! The function initializes common parameters of a formatting sink and returns the constructed sink
318 template< typename BackendT >
319 static shared_ptr< sinks::sink > init_sink(shared_ptr< BackendT > const& backend, settings_section const& params)
321 typedef BackendT backend_t;
322 typedef typename sinks::has_requirement<
323 typename backend_t::frontend_requirements,
324 sinks::formatted_records
325 >::type is_formatting_t;
329 if (optional< string_type > filter_param = params["Filter"])
331 filt = parse_filter(filter_param.get());
334 shared_ptr< sinks::basic_sink_frontend > p;
336 #if !defined(BOOST_LOG_NO_THREADS)
337 // Asynchronous. TODO: make it more flexible.
339 if (optional< string_type > async_param = params["Asynchronous"])
341 async = param_cast_to_bool("Asynchronous", async_param.get());
344 // Construct the frontend, considering Asynchronous parameter
347 p = init_formatter(boost::make_shared< sinks::synchronous_sink< backend_t > >(backend), params, is_formatting_t());
351 p = init_formatter(boost::make_shared< sinks::asynchronous_sink< backend_t > >(backend), params, is_formatting_t());
353 // https://svn.boost.org/trac/boost/ticket/10638
354 // The user doesn't have a way to process excaptions from the dedicated thread anyway, so just suppress them instead of
355 // terminating the application.
356 p->set_exception_handler(nop());
359 // When multithreading is disabled we always use the unlocked sink frontend
360 p = init_formatter(boost::make_shared< sinks::unlocked_sink< backend_t > >(backend), params, is_formatting_t());
369 //! The function initializes formatter for the sinks that support formatting
370 template< typename SinkT >
371 static shared_ptr< SinkT > init_formatter(shared_ptr< SinkT > const& sink, settings_section const& params, mpl::true_)
374 if (optional< string_type > format_param = params["Format"])
376 typedef typename SinkT::char_type sink_char_type;
377 std::basic_string< sink_char_type > format_str;
378 log::aux::code_convert(format_param.get(), format_str);
379 sink->set_formatter(parse_formatter(format_str));
383 template< typename SinkT >
384 static shared_ptr< SinkT > init_formatter(shared_ptr< SinkT > const& sink, settings_section const& params, mpl::false_)
390 //! Default console sink factory
391 template< typename CharT >
392 class default_console_sink_factory :
393 public basic_default_sink_factory< CharT >
396 typedef basic_default_sink_factory< CharT > base_type;
397 typedef typename base_type::char_type char_type;
398 typedef typename base_type::string_type string_type;
399 typedef typename base_type::settings_section settings_section;
400 typedef typename base_type::constants constants;
407 typedef shared_ptr< sinks::sink > result_type;
409 template< typename BackendCharT >
410 result_type operator() (settings_section const& params, type< BackendCharT >) const
412 // Construct the backend
413 typedef boost::log::aux::char_constants< BackendCharT > constants;
414 typedef sinks::basic_text_ostream_backend< BackendCharT > backend_t;
415 shared_ptr< backend_t > backend = boost::make_shared< backend_t >();
416 backend->add_stream(shared_ptr< typename backend_t::stream_type >(&constants::get_console_log_stream(), boost::null_deleter()));
419 if (optional< string_type > auto_newline_param = params["AutoNewline"])
421 backend->set_auto_newline_mode(param_cast_to_auto_newline_mode("AutoNewline", auto_newline_param.get()));
425 if (optional< string_type > auto_flush_param = params["AutoFlush"])
427 backend->auto_flush(param_cast_to_bool("AutoFlush", auto_flush_param.get()));
430 return base_type::init_sink(backend, params);
435 //! The function constructs a sink that writes log records to the console
436 shared_ptr< sinks::sink > create_sink(settings_section const& params)
438 return base_type::select_backend_character_type(params, impl());
442 //! Default text file sink factory
443 template< typename CharT >
444 class default_text_file_sink_factory :
445 public basic_default_sink_factory< CharT >
448 typedef basic_default_sink_factory< CharT > base_type;
449 typedef typename base_type::char_type char_type;
450 typedef typename base_type::string_type string_type;
451 typedef typename base_type::settings_section settings_section;
452 typedef typename base_type::constants constants;
455 //! The function constructs a sink that writes log records to a text file
456 shared_ptr< sinks::sink > create_sink(settings_section const& params)
458 typedef sinks::text_file_backend backend_t;
459 shared_ptr< backend_t > backend = boost::make_shared< backend_t >();
462 if (optional< string_type > file_name_param = params["FileName"])
464 backend->set_file_name_pattern(filesystem::path(file_name_param.get()));
467 BOOST_LOG_THROW_DESCR(missing_value, "File name is not specified");
470 if (optional< string_type > target_file_name_param = params["TargetFileName"])
472 backend->set_target_file_name_pattern(filesystem::path(target_file_name_param.get()));
475 // File rotation size
476 if (optional< string_type > rotation_size_param = params["RotationSize"])
478 backend->set_rotation_size(param_cast_to_int< uintmax_t >("RotationSize", rotation_size_param.get()));
481 // File rotation interval
482 if (optional< string_type > rotation_interval_param = params["RotationInterval"])
484 backend->set_time_based_rotation(sinks::file::rotation_at_time_interval(
485 posix_time::seconds(param_cast_to_int< unsigned int >("RotationInterval", rotation_interval_param.get()))));
487 else if (optional< string_type > rotation_time_point_param = params["RotationTimePoint"])
489 // File rotation time point
490 backend->set_time_based_rotation(param_cast_to_rotation_time_point("RotationTimePoint", rotation_time_point_param.get()));
494 if (optional< string_type > enable_final_rotation_param = params["EnableFinalRotation"])
496 backend->enable_final_rotation(param_cast_to_bool("EnableFinalRotation", enable_final_rotation_param.get()));
500 if (optional< string_type > auto_newline_param = params["AutoNewline"])
502 backend->set_auto_newline_mode(param_cast_to_auto_newline_mode("AutoNewline", auto_newline_param.get()));
506 if (optional< string_type > auto_flush_param = params["AutoFlush"])
508 backend->auto_flush(param_cast_to_bool("AutoFlush", auto_flush_param.get()));
512 if (optional< string_type > append_param = params["Append"])
514 if (param_cast_to_bool("Append", append_param.get()))
515 backend->set_open_mode(std::ios_base::out | std::ios_base::app);
518 // File collector parameters
520 if (optional< string_type > target_param = params["Target"])
522 filesystem::path target_dir(target_param.get());
525 uintmax_t max_size = (std::numeric_limits< uintmax_t >::max)();
526 if (optional< string_type > max_size_param = params["MaxSize"])
527 max_size = param_cast_to_int< uintmax_t >("MaxSize", max_size_param.get());
531 if (optional< string_type > min_space_param = params["MinFreeSpace"])
532 space = param_cast_to_int< uintmax_t >("MinFreeSpace", min_space_param.get());
534 // Max number of files
535 uintmax_t max_files = (std::numeric_limits< uintmax_t >::max)();
536 if (optional< string_type > max_files_param = params["MaxFiles"])
537 max_files = param_cast_to_int< uintmax_t >("MaxFiles", max_files_param.get());
539 backend->set_file_collector(sinks::file::make_collector(
540 keywords::target = target_dir,
541 keywords::max_size = max_size,
542 keywords::min_free_space = space,
543 keywords::max_files = max_files));
545 // Scan for log files
546 if (optional< string_type > scan_param = params["ScanForFiles"])
548 string_type const& value = scan_param.get();
549 if (value == constants::scan_method_all())
550 backend->scan_for_files(sinks::file::scan_all);
551 else if (value == constants::scan_method_matching())
552 backend->scan_for_files(sinks::file::scan_matching);
555 BOOST_LOG_THROW_DESCR(invalid_value,
556 "File scan method \"" + boost::log::aux::to_narrow(value) + "\" is not supported");
561 return base_type::init_sink(backend, params);
565 #ifndef BOOST_LOG_WITHOUT_SYSLOG
567 //! Default syslog sink factory
568 template< typename CharT >
569 class default_syslog_sink_factory :
570 public basic_default_sink_factory< CharT >
573 typedef basic_default_sink_factory< CharT > base_type;
574 typedef typename base_type::char_type char_type;
575 typedef typename base_type::string_type string_type;
576 typedef typename base_type::settings_section settings_section;
577 typedef typename base_type::constants constants;
580 //! The function constructs a sink that writes log records to syslog
581 shared_ptr< sinks::sink > create_sink(settings_section const& params)
583 // Construct the backend
584 typedef sinks::syslog_backend backend_t;
585 shared_ptr< backend_t > backend = boost::make_shared< backend_t >();
587 // For now we use only the default level mapping. Will add support for configuration later.
588 backend->set_severity_mapper(sinks::syslog::direct_severity_mapping< >(log::aux::default_attribute_names::severity()));
590 #if !defined(BOOST_LOG_NO_ASIO)
591 // Setup local and remote addresses
592 if (optional< string_type > local_address_param = params["LocalAddress"])
593 backend->set_local_address(param_cast_to_address("LocalAddress", local_address_param.get()));
595 if (optional< string_type > target_address_param = params["TargetAddress"])
596 backend->set_target_address(param_cast_to_address("TargetAddress", target_address_param.get()));
597 #endif // !defined(BOOST_LOG_NO_ASIO)
599 return base_type::init_sink(backend, params);
603 #endif // !defined(BOOST_LOG_WITHOUT_SYSLOG)
605 #ifndef BOOST_LOG_WITHOUT_DEBUG_OUTPUT
607 //! Default debugger sink factory
608 template< typename CharT >
609 class default_debugger_sink_factory :
610 public basic_default_sink_factory< CharT >
613 typedef basic_default_sink_factory< CharT > base_type;
614 typedef typename base_type::char_type char_type;
615 typedef typename base_type::string_type string_type;
616 typedef typename base_type::settings_section settings_section;
617 typedef typename base_type::constants constants;
624 typedef shared_ptr< sinks::sink > result_type;
626 template< typename BackendCharT >
627 result_type operator() (settings_section const& params, type< BackendCharT >) const
629 // Construct the backend
630 typedef sinks::basic_debug_output_backend< BackendCharT > backend_t;
631 shared_ptr< backend_t > backend = boost::make_shared< backend_t >();
633 return base_type::init_sink(backend, params);
638 //! The function constructs a sink that writes log records to the debugger
639 shared_ptr< sinks::sink > create_sink(settings_section const& params)
641 return base_type::select_backend_character_type(params, impl());
645 #endif // !defined(BOOST_LOG_WITHOUT_DEBUG_OUTPUT)
647 #ifndef BOOST_LOG_WITHOUT_EVENT_LOG
649 //! Default simple event log sink factory
650 template< typename CharT >
651 class default_simple_event_log_sink_factory :
652 public basic_default_sink_factory< CharT >
655 typedef basic_default_sink_factory< CharT > base_type;
656 typedef typename base_type::char_type char_type;
657 typedef typename base_type::string_type string_type;
658 typedef typename base_type::settings_section settings_section;
659 typedef typename base_type::constants constants;
666 typedef shared_ptr< sinks::sink > result_type;
668 template< typename BackendCharT >
669 result_type operator() (settings_section const& params, type< BackendCharT >) const
671 typedef sinks::basic_simple_event_log_backend< BackendCharT > backend_t;
672 typedef typename backend_t::string_type backend_string_type;
674 // Determine the log name
675 backend_string_type log_name;
676 if (optional< string_type > log_name_param = params["LogName"])
677 log::aux::code_convert(log_name_param.get(), log_name);
679 log_name = backend_t::get_default_log_name();
681 // Determine the log source name
682 backend_string_type source_name;
683 if (optional< string_type > log_source_param = params["LogSource"])
684 log::aux::code_convert(log_source_param.get(), source_name);
686 source_name = backend_t::get_default_source_name();
688 // Determine the registration mode
689 sinks::event_log::registration_mode reg_mode = sinks::event_log::on_demand;
690 if (optional< string_type > registration_param = params["Registration"])
692 string_type const& value = registration_param.get();
693 if (value == constants::registration_never())
694 reg_mode = sinks::event_log::never;
695 else if (value == constants::registration_on_demand())
696 reg_mode = sinks::event_log::on_demand;
697 else if (value == constants::registration_forced())
698 reg_mode = sinks::event_log::forced;
701 BOOST_LOG_THROW_DESCR(invalid_value,
702 "The registration mode \"" + log::aux::to_narrow(value) + "\" is not supported");
706 // Construct the backend
707 shared_ptr< backend_t > backend(boost::make_shared< backend_t >((
708 keywords::log_name = log_name,
709 keywords::log_source = source_name,
710 keywords::registration = reg_mode)));
712 // For now we use only the default event type mapping. Will add support for configuration later.
713 backend->set_event_type_mapper(sinks::event_log::direct_event_type_mapping< >(log::aux::default_attribute_names::severity()));
715 return base_type::init_sink(backend, params);
720 //! The function constructs a sink that writes log records to the Windows NT Event Log
721 shared_ptr< sinks::sink > create_sink(settings_section const& params)
723 return base_type::select_backend_character_type(params, impl());
727 #endif // !defined(BOOST_LOG_WITHOUT_EVENT_LOG)
730 //! The supported sinks repository
731 template< typename CharT >
732 struct sinks_repository :
733 public log::aux::lazy_singleton< sinks_repository< CharT > >
735 typedef log::aux::lazy_singleton< sinks_repository< CharT > > base_type;
737 #if !defined(BOOST_LOG_BROKEN_FRIEND_TEMPLATE_SPECIALIZATIONS)
738 friend class log::aux::lazy_singleton< sinks_repository< CharT > >;
740 friend class base_type;
743 typedef CharT char_type;
744 typedef std::basic_string< char_type > string_type;
745 typedef basic_settings_section< char_type > settings_section;
746 typedef boost::log::aux::char_constants< char_type > constants;
747 typedef boost::shared_ptr< sink_factory< char_type > > sink_factory_ptr;
748 typedef std::map< std::string, sink_factory_ptr > sink_factories;
750 #if !defined(BOOST_LOG_NO_THREADS)
751 //! Synchronization mutex
752 log::aux::light_rw_mutex m_Mutex;
754 //! Map of the sink factories
755 sink_factories m_Factories;
757 //! The function constructs a sink from the settings
758 shared_ptr< sinks::sink > construct_sink_from_settings(settings_section const& params)
760 typedef typename settings_section::const_reference param_const_reference;
761 if (param_const_reference dest_node = params["Destination"])
763 std::string dest = log::aux::to_narrow(dest_node.get().get());
765 BOOST_LOG_EXPR_IF_MT(log::aux::shared_lock_guard< log::aux::light_rw_mutex > lock(m_Mutex);)
766 typename sink_factories::const_iterator it = m_Factories.find(dest);
767 if (it != m_Factories.end())
769 return it->second->create_sink(params);
773 BOOST_LOG_THROW_DESCR(invalid_value, "The sink destination is not supported: " + dest);
778 BOOST_LOG_THROW_DESCR(missing_value, "The sink destination is not set");
782 static void init_instance()
784 sinks_repository& instance = base_type::get_instance();
785 instance.m_Factories["TextFile"] = boost::make_shared< default_text_file_sink_factory< char_type > >();
786 instance.m_Factories["Console"] = boost::make_shared< default_console_sink_factory< char_type > >();
787 #ifndef BOOST_LOG_WITHOUT_SYSLOG
788 instance.m_Factories["Syslog"] = boost::make_shared< default_syslog_sink_factory< char_type > >();
790 #ifndef BOOST_LOG_WITHOUT_DEBUG_OUTPUT
791 instance.m_Factories["Debugger"] = boost::make_shared< default_debugger_sink_factory< char_type > >();
793 #ifndef BOOST_LOG_WITHOUT_EVENT_LOG
794 instance.m_Factories["SimpleEventLog"] = boost::make_shared< default_simple_event_log_sink_factory< char_type > >();
799 sinks_repository() {}
802 //! The function applies the settings to the logging core
803 template< typename CharT >
804 void apply_core_settings(basic_settings_section< CharT > const& params)
806 typedef CharT char_type;
807 typedef std::basic_string< char_type > string_type;
809 core_ptr core = boost::log::core::get();
812 if (optional< string_type > filter_param = params["Filter"])
813 core->set_filter(parse_filter(filter_param.get()));
815 core->reset_filter();
818 if (optional< string_type > disable_logging_param = params["DisableLogging"])
819 core->set_logging_enabled(!param_cast_to_bool("DisableLogging", disable_logging_param.get()));
821 core->set_logging_enabled(true);
827 //! The function initializes the logging library from a settings container
828 template< typename CharT >
829 BOOST_LOG_SETUP_API void init_from_settings(basic_settings_section< CharT > const& setts)
831 typedef basic_settings_section< CharT > section;
832 typedef typename section::char_type char_type;
833 typedef sinks_repository< char_type > sinks_repo_t;
835 // Apply core settings
836 if (section core_params = setts["Core"])
837 apply_core_settings(core_params);
839 // Construct and initialize sinks
840 if (section sink_params = setts["Sinks"])
842 sinks_repo_t& sinks_repo = sinks_repo_t::get();
843 std::vector< shared_ptr< sinks::sink > > new_sinks;
845 for (typename section::const_iterator it = sink_params.begin(), end = sink_params.end(); it != end; ++it)
847 section sink_params = *it;
849 // Ignore empty sections as they are most likely individual parameters (which should not be here anyway)
850 if (!sink_params.empty())
852 new_sinks.push_back(sinks_repo.construct_sink_from_settings(sink_params));
856 std::for_each(new_sinks.begin(), new_sinks.end(), boost::bind(&core::add_sink, core::get(), _1));
861 //! The function registers a factory for a sink
862 template< typename CharT >
863 BOOST_LOG_SETUP_API void register_sink_factory(const char* sink_name, shared_ptr< sink_factory< CharT > > const& factory)
865 sinks_repository< CharT >& repo = sinks_repository< CharT >::get();
866 BOOST_LOG_EXPR_IF_MT(lock_guard< log::aux::light_rw_mutex > lock(repo.m_Mutex);)
867 repo.m_Factories[sink_name] = factory;
870 #ifdef BOOST_LOG_USE_CHAR
871 template BOOST_LOG_SETUP_API void register_sink_factory< char >(const char* sink_name, shared_ptr< sink_factory< char > > const& factory);
872 template BOOST_LOG_SETUP_API void init_from_settings< char >(basic_settings_section< char > const& setts);
875 #ifdef BOOST_LOG_USE_WCHAR_T
876 template BOOST_LOG_SETUP_API void register_sink_factory< wchar_t >(const char* sink_name, shared_ptr< sink_factory< wchar_t > > const& factory);
877 template BOOST_LOG_SETUP_API void init_from_settings< wchar_t >(basic_settings_section< wchar_t > const& setts);
880 BOOST_LOG_CLOSE_NAMESPACE // namespace log
884 #include <boost/log/detail/footer.hpp>
886 #endif // BOOST_LOG_WITHOUT_SETTINGS_PARSERS