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