bind_to_log.hpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 bind_to_log.hpp
  9. * \author Andrey Semashev
  10. * \date 06.11.2012
  11. *
  12. * This header contains a function object that puts the received value to the bound stream using the \c to_log manipulator.
  13. * This is a lightweight alternative to what Boost.Phoenix and Boost.Lambda provides.
  14. */
  15. #ifndef BOOST_LOG_UTILITY_FUNCTIONAL_BIND_TO_LOG_HPP_INCLUDED_
  16. #define BOOST_LOG_UTILITY_FUNCTIONAL_BIND_TO_LOG_HPP_INCLUDED_
  17. #include <boost/log/detail/config.hpp>
  18. #include <boost/log/utility/functional/bind.hpp>
  19. #include <boost/log/utility/manipulators/to_log.hpp>
  20. #include <boost/log/detail/header.hpp>
  21. #ifdef BOOST_HAS_PRAGMA_ONCE
  22. #pragma once
  23. #endif
  24. namespace boost {
  25. BOOST_LOG_OPEN_NAMESPACE
  26. //! The function object that outputs its second operand to the first one
  27. template< typename TagT = void >
  28. struct to_log_fun
  29. {
  30. typedef void result_type;
  31. template< typename StreamT, typename T >
  32. void operator() (StreamT& strm, T const& val) const
  33. {
  34. strm << boost::log::to_log< TagT >(val);
  35. }
  36. };
  37. //! The function object that outputs its second operand to the first one
  38. template< >
  39. struct to_log_fun< void >
  40. {
  41. typedef void result_type;
  42. template< typename StreamT, typename T >
  43. void operator() (StreamT& strm, T const& val) const
  44. {
  45. strm << boost::log::to_log(val);
  46. }
  47. };
  48. template< typename StreamT >
  49. BOOST_FORCEINLINE binder1st< to_log_fun< >, StreamT& > bind_to_log(StreamT& strm)
  50. {
  51. return binder1st< to_log_fun< >, StreamT& >(to_log_fun< >(), strm);
  52. }
  53. template< typename TagT, typename StreamT >
  54. BOOST_FORCEINLINE binder1st< to_log_fun< TagT >, StreamT& > bind_to_log(StreamT& strm)
  55. {
  56. return binder1st< to_log_fun< TagT >, StreamT& >(to_log_fun< TagT >(), strm);
  57. }
  58. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  59. } // namespace boost
  60. #include <boost/log/detail/footer.hpp>
  61. #endif // BOOST_LOG_UTILITY_FUNCTIONAL_BIND_TO_LOG_HPP_INCLUDED_