source: trunk/GDE/SINA/builddir/include/spdlog/common.h

Last change on this file was 19170, checked in by westram, 2 years ago
  • sina source
    • unpack + remove tarball
    • no longer ignore sina builddir.
File size: 5.8 KB
Line 
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
26#include "spdlog/details/null_mutex.h"
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
71namespace spdlog {
72
73class formatter;
74
75namespace sinks {
76class sink;
77}
78
79using log_clock = std::chrono::system_clock;
80using sink_ptr = std::shared_ptr<sinks::sink>;
81using sinks_init_list = std::initializer_list<sink_ptr>;
82using 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)
86using string_view_t = std::string_view;
87#else
88using string_view_t = fmt::string_view;
89#endif
90
91#if defined(SPDLOG_NO_ATOMIC_LEVELS)
92using level_t = details::null_atomic_int;
93#else
94using 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
110namespace level {
111enum level_enum
112{
113    trace = SPDLOG_LEVEL_TRACE,
114    debug = SPDLOG_LEVEL_DEBUG,
115    info = SPDLOG_LEVEL_INFO,
116    warn = SPDLOG_LEVEL_WARN,
117    err = SPDLOG_LEVEL_ERROR,
118    critical = SPDLOG_LEVEL_CRITICAL,
119    off = SPDLOG_LEVEL_OFF,
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
129static string_view_t level_string_views[] SPDLOG_LEVEL_NAMES;
130static const char *short_level_names[]{"T", "D", "I", "W", "E", "C", "O"};
131
132inline string_view_t &to_string_view(spdlog::level::level_enum l) SPDLOG_NOEXCEPT
133{
134    return level_string_views[l];
135}
136
137inline const char *to_short_c_str(spdlog::level::level_enum l) SPDLOG_NOEXCEPT
138{
139    return short_level_names[l];
140}
141
142inline 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
156using 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//
163enum class pattern_time_type
164{
165    local, // log localtime
166    utc    // log utc
167};
168
169//
170// Log exception
171//
172class spdlog_ex : public std::exception
173{
174public:
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
192private:
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)
200using filename_t = std::wstring;
201#else
202using filename_t = std::string;
203#endif
204
205struct source_loc
206{
207    SPDLOG_CONSTEXPR source_loc()
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
220    SPDLOG_CONSTEXPR bool empty() const SPDLOG_NOEXCEPT
221    {
222        return line == 0;
223    }
224    const char *filename;
225    uint32_t line;
226    const char *funcname;
227};
228
229namespace details {
230// make_unique support for pre c++14
231
232#if __cplusplus >= 201402L // C++14 and beyond
233using std::make_unique;
234#else
235template<typename T, typename... Args>
236std::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
Note: See TracBrowser for help on using the repository browser.