spdlog/src/common.cpp

60 lines
1.4 KiB
C++
Raw Normal View History

2019-06-04 05:09:16 +08:00
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
2019-05-12 01:06:17 +08:00
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
2023-09-25 01:26:32 +08:00
#include <spdlog/common.h>
2019-05-13 05:09:00 +08:00
#include <algorithm>
#include <iterator>
namespace spdlog {
2021-01-11 18:25:27 +08:00
spdlog::level level_from_str(const std::string &name) noexcept
{
2023-09-23 06:48:38 +08:00
auto it = std::find(std::begin(level_string_views), std::end(level_string_views), name);
if (it != std::end(level_string_views))
return static_cast<level>(std::distance(std::begin(level_string_views), it));
// check also for "warn" and "err" before giving up..
if (name == "warn")
{
return spdlog::level::warn;
}
if (name == "err")
{
return level::err;
}
return level::off;
}
spdlog_ex::spdlog_ex(std::string msg)
: msg_(std::move(msg))
{}
spdlog_ex::spdlog_ex(const std::string &msg, int last_errno)
2019-05-12 01:15:03 +08:00
{
#ifdef SPDLOG_USE_STD_FORMAT
msg_ = std::system_error(std::error_code(last_errno, std::generic_category()), msg).what();
#else
memory_buf_t outbuf;
2021-06-24 18:22:02 +08:00
fmt::format_system_error(outbuf, last_errno, msg.c_str());
2019-05-12 01:15:03 +08:00
msg_ = fmt::to_string(outbuf);
#endif
2019-05-12 01:15:03 +08:00
}
const char *spdlog_ex::what() const noexcept
2019-05-12 01:15:03 +08:00
{
return msg_.c_str();
}
void throw_spdlog_ex(const std::string &msg, int last_errno)
{
2020-03-22 06:09:56 +08:00
SPDLOG_THROW(spdlog_ex(msg, last_errno));
}
void throw_spdlog_ex(std::string msg)
{
2020-03-22 06:09:56 +08:00
SPDLOG_THROW(spdlog_ex(std::move(msg)));
}
} // namespace spdlog