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