trivial.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 log/trivial.hpp
  9. * \author Andrey Semashev
  10. * \date 07.11.2009
  11. *
  12. * This header defines tools for trivial logging support
  13. */
  14. #ifndef BOOST_LOG_TRIVIAL_HPP_INCLUDED_
  15. #define BOOST_LOG_TRIVIAL_HPP_INCLUDED_
  16. #include <iosfwd>
  17. #include <ostream>
  18. #include <boost/log/detail/config.hpp>
  19. #include <boost/log/keywords/severity.hpp>
  20. #include <boost/log/sources/severity_logger.hpp>
  21. #include <boost/log/sources/record_ostream.hpp>
  22. #include <boost/log/detail/header.hpp>
  23. #ifdef BOOST_HAS_PRAGMA_ONCE
  24. #pragma once
  25. #endif
  26. #if !defined(BOOST_LOG_USE_CHAR)
  27. #error Boost.Log: Trivial logging is available for narrow-character builds only. Use advanced initialization routines to setup wide-character logging.
  28. #endif
  29. namespace boost {
  30. BOOST_LOG_OPEN_NAMESPACE
  31. namespace trivial {
  32. //! Trivial severity levels
  33. enum severity_level
  34. {
  35. trace,
  36. debug,
  37. info,
  38. warning,
  39. error,
  40. fatal
  41. };
  42. //! Returns stringized enumeration value or \c NULL, if the value is not valid
  43. BOOST_LOG_API const char* to_string(severity_level lvl);
  44. //! Outputs stringized representation of the severity level to the stream
  45. template< typename CharT, typename TraitsT >
  46. inline std::basic_ostream< CharT, TraitsT >& operator<< (
  47. std::basic_ostream< CharT, TraitsT >& strm, severity_level lvl)
  48. {
  49. const char* str = boost::log::trivial::to_string(lvl);
  50. if (str)
  51. strm << str;
  52. else
  53. strm << static_cast< int >(lvl);
  54. return strm;
  55. }
  56. //! Reads stringized representation of the severity level from the stream
  57. template< typename CharT, typename TraitsT >
  58. BOOST_LOG_API std::basic_istream< CharT, TraitsT >& operator>> (
  59. std::basic_istream< CharT, TraitsT >& strm, severity_level& lvl);
  60. //! Trivial logger type
  61. #if !defined(BOOST_LOG_NO_THREADS)
  62. typedef sources::severity_logger_mt< severity_level > logger_type;
  63. #else
  64. typedef sources::severity_logger< severity_level > logger_type;
  65. #endif
  66. /*!
  67. * \brief Trivial logger tag
  68. *
  69. * This tag can be used to acquire the logger that is used with lrivial logging macros.
  70. * This may be useful when the logger is used with other macros which require a logger.
  71. */
  72. struct logger
  73. {
  74. //! Logger type
  75. typedef trivial::logger_type logger_type;
  76. /*!
  77. * Returns a reference to the trivial logger instance
  78. */
  79. static BOOST_LOG_API logger_type& get();
  80. // Implementation details - never use these
  81. #if !defined(BOOST_LOG_DOXYGEN_PASS)
  82. enum registration_line_t { registration_line = __LINE__ };
  83. static const char* registration_file() { return __FILE__; }
  84. static BOOST_LOG_API logger_type construct_logger();
  85. #endif
  86. };
  87. /*!
  88. * The macro is used to initiate logging. The \c lvl argument of the macro specifies one of the following
  89. * severity levels: \c trace, \c debug, \c info, \c warning, \c error or \c fatal (see \c severity_level enum).
  90. * Following the macro, there may be a streaming expression that composes the record message string. For example:
  91. *
  92. * \code
  93. * BOOST_LOG_TRIVIAL(info) << "Hello, world!";
  94. * \endcode
  95. */
  96. #define BOOST_LOG_TRIVIAL(lvl)\
  97. BOOST_LOG_STREAM_WITH_PARAMS(::boost::log::trivial::logger::get(),\
  98. (::boost::log::keywords::severity = ::boost::log::trivial::lvl))
  99. } // namespace trivial
  100. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  101. } // namespace boost
  102. #include <boost/log/detail/footer.hpp>
  103. #if defined(BOOST_LOG_EXPRESSIONS_KEYWORD_HPP_INCLUDED_)
  104. #include <boost/log/detail/trivial_keyword.hpp>
  105. #endif
  106. #endif // BOOST_LOG_TRIVIAL_HPP_INCLUDED_