You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

common.h 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // Copyright(c) 2015 Gabi Melman.
  3. // Distributed under the MIT License (http://opensource.org/licenses/MIT)
  4. //
  5. #pragma once
  6. #include <string>
  7. #include <initializer_list>
  8. #include <chrono>
  9. #include <memory>
  10. #include <atomic>
  11. #include <exception>
  12. #include<functional>
  13. #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
  14. #include <codecvt>
  15. #include <locale>
  16. #endif
  17. #include <spdlog/details/null_mutex.h>
  18. //visual studio upto 2013 does not support noexcept nor constexpr
  19. #if defined(_MSC_VER) && (_MSC_VER < 1900)
  20. #define SPDLOG_NOEXCEPT throw()
  21. #define SPDLOG_CONSTEXPR
  22. #else
  23. #define SPDLOG_NOEXCEPT noexcept
  24. #define SPDLOG_CONSTEXPR constexpr
  25. #endif
  26. #if defined(__GNUC__) || defined(__clang__)
  27. #define DEPRECATED __attribute__((deprecated))
  28. #elif defined(_MSC_VER)
  29. #define DEPRECATED __declspec(deprecated)
  30. #else
  31. #define DEPRECATED
  32. #endif
  33. #include <spdlog/fmt/fmt.h>
  34. namespace spdlog
  35. {
  36. class formatter;
  37. namespace sinks
  38. {
  39. class sink;
  40. }
  41. using log_clock = std::chrono::system_clock;
  42. using sink_ptr = std::shared_ptr < sinks::sink >;
  43. using sinks_init_list = std::initializer_list < sink_ptr >;
  44. using formatter_ptr = std::shared_ptr<spdlog::formatter>;
  45. #if defined(SPDLOG_NO_ATOMIC_LEVELS)
  46. using level_t = details::null_atomic_int;
  47. #else
  48. using level_t = std::atomic_int;
  49. #endif
  50. using log_err_handler = std::function<void(const std::string &err_msg)>;
  51. //Log level enum
  52. namespace level
  53. {
  54. typedef enum
  55. {
  56. trace = 0,
  57. debug = 1,
  58. info = 2,
  59. warn = 3,
  60. err = 4,
  61. critical = 5,
  62. off = 6
  63. } level_enum;
  64. static const char* level_names[] { "trace", "debug", "info", "warning", "error", "critical", "off" };
  65. static const char* short_level_names[] { "T", "D", "I", "W", "E", "C", "O" };
  66. inline const char* to_str(spdlog::level::level_enum l)
  67. {
  68. return level_names[l];
  69. }
  70. inline const char* to_short_str(spdlog::level::level_enum l)
  71. {
  72. return short_level_names[l];
  73. }
  74. } //level
  75. //
  76. // Async overflow policy - block by default.
  77. //
  78. enum class async_overflow_policy
  79. {
  80. block_retry, // Block / yield / sleep until message can be enqueued
  81. discard_log_msg // Discard the message it enqueue fails
  82. };
  83. //
  84. // Log exception
  85. //
  86. namespace details
  87. {
  88. namespace os
  89. {
  90. std::string errno_str(int err_num);
  91. }
  92. }
  93. class spdlog_ex: public std::exception
  94. {
  95. public:
  96. spdlog_ex(const std::string& msg):_msg(msg)
  97. {}
  98. spdlog_ex(const std::string& msg, int last_errno)
  99. {
  100. _msg = msg + ": " + details::os::errno_str(last_errno);
  101. }
  102. const char* what() const SPDLOG_NOEXCEPT override
  103. {
  104. return _msg.c_str();
  105. }
  106. private:
  107. std::string _msg;
  108. };
  109. //
  110. // wchar support for windows file names (SPDLOG_WCHAR_FILENAMES must be defined)
  111. //
  112. #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
  113. using filename_t = std::wstring;
  114. #else
  115. using filename_t = std::string;
  116. #endif
  117. } //spdlog