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 | |
13 | namespace spdlog { |
14 | namespace level { |
15 | |
16 | #if __cplusplus >= 201703L |
17 | constexpr |
18 | #endif |
19 | static string_view_t level_string_views[] SPDLOG_LEVEL_NAMES; |
20 | |
21 | static const char *short_level_names[] SPDLOG_SHORT_LEVEL_NAMES; |
22 | |
23 | SPDLOG_INLINE const string_view_t &to_string_view(spdlog::level::level_enum l) SPDLOG_NOEXCEPT |
24 | { |
25 | return level_string_views[l]; |
26 | } |
27 | |
28 | SPDLOG_INLINE const char *to_short_c_str(spdlog::level::level_enum l) SPDLOG_NOEXCEPT |
29 | { |
30 | return short_level_names[l]; |
31 | } |
32 | |
33 | SPDLOG_INLINE spdlog::level::level_enum from_str(const std::string &name) SPDLOG_NOEXCEPT |
34 | { |
35 | auto it = std::find(std::begin(level_string_views), std::end(level_string_views), name); |
36 | if (it != std::end(level_string_views)) |
37 | return static_cast<level::level_enum>(std::distance(std::begin(level_string_views), it)); |
38 | |
39 | // check also for "warn" and "err" before giving up.. |
40 | if (name == "warn" ) |
41 | { |
42 | return level::warn; |
43 | } |
44 | if (name == "err" ) |
45 | { |
46 | return level::err; |
47 | } |
48 | return level::off; |
49 | } |
50 | } // namespace level |
51 | |
52 | SPDLOG_INLINE spdlog_ex::spdlog_ex(std::string msg) |
53 | : msg_(std::move(msg)) |
54 | {} |
55 | |
56 | SPDLOG_INLINE spdlog_ex::spdlog_ex(const std::string &msg, int last_errno) |
57 | { |
58 | #ifdef SPDLOG_USE_STD_FORMAT |
59 | msg_ = std::system_error(std::error_code(last_errno, std::generic_category()), msg).what(); |
60 | #else |
61 | memory_buf_t outbuf; |
62 | fmt::format_system_error(outbuf, last_errno, msg.c_str()); |
63 | msg_ = fmt::to_string(outbuf); |
64 | #endif |
65 | } |
66 | |
67 | SPDLOG_INLINE const char *spdlog_ex::what() const SPDLOG_NOEXCEPT |
68 | { |
69 | return msg_.c_str(); |
70 | } |
71 | |
72 | SPDLOG_INLINE void throw_spdlog_ex(const std::string &msg, int last_errno) |
73 | { |
74 | SPDLOG_THROW(spdlog_ex(msg, last_errno)); |
75 | } |
76 | |
77 | SPDLOG_INLINE void throw_spdlog_ex(std::string msg) |
78 | { |
79 | SPDLOG_THROW(spdlog_ex(std::move(msg))); |
80 | } |
81 | |
82 | } // namespace spdlog |
83 | |