common-inl.h 2.0 KB

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