/* * Copyright Andrey Semashev 2007 - 2015. * Distributed under the Boost Software License, Version 1.0. * (See accompanying file LICENSE_1_0.txt or copy at * http://www.boost.org/LICENSE_1_0.txt) */ /*! * \file file.hpp * \author Andrey Semashev * \date 16.05.2008 * * The header contains implementation of convenience functions for enabling logging to a file. */ #ifndef BOOST_LOG_UTILITY_SETUP_FILE_HPP_INCLUDED_ #define BOOST_LOG_UTILITY_SETUP_FILE_HPP_INCLUDED_ #include #include #include // for is_named_argument #include #include #include #include #include #include #include #include #include #include #ifndef BOOST_LOG_NO_THREADS #include #else #include #endif #include #include #include #ifdef BOOST_HAS_PRAGMA_ONCE #pragma once #endif #ifndef BOOST_LOG_DOXYGEN_PASS #ifndef BOOST_LOG_NO_THREADS #define BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL sinks::synchronous_sink #else #define BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL sinks::unlocked_sink #endif #endif // BOOST_LOG_DOXYGEN_PASS namespace boost { BOOST_LOG_OPEN_NAMESPACE namespace aux { //! The function creates a file collector according to the specified arguments template< typename ArgsT > inline shared_ptr< sinks::file::collector > setup_file_collector(ArgsT const&, mpl::true_ const&) { return shared_ptr< sinks::file::collector >(); } template< typename ArgsT > inline shared_ptr< sinks::file::collector > setup_file_collector(ArgsT const& args, mpl::false_ const&) { return sinks::file::make_collector(args); } //! The function constructs the sink and adds it to the core template< typename ArgsT > shared_ptr< BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL< sinks::text_file_backend > > add_file_log(ArgsT const& args) { typedef sinks::text_file_backend backend_t; shared_ptr< backend_t > pBackend = boost::make_shared< backend_t >(args); shared_ptr< sinks::file::collector > pCollector = aux::setup_file_collector(args, typename is_void< typename parameter::binding< ArgsT, keywords::tag::target, void >::type >::type()); if (pCollector) { pBackend->set_file_collector(pCollector); pBackend->scan_for_files(args[keywords::scan_method | sinks::file::scan_matching]); } shared_ptr< BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL< backend_t > > pSink = boost::make_shared< BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL< backend_t > >(pBackend); aux::setup_filter(*pSink, args, typename is_void< typename parameter::binding< ArgsT, keywords::tag::filter, void >::type >::type()); aux::setup_formatter(*pSink, args, typename is_void< typename parameter::binding< ArgsT, keywords::tag::format, void >::type >::type()); core::get()->add_sink(pSink); return pSink; } //! The function wraps the argument into a file_name named argument, if needed template< typename T > inline T const& wrap_file_name(T const& arg, mpl::true_) { return arg; } template< typename T > inline typename parameter::aux::tag< keywords::tag::file_name, T const >::type wrap_file_name(T const& arg, mpl::false_) { return keywords::file_name = arg; } } // namespace aux #ifndef BOOST_LOG_DOXYGEN_PASS #define BOOST_LOG_INIT_LOG_TO_FILE_INTERNAL(z, n, data)\ template< BOOST_PP_ENUM_PARAMS(n, typename T) >\ inline shared_ptr< BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL< sinks::text_file_backend > > add_file_log(BOOST_PP_ENUM_BINARY_PARAMS(n, T, const& arg))\ {\ return aux::add_file_log((\ aux::wrap_file_name(arg0, typename parameter::aux::is_named_argument< T0 >::type())\ BOOST_PP_COMMA_IF(BOOST_PP_GREATER(n, 1))\ BOOST_PP_ENUM_SHIFTED_PARAMS(n, arg)\ ));\ } BOOST_PP_REPEAT_FROM_TO(1, BOOST_LOG_MAX_PARAMETER_ARGS, BOOST_LOG_INIT_LOG_TO_FILE_INTERNAL, ~) #undef BOOST_LOG_INIT_LOG_TO_FILE_INTERNAL #else // BOOST_LOG_DOXYGEN_PASS /*! * The function initializes the logging library to write logs to a file stream. * * \param args A number of named arguments. The following parameters are supported: * \li \c file_name The file name or its pattern. This parameter is mandatory. * \li \c open_mode The mask that describes the open mode for the file. See std::ios_base::openmode. * \li \c rotation_size The size of the file at which rotation should occur. See basic_text_file_backend. * \li \c time_based_rotation The predicate for time-based file rotations. See basic_text_file_backend. * \li \c auto_flush A boolean flag that shows whether the sink should automatically flush the file * after each written record. * \li \c target The target directory to store rotated files in. See sinks::file::make_collector. * \li \c max_size The maximum total size of rotated files in the target directory. See sinks::file::make_collector. * \li \c min_free_space Minimum free space in the target directory. See sinks::file::make_collector. * \li \c max_files The maximum total number of rotated files in the target directory. See sinks::file::make_collector. * \li \c scan_method The method of scanning the target directory for log files. See sinks::file::scan_method. * \li \c filter Specifies a filter to install into the sink. May be a string that represents a filter, * or a filter lambda expression. * \li \c format Specifies a formatter to install into the sink. May be a string that represents a formatter, * or a formatter lambda expression (either streaming or Boost.Format-like notation). * \return Pointer to the constructed sink. */ template< typename... ArgsT > shared_ptr< BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL< sinks::text_file_backend > > add_file_log(ArgsT... const& args); #endif // BOOST_LOG_DOXYGEN_PASS BOOST_LOG_CLOSE_NAMESPACE // namespace log } // namespace boost #undef BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL #include #endif // BOOST_LOG_UTILITY_SETUP_FILE_HPP_INCLUDED_