WireCellToolkit
Wire Cell Simulation, Signal Process and Reconstruction Toolki for Liquid Argon Detectors
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
128 
129 static string_view_t level_string_views[] SPDLOG_LEVEL_NAMES;
130 static const char *short_level_names[]{"T", "D", "I", "W", "E", "C", "O"};
131 
133 {
134  return level_string_views[l];
135 }
136 
138 {
139  return short_level_names[l];
140 }
141 
142 inline spdlog::level::level_enum from_str(const std::string &name) SPDLOG_NOEXCEPT
143 {
144  int level = 0;
145  for (const auto &level_str : level_string_views)
146  {
147  if (level_str == name)
148  {
149  return static_cast<level::level_enum>(level);
150  }
151  level++;
152  }
153  return level::off;
154 }
155 
156 using level_hasher = std::hash<int>;
157 } // namespace level
158 
159 //
160 // Pattern time - specific time getting to use for pattern_formatter.
161 // local time by default
162 //
164 {
165  local, // log localtime
166  utc // log utc
167 };
168 
169 //
170 // Log exception
171 //
172 class spdlog_ex : public std::exception
173 {
174 public:
175  explicit spdlog_ex(std::string msg)
176  : msg_(std::move(msg))
177  {
178  }
179 
180  spdlog_ex(const std::string &msg, int last_errno)
181  {
182  fmt::memory_buffer outbuf;
183  fmt::format_system_error(outbuf, last_errno, msg);
184  msg_ = fmt::to_string(outbuf);
185  }
186 
187  const char *what() const SPDLOG_NOEXCEPT override
188  {
189  return msg_.c_str();
190  }
191 
192 private:
193  std::string msg_;
194 };
195 
196 //
197 // wchar support for windows file names (SPDLOG_WCHAR_FILENAMES must be defined)
198 //
199 #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
200 using filename_t = std::wstring;
201 #else
202 using filename_t = std::string;
203 #endif
204 
206 {
208  : filename{""}
209  , line{0}
210  , funcname{""}
211  {
212  }
213  SPDLOG_CONSTEXPR source_loc(const char *filename, int line, const char *funcname)
214  : filename{filename}
215  , line{static_cast<uint32_t>(line)}
216  , funcname{funcname}
217  {
218  }
219 
221  {
222  return line == 0;
223  }
224  const char *filename;
225  uint32_t line;
226  const char *funcname;
227 };
228 
229 namespace details {
230 // make_unique support for pre c++14
231 
232 #if __cplusplus >= 201402L // C++14 and beyond
233 using std::make_unique;
234 #else
235 template<typename T, typename... Args>
236 std::unique_ptr<T> make_unique(Args &&... args)
237 {
238  static_assert(!std::is_array<T>::value, "arrays not supported");
239  return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
240 }
241 #endif
242 } // namespace details
243 } // namespace spdlog
void warn(const char *fmt, const Args &... args)
Definition: spdlog.h:195
spdlog::level::level_enum from_str(const std::string &name) SPDLOG_NOEXCEPT
Definition: common.h:142
basic_memory_buffer< char > memory_buffer
Definition: format.h:553
std::initializer_list< sink_ptr > sinks_init_list
Definition: common.h:81
#define SPDLOG_LEVEL_WARN
Definition: common.h:100
SPDLOG_CONSTEXPR source_loc()
Definition: common.h:207
const char * what() const SPDLOG_NOEXCEPT override
Definition: common.h:187
If we are still before C++14, supply the fodder for doing the "indices trick".
Definition: format.h:297
FMT_FUNC void format_system_error(internal::buffer &out, int error_code, string_view message) FMT_NOEXCEPT
Definition: format-inl.h:906
#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:226
#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:224
Definition: async.h:27
spdlog_ex(const std::string &msg, int last_errno)
Definition: common.h:180
std::hash< int > level_hasher
Definition: common.h:156
void info(const char *fmt, const Args &... args)
Definition: spdlog.h:189
#define SPDLOG_LEVEL_OFF
Definition: common.h:103
pattern_time_type
Definition: common.h:163
basic_string_view< char > string_view
Definition: core.h:428
std::string filename_t
Definition: common.h:202
#define SPDLOG_LEVEL_ERROR
Definition: common.h:101
void trace(const char *fmt, const Args &... args)
Definition: spdlog.h:177
spdlog_ex(std::string msg)
Definition: common.h:175
void debug(const char *fmt, const Args &... args)
Definition: spdlog.h:183
std::chrono::system_clock log_clock
Definition: common.h:79
void critical(const char *fmt, const Args &... args)
Definition: spdlog.h:207
string_view_t & to_string_view(spdlog::level::level_enum l) SPDLOG_NOEXCEPT
Definition: common.h:132
fmt::string_view string_view_t
Definition: common.h:88
std::string to_string(const T &value)
Definition: format.h:3209
std::unique_ptr< T > make_unique(Args &&... args)
Definition: common.h:236
const char * to_short_c_str(spdlog::level::level_enum l) SPDLOG_NOEXCEPT
Definition: common.h:137
#define SPDLOG_NOEXCEPT
Definition: common.h:35
#define SPDLOG_CONSTEXPR
Definition: common.h:36
std::atomic< int > level_t
Definition: common.h:94
SPDLOG_CONSTEXPR bool empty() const SPDLOG_NOEXCEPT
Definition: common.h:220
#define SPDLOG_LEVEL_CRITICAL
Definition: common.h:102
std::function< void(const std::string &err_msg)> log_err_handler
Definition: common.h:82
uint32_t line
Definition: common.h:225
const Args & args
Definition: core.h:1496
SPDLOG_CONSTEXPR source_loc(const char *filename, int line, const char *funcname)
Definition: common.h:213