-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathfossilize_errors.hpp
More file actions
170 lines (150 loc) · 5.82 KB
/
fossilize_errors.hpp
File metadata and controls
170 lines (150 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* Copyright (c) 2018-2019 Hans-Kristian Arntzen
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include <string>
#include <utility>
#include <stdint.h>
#include <atomic>
#include <string.h>
#include "fossilize_inttypes.h"
#if defined(_MSC_VER) && (_MSC_VER <= 1800)
#define FOSSILIZE_NOEXCEPT
#else
#define FOSSILIZE_NOEXCEPT noexcept
#endif
namespace Fossilize
{
// Sets the global logging level for the current thread.
// Used by the Fossilize API.
// Any internal threads created by Fossilize will inherit the log level and callbacks from creating thread.
// FOSSILIZE_API_DEFAULT_LOG_LEVEL define can be used to set initial value.
void set_thread_log_level(LogLevel level);
LogLevel get_thread_log_level();
// Custom callback. Log level filter is somewhat moot, but at least avoids some redundant
// work from happening to build a message that is ignored.
using LogCallback = void (*)(LogLevel level, const char *message, void *userdata);
void set_thread_log_callback(LogCallback cb, void *userdata);
namespace Internal
{
bool log_thread_callback(LogLevel level, const char *fmt, ...);
LogCallback get_thread_log_callback();
void *get_thread_log_userdata();
static inline const char *extract_basepath(const char *path)
{
const char *base = strrchr(path, '\\');
if (base)
return base + 1;
base = strrchr(path, '/');
if (base)
return base + 1;
return "";
}
}
#ifndef LOGE
#error "LOGE must be defined before including fossilize_errors.hpp."
#endif
#ifndef LOGW
#error "LOGW must be defined before including fossilize_errors.hpp."
#endif
#ifndef FOSSILIZE_SPAM_LIMIT_WARN
#define FOSSILIZE_SPAM_LIMIT_WARN 3
#endif
#ifndef FOSSILIZE_SPAM_LIMIT_ERR
#define FOSSILIZE_SPAM_LIMIT_ERR 3
#endif
#define LOGE_LEVEL(...) do { \
if (get_thread_log_level() <= LOG_ERROR) { \
static std::atomic<uint32_t> log_spam_counter; \
uint32_t spam_count = log_spam_counter.load(std::memory_order_relaxed); \
if (spam_count <= FOSSILIZE_SPAM_LIMIT_ERR) \
spam_count = log_spam_counter.fetch_add(1, std::memory_order_relaxed); \
if (spam_count < FOSSILIZE_SPAM_LIMIT_ERR) { \
if (!Internal::log_thread_callback(LOG_ERROR, __VA_ARGS__)) { \
LOGE(__VA_ARGS__); \
} \
} else if (spam_count == FOSSILIZE_SPAM_LIMIT_ERR) { \
const char *basepath_name = Internal::extract_basepath(__FILE__); \
if (!Internal::log_thread_callback(LOG_ERROR, " ... error spam detected at %s:%d, silencing.\n", basepath_name, __LINE__)) { \
LOGW(" ... error spam detected at %s:%d, silencing.\n", basepath_name, __LINE__); \
} \
} \
} \
} while(0)
#define LOGW_LEVEL(...) do { \
if (get_thread_log_level() <= LOG_WARNING) { \
static std::atomic<uint32_t> log_spam_counter; \
uint32_t spam_count = log_spam_counter.load(std::memory_order_relaxed); \
if (spam_count <= FOSSILIZE_SPAM_LIMIT_WARN) \
spam_count = log_spam_counter.fetch_add(1, std::memory_order_relaxed); \
if (spam_count < FOSSILIZE_SPAM_LIMIT_WARN) { \
if (!Internal::log_thread_callback(LOG_WARNING, __VA_ARGS__)) { \
LOGW(__VA_ARGS__); \
} \
} else if (spam_count == FOSSILIZE_SPAM_LIMIT_WARN) { \
const char *basepath_name = Internal::extract_basepath(__FILE__); \
if (!Internal::log_thread_callback(LOG_WARNING, " ... warning spam detected at %s:%d, silencing.\n", basepath_name, __LINE__)) { \
LOGW(" ... warning spam detected at %s:%d, silencing.\n", basepath_name, __LINE__); \
} \
} \
} \
} while(0)
static inline void log_error_pnext_chain(std::string what, const void *pNext)
{
what += " (pNext->sType chain: [";
while (pNext != nullptr)
{
auto *next = static_cast<const VkBaseInStructure *>(pNext);
what += std::to_string(next->sType);
pNext = next->pNext;
if (pNext != nullptr)
what += ", ";
}
what += "])";
LOGW_LEVEL("%s\n", what.c_str());
}
static inline void log_missing_resource(const char *type, Hash hash)
{
LOGW_LEVEL("Referenced %s %016" PRIx64
", but it does not exist.\n"
"This can be expected when replaying an archive from Steam.\n"
"If replaying just the application cache, "
"make sure to replay together with the common cache, "
"as application cache can depend on common cache.\n",
type, hash);
}
static inline void log_invalid_resource(const char *type, Hash hash)
{
LOGW_LEVEL("Referenced %s %016" PRIx64
", but it is VK_NULL_HANDLE.\n"
"The create info was likely not supported by device.\n",
type, hash);
}
template <typename T>
static inline void log_failed_hash(const char *type, T object)
{
LOGW_LEVEL("%s handle 0x%016" PRIx64
" is not registered.\n"
"It has either not been recorded, or it failed to be recorded earlier "
"(which is expected if application uses an extension that is not recognized by Fossilize).\n",
type, (uint64_t)object);
}
}