bitz-server  2.0.3
lib/spdlog/common.h
1 //
2 // Copyright(c) 2015 Gabi Melman.
3 // Distributed under the MIT License (http://opensource.org/licenses/MIT)
4 //
5 
6 #pragma once
7 
8 #define SPDLOG_VERSION "0.17.0"
9 
10 #include "tweakme.h"
11 
12 #include <atomic>
13 #include <chrono>
14 #include <exception>
15 #include <functional>
16 #include <initializer_list>
17 #include <memory>
18 #include <string>
19 #include <unordered_map>
20 
21 #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
22 #include <codecvt>
23 #include <locale>
24 #endif
25 
26 #include "details/null_mutex.h"
27 
28 // visual studio upto 2013 does not support noexcept nor constexpr
29 #if defined(_MSC_VER) && (_MSC_VER < 1900)
30 #define SPDLOG_NOEXCEPT throw()
31 #define SPDLOG_CONSTEXPR
32 #else
33 #define SPDLOG_NOEXCEPT noexcept
34 #define SPDLOG_CONSTEXPR constexpr
35 #endif
36 
37 // final keyword support. On by default. See tweakme.h
38 #if defined(SPDLOG_NO_FINAL)
39 #define SPDLOG_FINAL
40 #else
41 #define SPDLOG_FINAL final
42 #endif
43 
44 #if defined(__GNUC__) || defined(__clang__)
45 #define SPDLOG_DEPRECATED __attribute__((deprecated))
46 #elif defined(_MSC_VER)
47 #define SPDLOG_DEPRECATED __declspec(deprecated)
48 #else
49 #define SPDLOG_DEPRECATED
50 #endif
51 
52 #include "fmt/fmt.h"
53 
54 namespace spdlog {
55 
56 class formatter;
57 
58 namespace sinks {
59 class sink;
60 }
61 
62 using log_clock = std::chrono::system_clock;
63 using sink_ptr = std::shared_ptr<sinks::sink>;
64 using sinks_init_list = std::initializer_list<sink_ptr>;
65 using formatter_ptr = std::shared_ptr<spdlog::formatter>;
66 #if defined(SPDLOG_NO_ATOMIC_LEVELS)
67 using level_t = details::null_atomic_int;
68 #else
69 using level_t = std::atomic<int>;
70 #endif
71 
72 using log_err_handler = std::function<void(const std::string &err_msg)>;
73 
74 // Log level enum
75 namespace level {
76 enum level_enum
77 {
78  trace = 0,
79  debug = 1,
80  info = 2,
81  warn = 3,
82  err = 4,
83  critical = 5,
84  off = 6
85 };
86 
87 #if !defined(SPDLOG_LEVEL_NAMES)
88 #define SPDLOG_LEVEL_NAMES \
89  { \
90  "trace", "debug", "info", "warning", "error", "critical", "off" \
91  }
92 #endif
93 static const char *level_names[] SPDLOG_LEVEL_NAMES;
94 
95 static const char *short_level_names[]{"T", "D", "I", "W", "E", "C", "O"};
96 
97 inline const char *to_str(spdlog::level::level_enum l)
98 {
99  return level_names[l];
100 }
101 
102 inline const char *to_short_str(spdlog::level::level_enum l)
103 {
104  return short_level_names[l];
105 }
106 inline spdlog::level::level_enum from_str(const std::string &name)
107 {
108  static std::unordered_map<std::string, level_enum> name_to_level = // map string->level
109  {{level_names[0], level::trace}, // trace
110  {level_names[1], level::debug}, // debug
111  {level_names[2], level::info}, // info
112  {level_names[3], level::warn}, // warn
113  {level_names[4], level::err}, // err
114  {level_names[5], level::critical}, // critical
115  {level_names[6], level::off}}; // off
116 
117  auto lvl_it = name_to_level.find(name);
118  return lvl_it != name_to_level.end() ? lvl_it->second : level::off;
119 }
120 
121 using level_hasher = std::hash<int>;
122 } // namespace level
123 
124 //
125 // Async overflow policy - block by default.
126 //
127 enum class async_overflow_policy
128 {
129  block_retry, // Block / yield / sleep until message can be enqueued
130  discard_log_msg // Discard the message it enqueue fails
131 };
132 
133 //
134 // Pattern time - specific time getting to use for pattern_formatter.
135 // local time by default
136 //
137 enum class pattern_time_type
138 {
139  local, // log localtime
140  utc // log utc
141 };
142 
143 //
144 // Log exception
145 //
146 class spdlog_ex : public std::exception
147 {
148 public:
149  explicit spdlog_ex(std::string msg)
150  : _msg(std::move(msg))
151  {
152  }
153 
154  spdlog_ex(const std::string &msg, int last_errno)
155  {
156  fmt::MemoryWriter writer;
157  fmt::format_system_error(writer, last_errno, msg);
158  _msg = writer.str();
159  }
160 
161  const char *what() const SPDLOG_NOEXCEPT override
162  {
163  return _msg.c_str();
164  }
165 
166 private:
167  std::string _msg;
168 };
169 
170 //
171 // wchar support for windows file names (SPDLOG_WCHAR_FILENAMES must be defined)
172 //
173 #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
174 using filename_t = std::wstring;
175 #else
176 using filename_t = std::string;
177 #endif
178 
179 #define SPDLOG_CATCH_AND_HANDLE \
180  catch (const std::exception &ex) \
181  { \
182  _err_handler(ex.what()); \
183  } \
184  catch (...) \
185  { \
186  _err_handler("Unknown exception in logger"); \
187  }
188 } // namespace spdlog
Definition: lib/spdlog/common.h:146
Definition: async_logger.h:26
std::basic_string< Char > str() const
Definition: format.h:3302
Definition: format.h:3924