/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 | | |
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 | 17.4M | SPDLOG_INLINE const string_view_t &to_string_view(spdlog::level::level_enum l) SPDLOG_NOEXCEPT { |
24 | 17.4M | return level_string_views[l]; |
25 | 17.4M | } |
26 | | |
27 | 0 | SPDLOG_INLINE const char *to_short_c_str(spdlog::level::level_enum l) SPDLOG_NOEXCEPT { |
28 | 0 | return short_level_names[l]; |
29 | 0 | } |
30 | | |
31 | 0 | SPDLOG_INLINE spdlog::level::level_enum from_str(const std::string &name) SPDLOG_NOEXCEPT { |
32 | 0 | auto it = std::find(std::begin(level_string_views), std::end(level_string_views), name); |
33 | 0 | if (it != std::end(level_string_views)) |
34 | 0 | return static_cast<level::level_enum>(std::distance(std::begin(level_string_views), it)); |
35 | 0 |
|
36 | 0 | // check also for "warn" and "err" before giving up.. |
37 | 0 | if (name == "warn") { |
38 | 0 | return level::warn; |
39 | 0 | } |
40 | 0 | if (name == "err") { |
41 | 0 | return level::err; |
42 | 0 | } |
43 | 0 | return level::off; |
44 | 0 | } |
45 | | } // namespace level |
46 | | |
47 | | SPDLOG_INLINE spdlog_ex::spdlog_ex(std::string msg) |
48 | 0 | : msg_(std::move(msg)) {} |
49 | | |
50 | 0 | SPDLOG_INLINE spdlog_ex::spdlog_ex(const std::string &msg, int last_errno) { |
51 | | #ifdef SPDLOG_USE_STD_FORMAT |
52 | | msg_ = std::system_error(std::error_code(last_errno, std::generic_category()), msg).what(); |
53 | | #else |
54 | 0 | memory_buf_t outbuf; |
55 | 0 | fmt::format_system_error(outbuf, last_errno, msg.c_str()); |
56 | 0 | msg_ = fmt::to_string(outbuf); |
57 | 0 | #endif |
58 | 0 | } |
59 | | |
60 | 0 | SPDLOG_INLINE const char *spdlog_ex::what() const SPDLOG_NOEXCEPT { return msg_.c_str(); } |
61 | | |
62 | 0 | SPDLOG_INLINE void throw_spdlog_ex(const std::string &msg, int last_errno) { |
63 | 0 | SPDLOG_THROW(spdlog_ex(msg, last_errno)); |
64 | 0 | } |
65 | | |
66 | 0 | SPDLOG_INLINE void throw_spdlog_ex(std::string msg) { SPDLOG_THROW(spdlog_ex(std::move(msg))); } |
67 | | |
68 | | } // namespace spdlog |