trivial.hpp 4.1 KB

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