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