file.hpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 file.hpp
  9. * \author Andrey Semashev
  10. * \date 16.05.2008
  11. *
  12. * The header contains implementation of convenience functions for enabling logging to a file.
  13. */
  14. #ifndef BOOST_LOG_UTILITY_SETUP_FILE_HPP_INCLUDED_
  15. #define BOOST_LOG_UTILITY_SETUP_FILE_HPP_INCLUDED_
  16. #include <boost/type_traits/is_void.hpp>
  17. #include <boost/smart_ptr/shared_ptr.hpp>
  18. #include <boost/smart_ptr/make_shared_object.hpp>
  19. #include <boost/parameter/parameters.hpp> // for is_named_argument
  20. #include <boost/preprocessor/control/expr_if.hpp>
  21. #include <boost/preprocessor/comparison/greater.hpp>
  22. #include <boost/preprocessor/punctuation/comma_if.hpp>
  23. #include <boost/preprocessor/repetition/enum_params.hpp>
  24. #include <boost/preprocessor/repetition/enum_binary_params.hpp>
  25. #include <boost/preprocessor/repetition/enum_shifted_params.hpp>
  26. #include <boost/preprocessor/repetition/repeat_from_to.hpp>
  27. #include <boost/log/detail/config.hpp>
  28. #include <boost/log/detail/sink_init_helpers.hpp>
  29. #include <boost/log/detail/parameter_tools.hpp>
  30. #include <boost/log/core/core.hpp>
  31. #ifndef BOOST_LOG_NO_THREADS
  32. #include <boost/log/sinks/sync_frontend.hpp>
  33. #else
  34. #include <boost/log/sinks/unlocked_frontend.hpp>
  35. #endif
  36. #include <boost/log/sinks/text_file_backend.hpp>
  37. #include <boost/log/keywords/format.hpp>
  38. #include <boost/log/keywords/filter.hpp>
  39. #include <boost/log/keywords/scan_method.hpp>
  40. #include <boost/log/detail/header.hpp>
  41. #ifdef BOOST_HAS_PRAGMA_ONCE
  42. #pragma once
  43. #endif
  44. #ifndef BOOST_LOG_DOXYGEN_PASS
  45. #ifndef BOOST_LOG_NO_THREADS
  46. #define BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL sinks::synchronous_sink
  47. #else
  48. #define BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL sinks::unlocked_sink
  49. #endif
  50. #endif // BOOST_LOG_DOXYGEN_PASS
  51. namespace boost {
  52. BOOST_LOG_OPEN_NAMESPACE
  53. namespace aux {
  54. //! The function creates a file collector according to the specified arguments
  55. template< typename ArgsT >
  56. inline shared_ptr< sinks::file::collector > setup_file_collector(ArgsT const&, mpl::true_ const&)
  57. {
  58. return shared_ptr< sinks::file::collector >();
  59. }
  60. template< typename ArgsT >
  61. inline shared_ptr< sinks::file::collector > setup_file_collector(ArgsT const& args, mpl::false_ const&)
  62. {
  63. return sinks::file::make_collector(args);
  64. }
  65. //! The function constructs the sink and adds it to the core
  66. template< typename ArgsT >
  67. shared_ptr< BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL< sinks::text_file_backend > > add_file_log(ArgsT const& args)
  68. {
  69. typedef sinks::text_file_backend backend_t;
  70. shared_ptr< backend_t > pBackend = boost::make_shared< backend_t >(args);
  71. shared_ptr< sinks::file::collector > pCollector = aux::setup_file_collector(args,
  72. typename is_void< typename parameter::binding< ArgsT, keywords::tag::target, void >::type >::type());
  73. if (pCollector)
  74. {
  75. pBackend->set_file_collector(pCollector);
  76. pBackend->scan_for_files(args[keywords::scan_method | sinks::file::scan_matching]);
  77. }
  78. shared_ptr< BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL< backend_t > > pSink =
  79. boost::make_shared< BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL< backend_t > >(pBackend);
  80. aux::setup_filter(*pSink, args,
  81. typename is_void< typename parameter::binding< ArgsT, keywords::tag::filter, void >::type >::type());
  82. aux::setup_formatter(*pSink, args,
  83. typename is_void< typename parameter::binding< ArgsT, keywords::tag::format, void >::type >::type());
  84. core::get()->add_sink(pSink);
  85. return pSink;
  86. }
  87. //! The trait wraps the argument into a file_name named argument, if needed
  88. template< typename T, bool IsNamedArgument = parameter::aux::is_named_argument< T >::value >
  89. struct file_name_param_traits
  90. {
  91. static shared_ptr< BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL< sinks::text_file_backend > > wrap_add_file_log(T const& file_name_arg)
  92. {
  93. return aux::add_file_log(file_name_arg);
  94. }
  95. template< typename ArgsT >
  96. static shared_ptr< BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL< sinks::text_file_backend > > wrap_add_file_log(T const& file_name_arg, ArgsT const& args)
  97. {
  98. return aux::add_file_log((args, file_name_arg));
  99. }
  100. };
  101. template< typename T >
  102. struct file_name_param_traits< T, false >
  103. {
  104. static shared_ptr< BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL< sinks::text_file_backend > > wrap_add_file_log(T const& file_name_arg)
  105. {
  106. return aux::add_file_log(keywords::file_name = file_name_arg);
  107. }
  108. template< typename ArgsT >
  109. static shared_ptr< BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL< sinks::text_file_backend > > wrap_add_file_log(T const& file_name_arg, ArgsT const& args)
  110. {
  111. return aux::add_file_log((args, (keywords::file_name = file_name_arg)));
  112. }
  113. };
  114. } // namespace aux
  115. #ifndef BOOST_LOG_DOXYGEN_PASS
  116. #define BOOST_LOG_INIT_LOG_TO_FILE_INTERNAL(z, n, data)\
  117. template< BOOST_PP_ENUM_PARAMS_Z(z, n, typename T) >\
  118. inline shared_ptr< BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL< sinks::text_file_backend > > add_file_log(BOOST_PP_ENUM_BINARY_PARAMS_Z(z, n, T, const& arg))\
  119. {\
  120. return aux::file_name_param_traits< T0 >::wrap_add_file_log(\
  121. arg0\
  122. BOOST_PP_COMMA_IF(BOOST_PP_GREATER(n, 1))\
  123. BOOST_PP_EXPR_IF(BOOST_PP_GREATER(n, 1), (BOOST_PP_ENUM_SHIFTED_PARAMS_Z(z, n, arg)))\
  124. );\
  125. }
  126. BOOST_PP_REPEAT_FROM_TO(1, BOOST_LOG_MAX_PARAMETER_ARGS, BOOST_LOG_INIT_LOG_TO_FILE_INTERNAL, ~)
  127. #undef BOOST_LOG_INIT_LOG_TO_FILE_INTERNAL
  128. #else // BOOST_LOG_DOXYGEN_PASS
  129. /*!
  130. * The function initializes the logging library to write logs to a file stream.
  131. *
  132. * \param args A number of named arguments. The following parameters are supported:
  133. * \li \c file_name The active file name or its pattern. This parameter is mandatory.
  134. * \li \c target_file_name - Specifies the target file name pattern to use to rename the log file on rotation,
  135. * before passing it to the file collector. The pattern may contain the same
  136. * placeholders as the \c file_name parameter. By default, no renaming is done,
  137. * i.e. the written log file keeps its name according to \c file_name.
  138. * \li \c open_mode The mask that describes the open mode for the file. See <tt>std::ios_base::openmode</tt>.
  139. * \li \c rotation_size The size of the file at which rotation should occur. See <tt>basic_text_file_backend</tt>.
  140. * \li \c time_based_rotation The predicate for time-based file rotations. See <tt>basic_text_file_backend</tt>.
  141. * \li \c auto_flush A boolean flag that shows whether the sink should automatically flush the file
  142. * after each written record.
  143. * \li \c auto_newline_mode - Specifies automatic trailing newline insertion mode. Must be a value of
  144. * the \c auto_newline_mode enum. By default, is <tt>auto_newline_mode::insert_if_missing</tt>.
  145. * \li \c target The target directory to store rotated files in. Enables file collector and, if specified, limits associated
  146. * with the target directory. See <tt>sinks::file::make_collector</tt>.
  147. * \li \c max_size The maximum total size of rotated files in the target directory. See <tt>sinks::file::make_collector</tt>.
  148. * \li \c min_free_space Minimum free space in the target directory. See <tt>sinks::file::make_collector</tt>.
  149. * \li \c max_files The maximum total number of rotated files in the target directory. See <tt>sinks::file::make_collector</tt>.
  150. * \li \c scan_method The method of scanning the target directory for log files. See <tt>sinks::file::scan_method</tt>.
  151. * \li \c filter Specifies a filter to install into the sink. May be a string that represents a filter,
  152. * or a filter lambda expression.
  153. * \li \c format Specifies a formatter to install into the sink. May be a string that represents a formatter,
  154. * or a formatter lambda expression (either streaming or Boost.Format-like notation).
  155. *
  156. * \return Pointer to the constructed sink.
  157. *
  158. * \note The \c target named argument is required to enable the file collector and the limits associated with the target directory.
  159. * If the parameter is not specified, the file collector will not be created and the limits will not be maintained.
  160. */
  161. template< typename... ArgsT >
  162. shared_ptr< BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL< sinks::text_file_backend > > add_file_log(ArgsT... const& args);
  163. #endif // BOOST_LOG_DOXYGEN_PASS
  164. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  165. } // namespace boost
  166. #undef BOOST_LOG_FILE_SINK_FRONTEND_INTERNAL
  167. #include <boost/log/detail/footer.hpp>
  168. #endif // BOOST_LOG_UTILITY_SETUP_FILE_HPP_INCLUDED_