Livox SDK API  V2.2.0
common.h
Go to the documentation of this file.
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 #include "spdlog/tweakme.h"
9 
10 #include <atomic>
11 #include <chrono>
12 #include <functional>
13 #include <initializer_list>
14 #include <memory>
15 #include <stdexcept>
16 #include <string>
17 #include <cstring>
18 #include <type_traits>
19 #include <unordered_map>
20 
21 #if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
22 #include <codecvt>
23 #include <locale>
24 #endif
25 
27 
28 #include "spdlog/fmt/fmt.h"
29 
30 // visual studio upto 2013 does not support noexcept nor constexpr
31 #if defined(_MSC_VER) && (_MSC_VER < 1900)
32 #define SPDLOG_NOEXCEPT throw()
33 #define SPDLOG_CONSTEXPR
34 #else
35 #define SPDLOG_NOEXCEPT noexcept
36 #define SPDLOG_CONSTEXPR constexpr
37 #endif
38 
39 #if defined(__GNUC__) || defined(__clang__)
40 #define SPDLOG_DEPRECATED __attribute__((deprecated))
41 #elif defined(_MSC_VER)
42 #define SPDLOG_DEPRECATED __declspec(deprecated)
43 #else
44 #define SPDLOG_DEPRECATED
45 #endif
46 
47 // disable thread local on msvc 2013
48 #ifndef SPDLOG_NO_TLS
49 #if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__cplusplus_winrt)
50 #define SPDLOG_NO_TLS 1
51 #endif
52 #endif
53 
54 // Get the basename of __FILE__ (at compile time if possible)
55 #if FMT_HAS_FEATURE(__builtin_strrchr)
56 #define SPDLOG_STRRCHR(str, sep) __builtin_strrchr(str, sep)
57 #else
58 #define SPDLOG_STRRCHR(str, sep) strrchr(str, sep)
59 #endif //__builtin_strrchr not defined
60 
61 #ifdef _WIN32
62 #define SPDLOG_FILE_BASENAME(file) SPDLOG_STRRCHR("\\" file, '\\') + 1
63 #else
64 #define SPDLOG_FILE_BASENAME(file) SPDLOG_STRRCHR("/" file, '/') + 1
65 #endif
66 
67 #ifndef SPDLOG_FUNCTION
68 #define SPDLOG_FUNCTION __FUNCTION__
69 #endif
70 
71 namespace spdlog {
72 
73 class formatter;
74 
75 namespace sinks {
76 class sink;
77 }
78 
79 using log_clock = std::chrono::system_clock;
80 using sink_ptr = std::shared_ptr<sinks::sink>;
81 using sinks_init_list = std::initializer_list<sink_ptr>;
82 using log_err_handler = std::function<void(const std::string &err_msg)>;
83 
84 // string_view type - either std::string_view or fmt::string_view (pre c++17)
85 #if defined(FMT_USE_STD_STRING_VIEW)
87 #else
89 #endif
90 
91 #if defined(SPDLOG_NO_ATOMIC_LEVELS)
93 #else
94 using level_t = std::atomic<int>;
95 #endif
96 
97 #define SPDLOG_LEVEL_TRACE 0
98 #define SPDLOG_LEVEL_DEBUG 1
99 #define SPDLOG_LEVEL_INFO 2
100 #define SPDLOG_LEVEL_WARN 3
101 #define SPDLOG_LEVEL_ERROR 4
102 #define SPDLOG_LEVEL_CRITICAL 5
103 #define SPDLOG_LEVEL_OFF 6
104 
105 #if !defined(SPDLOG_ACTIVE_LEVEL)
106 #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO
107 #endif
108 
109 // Log level enum
110 namespace level {
112 {
120 };
121 
122 #if !defined(SPDLOG_LEVEL_NAMES)
123 #define SPDLOG_LEVEL_NAMES \
124  { \
125  "trace", "debug", "info", "warning", "error", "critical", "off" \
126  }
127 #endif
129 
130 #if !defined(SPDLOG_SHORT_LEVEL_NAMES)
131 #define SPDLOG_SHORT_LEVEL_NAMES {"T", "D", "I", "W", "E", "C", "O"}
132 #endif
134 
136 {
137  return level_string_views[l];
138 }
139 
141 {
142  return short_level_names[l];
143 }
144 
145 inline spdlog::level::level_enum from_str(const std::string &name) SPDLOG_NOEXCEPT
146 {
147  int level = 0;
148  for (const auto &level_str : level_string_views)
149  {
150  if (level_str == name)
151  {
152  return static_cast<level::level_enum>(level);
153  }
154  level++;
155  }
156  return level::off;
157 }
158 
159 using level_hasher = std::hash<int>;
160 } // namespace level
161 
162 //
163 // Pattern time - specific time getting to use for pattern_formatter.
164 // local time by default
165 //
167 {
168  local, // log localtime
169  utc // log utc
170 };
171 
172 //
173 // Log exception
174 //
175 class spdlog_ex : public std::exception
176 {
177 public:
178  explicit spdlog_ex(std::string msg)
179  : msg_(std::move(msg))
180  {
181  }
182 
183  spdlog_ex(const std::string &msg, int last_errno)
184  {
185  fmt::memory_buffer outbuf;
186  fmt::format_system_error(outbuf, last_errno, msg);
187  msg_ = fmt::to_string(outbuf);
188  }
189 
190  const char *what() const SPDLOG_NOEXCEPT override
191  {
192  return msg_.c_str();
193  }
194 
195 private:
196  std::string msg_;
197 };
198 
199 //
200 // wchar support for windows file names (SPDLOG_WCHAR_FILENAMES must be defined)
201 //
202 #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
203 using filename_t = std::wstring;
204 #else
205 using filename_t = std::string;
206 #endif
207 
209 {
211  : filename{""}
212  , line{0}
213  , funcname{""}
214  {
215  }
216  SPDLOG_CONSTEXPR source_loc(const char *filename_in, int line_in, const char *funcname_in)
217  : filename{filename_in}
218  , line{static_cast<uint32_t>(line_in)}
219  , funcname{funcname_in}
220  {
221  }
222 
224  {
225  return line == 0;
226  }
227  const char *filename;
228  uint32_t line;
229  const char *funcname;
230 };
231 
232 namespace details {
233 // make_unique support for pre c++14
234 
235 #if __cplusplus >= 201402L // C++14 and beyond
236 using std::make_unique;
237 #else
238 template<typename T, typename... Args>
239 std::unique_ptr<T> make_unique(Args &&... args)
240 {
241  static_assert(!std::is_array<T>::value, "arrays not supported");
242  return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
243 }
244 #endif
245 } // namespace details
246 } // namespace spdlog
spdlog::level::level_enum from_str(const std::string &name) noexcept
Definition: common.h:145
std::initializer_list< sink_ptr > sinks_init_list
Definition: common.h:81
#define SPDLOG_LEVEL_WARN
Definition: common.h:100
void info(const char *fmt, const Args &...args)
Definition: spdlog.h:189
constexpr source_loc(const char *filename_in, int line_in, const char *funcname_in)
Definition: common.h:216
Definition: format.h:297
#define SPDLOG_LEVEL_INFO
Definition: common.h:99
#define SPDLOG_LEVEL_NAMES
Definition: common.h:123
#define SPDLOG_LEVEL_TRACE
Definition: common.h:97
const char * funcname
Definition: common.h:229
#define SPDLOG_LEVEL_DEBUG
Definition: common.h:98
std::shared_ptr< sinks::sink > sink_ptr
Definition: common.h:80
const char * filename
Definition: common.h:227
#define SPDLOG_SHORT_LEVEL_NAMES
Definition: common.h:131
Definition: async.h:27
spdlog_ex(const std::string &msg, int last_errno)
Definition: common.h:183
std::hash< int > level_hasher
Definition: common.h:159
static string_view_t level_string_views[]
Definition: common.h:128
void critical(const char *fmt, const Args &...args)
Definition: spdlog.h:207
const char * to_short_c_str(spdlog::level::level_enum l) noexcept
Definition: common.h:140
#define SPDLOG_LEVEL_OFF
Definition: common.h:103
pattern_time_type
Definition: common.h:166
std::string filename_t
Definition: common.h:205
#define SPDLOG_LEVEL_ERROR
Definition: common.h:101
spdlog_ex(std::string msg)
Definition: common.h:178
std::chrono::system_clock log_clock
Definition: common.h:79
constexpr source_loc()
Definition: common.h:210
constexpr bool empty() const noexcept
Definition: common.h:223
std::unique_ptr< T > make_unique(Args &&...args)
Definition: common.h:239
void warn(const char *fmt, const Args &...args)
Definition: spdlog.h:195
string_view_t & to_string_view(spdlog::level::level_enum l) noexcept
Definition: common.h:135
void debug(const char *fmt, const Args &...args)
Definition: spdlog.h:183
#define SPDLOG_NOEXCEPT
Definition: common.h:35
#define SPDLOG_CONSTEXPR
Definition: common.h:36
std::atomic< int > level_t
Definition: common.h:94
std::string to_string(const T &value)
Definition: format.h:3209
static const char * short_level_names[]
Definition: common.h:133
void format_system_error(internal::buffer &out, int error_code, string_view message)
Definition: format-inl.h:906
#define SPDLOG_LEVEL_CRITICAL
Definition: common.h:102
std::function< void(const std::string &err_msg)> log_err_handler
Definition: common.h:82
basic_string_view< char > string_view
Definition: core.h:428
void trace(const char *fmt, const Args &...args)
Definition: spdlog.h:177
uint32_t line
Definition: common.h:228
const char * what() const noexceptoverride
Definition: common.h:190