/src/spdlog/include/spdlog/common-inl.h
Line | Count | Source |
1 | | // Copyright(c) 2015-present, Gabi Melman & spdlog contributors. |
2 | | // Distributed under the MIT License (http://opensource.org/licenses/MIT) |
3 | | |
4 | | #pragma once |
5 | | |
6 | | #ifndef SPDLOG_HEADER_ONLY |
7 | | #include <spdlog/common.h> |
8 | | #endif |
9 | | |
10 | | #include <algorithm> |
11 | | #include <iterator> |
12 | | #include <cctype> |
13 | | |
14 | | SPDLOG_NAMESPACE_BEGIN |
15 | | namespace level { |
16 | | |
17 | | #if __cplusplus >= 201703L |
18 | | constexpr |
19 | | #endif |
20 | | static string_view_t level_string_views[] SPDLOG_LEVEL_NAMES; |
21 | | |
22 | | static const char *short_level_names[] SPDLOG_SHORT_LEVEL_NAMES; |
23 | | |
24 | 0 | SPDLOG_INLINE const string_view_t &to_string_view(level::level_enum l) SPDLOG_NOEXCEPT { |
25 | 0 | return level_string_views[l]; |
26 | 0 | } |
27 | | |
28 | 0 | SPDLOG_INLINE const char *to_short_c_str(level::level_enum l) SPDLOG_NOEXCEPT { |
29 | 0 | return short_level_names[l]; |
30 | 0 | } |
31 | | |
32 | 0 | SPDLOG_INLINE level::level_enum from_str(const std::string &name) SPDLOG_NOEXCEPT { |
33 | 0 | auto it = std::find_if(std::begin(level_string_views), std::end(level_string_views), |
34 | 0 | [&name](const string_view_t &level_name) { |
35 | 0 | return level_name.size() == name.size() && |
36 | 0 | std::equal(name.begin(), name.end(), level_name.begin(), |
37 | 0 | [](char a, char b) { |
38 | 0 | return std::tolower(static_cast<unsigned char>(a)) == |
39 | 0 | std::tolower(static_cast<unsigned char>(b)); |
40 | 0 | }); |
41 | 0 | }); |
42 | 0 | if (it != std::end(level_string_views)) |
43 | 0 | return static_cast<level::level_enum>(std::distance(std::begin(level_string_views), it)); |
44 | | |
45 | | // check also for "warn" and "err" before giving up.. |
46 | 0 | auto iequals = [](const std::string &a, const std::string &b) { |
47 | 0 | return a.size() == b.size() && |
48 | 0 | std::equal(a.begin(), a.end(), b.begin(), [](char ac, char bc) { |
49 | 0 | return std::tolower(static_cast<unsigned char>(ac)) == |
50 | 0 | std::tolower(static_cast<unsigned char>(bc)); |
51 | 0 | }); |
52 | 0 | }; |
53 | |
|
54 | 0 | if (iequals(name, "warn")) { |
55 | 0 | return level::warn; |
56 | 0 | } |
57 | 0 | if (iequals(name, "err")) { |
58 | 0 | return level::err; |
59 | 0 | } |
60 | 0 | return level::off; |
61 | 0 | } |
62 | | } // namespace level |
63 | | |
64 | | SPDLOG_INLINE spdlog_ex::spdlog_ex(std::string msg) |
65 | 0 | : msg_(std::move(msg)) {} |
66 | | |
67 | 0 | SPDLOG_INLINE spdlog_ex::spdlog_ex(const std::string &msg, int last_errno) { |
68 | | #ifdef SPDLOG_USE_STD_FORMAT |
69 | | msg_ = std::system_error(std::error_code(last_errno, std::generic_category()), msg).what(); |
70 | | #else |
71 | 0 | memory_buf_t outbuf; |
72 | 0 | fmt::format_system_error(outbuf, last_errno, msg.c_str()); |
73 | 0 | msg_ = fmt::to_string(outbuf); |
74 | 0 | #endif |
75 | 0 | } |
76 | | |
77 | 0 | SPDLOG_INLINE const char *spdlog_ex::what() const SPDLOG_NOEXCEPT { return msg_.c_str(); } |
78 | | |
79 | 0 | SPDLOG_INLINE void throw_spdlog_ex(const std::string &msg, int last_errno) { |
80 | 0 | SPDLOG_THROW(spdlog_ex(msg, last_errno)); |
81 | 0 | } |
82 | | |
83 | 0 | SPDLOG_INLINE void throw_spdlog_ex(std::string msg) { SPDLOG_THROW(spdlog_ex(std::move(msg))); } |
84 | | |
85 | | SPDLOG_NAMESPACE_END |