common-inl.h 2.0 KB

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