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 trivial.cpp
9 * \author Andrey Semashev
10 * \date 07.11.2009
11 *
12 * \brief This header is the Boost.Log library implementation, see the library documentation
13 * at http://www.boost.org/doc/libs/release/libs/log/doc/html/index.html.
14 */
15
16 #include <boost/log/detail/config.hpp>
17
18 #if defined(BOOST_LOG_USE_CHAR)
19
20 #include <string>
21 #include <istream>
22 #include <boost/log/trivial.hpp>
23 #include <boost/log/sources/global_logger_storage.hpp>
24 #include <boost/log/detail/header.hpp>
25
26 namespace boost {
27
28 BOOST_LOG_OPEN_NAMESPACE
29
30 namespace trivial {
31
32 //! Initialization routine
construct_logger()33 BOOST_LOG_API logger::logger_type logger::construct_logger()
34 {
35 return logger_type(keywords::severity = info);
36 }
37
38 //! Returns a reference to the trivial logger instance
get()39 BOOST_LOG_API logger::logger_type& logger::get()
40 {
41 return log::sources::aux::logger_singleton< logger >::get();
42 }
43
44 BOOST_LOG_ANONYMOUS_NAMESPACE {
45
46 BOOST_CONSTEXPR_OR_CONST unsigned int names_count = 6;
47
48 template< typename CharT >
49 struct severity_level_names
50 {
51 static const CharT names[names_count][8];
52 };
53
54 template< typename CharT >
55 const CharT severity_level_names< CharT >::names[names_count][8] =
56 {
57 { 't', 'r', 'a', 'c', 'e', 0 },
58 { 'd', 'e', 'b', 'u', 'g', 0 },
59 { 'i', 'n', 'f', 'o', 0 },
60 { 'w', 'a', 'r', 'n', 'i', 'n', 'g', 0 },
61 { 'e', 'r', 'r', 'o', 'r', 0 },
62 { 'f', 'a', 't', 'a', 'l', 0 }
63 };
64
65 } // namespace
66
67 template< typename CharT >
68 BOOST_LOG_API const CharT* to_string(severity_level lvl)
69 {
70 typedef severity_level_names< CharT > level_names;
71 if (BOOST_LIKELY(static_cast< unsigned int >(lvl) < names_count))
72 return level_names::names[static_cast< unsigned int >(lvl)];
73 return NULL;
74 }
75
76 //! Parses enumeration value from string and returns \c true on success and \c false otherwise
77 template< typename CharT >
from_string(const CharT * str,std::size_t len,severity_level & lvl)78 BOOST_LOG_API bool from_string(const CharT* str, std::size_t len, severity_level& lvl)
79 {
80 typedef severity_level_names< CharT > level_names;
81 typedef std::char_traits< CharT > char_traits;
82
83 if (len == 5u)
84 {
85 if (char_traits::compare(str, level_names::names[0], len) == 0)
86 lvl = static_cast< severity_level >(0);
87 else if (char_traits::compare(str, level_names::names[1], len) == 0)
88 lvl = static_cast< severity_level >(1);
89 else if (char_traits::compare(str, level_names::names[4], len) == 0)
90 lvl = static_cast< severity_level >(4);
91 else if (char_traits::compare(str, level_names::names[5], len) == 0)
92 lvl = static_cast< severity_level >(5);
93 else
94 goto no_match;
95 return true;
96 }
97 else if (len == 4u)
98 {
99 if (char_traits::compare(str, level_names::names[2], len) == 0)
100 lvl = static_cast< severity_level >(2);
101 else
102 goto no_match;
103 return true;
104 }
105 else if (len == 7u)
106 {
107 if (char_traits::compare(str, level_names::names[3], len) == 0)
108 lvl = static_cast< severity_level >(3);
109 else
110 goto no_match;
111 return true;
112 }
113
114 no_match:
115 return false;
116 }
117
118 template< typename CharT, typename TraitsT >
operator >>(std::basic_istream<CharT,TraitsT> & strm,severity_level & lvl)119 BOOST_LOG_API std::basic_istream< CharT, TraitsT >& operator>> (
120 std::basic_istream< CharT, TraitsT >& strm, severity_level& lvl)
121 {
122 if (BOOST_LIKELY(strm.good()))
123 {
124 typedef std::basic_string< CharT, TraitsT > string_type;
125 string_type str;
126 strm >> str;
127 if (BOOST_UNLIKELY(!boost::log::trivial::from_string(str.data(), str.size(), lvl)))
128 strm.setstate(std::ios_base::failbit);
129 }
130
131 return strm;
132 }
133
134 template BOOST_LOG_API const char* to_string< char >(severity_level lvl);
135 template BOOST_LOG_API bool from_string< char >(const char* begin, std::size_t len, severity_level& lvl);
136 template BOOST_LOG_API std::basic_istream< char, std::char_traits< char > >&
137 operator>> < char, std::char_traits< char > > (
138 std::basic_istream< char, std::char_traits< char > >& strm, severity_level& lvl);
139 #ifdef BOOST_LOG_USE_WCHAR_T
140 template BOOST_LOG_API const wchar_t* to_string< wchar_t >(severity_level lvl);
141 template BOOST_LOG_API bool from_string< wchar_t >(const wchar_t* begin, std::size_t len, severity_level& lvl);
142 template BOOST_LOG_API std::basic_istream< wchar_t, std::char_traits< wchar_t > >&
143 operator>> < wchar_t, std::char_traits< wchar_t > > (
144 std::basic_istream< wchar_t, std::char_traits< wchar_t > >& strm, severity_level& lvl);
145 #endif
146
147 } // namespace trivial
148
149 BOOST_LOG_CLOSE_NAMESPACE // namespace log
150
151 } // namespace boost
152
153 #include <boost/log/detail/footer.hpp>
154
155 #endif // defined(BOOST_LOG_USE_CHAR)
156