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