Repository navigation
Expand file tree
/
Copy pathdebug.js
More file actions
216 lines (198 loc) · 6.01 KB
/
Copy pathdebug.js
File metadata and controls
216 lines (198 loc) · 6.01 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import { Tracing } from './tracing.js';
/**
* Engine debug log system. Note that the logging only executes in the debug build of the engine,
* and is stripped out in other builds.
*
* The debug build ships in the npm package: import from `'playcanvas/debug'` instead of
* `'playcanvas'` to enable assertions, deprecation notices and validation warnings. A
* `'playcanvas/profiler'` build is also available for per-frame timings.
*/
class Debug {
/**
* Set storing already logged messages, to only print each unique message one time.
*
* @type {Set<string>}
* @private
*/
static _loggedMessages = new Set();
/**
* Deprecated warning message.
*
* @param {string} message - The message to log.
*/
static deprecated(message) {
if (!Debug._loggedMessages.has(message)) {
Debug._loggedMessages.add(message);
console.warn(`DEPRECATED: ${message}`);
}
}
/**
* Removed warning message.
*
* @param {string} message - The message to log.
*/
static removed(message) {
if (!Debug._loggedMessages.has(message)) {
Debug._loggedMessages.add(message);
console.error(`REMOVED: ${message}`);
}
}
/**
* Assertion deprecated message. If the assertion is false, the deprecated message is written to the log.
*
* @param {*} assertion - The assertion to check. Any falsy value fails.
* @param {string} message - The message to log.
*/
static assertDeprecated(assertion, message) {
if (!assertion) {
Debug.deprecated(message);
}
}
/**
* Assertion error message. If the assertion is false, the error message is written to the log.
*
* @param {*} assertion - The assertion to check. Any falsy value fails.
* @param {...*} args - The values to be written to the log.
*/
static assert(assertion, ...args) {
if (!assertion) {
console.error('ASSERT FAILED: ', ...args);
}
}
/**
* Assertion error message that writes an error message to the log if the object has already
* been destroyed. To be used along setDestroyed.
*
* @param {object} object - The object to check.
*/
static assertDestroyed(object) {
if (object?.__alreadyDestroyed) {
const message = `[${object.constructor?.name}] with name [${object.name}] has already been destroyed, and cannot be used.`;
if (!Debug._loggedMessages.has(message)) {
Debug._loggedMessages.add(message);
console.error('ASSERT FAILED: ', message, object);
}
}
}
/**
* Executes a function in debug mode only.
*
* @param {Function} func - Function to call.
*/
static call(func) {
func();
}
/**
* Info message.
*
* @param {...*} args - The values to be written to the log.
*/
static log(...args) {
console.log(...args);
}
/**
* Info message logged no more than once.
*
* @param {string} message - The message to log.
* @param {...*} args - The values to be written to the log.
*/
static logOnce(message, ...args) {
if (!Debug._loggedMessages.has(message)) {
Debug._loggedMessages.add(message);
console.log(message, ...args);
}
}
/**
* Warning message.
*
* @param {...*} args - The values to be written to the log.
*/
static warn(...args) {
console.warn(...args);
}
/**
* Warning message logged no more than once.
*
* @param {string} message - The message to log.
* @param {...*} args - The values to be written to the log.
*/
static warnOnce(message, ...args) {
if (!Debug._loggedMessages.has(message)) {
Debug._loggedMessages.add(message);
console.warn(message, ...args);
}
}
/**
* Error message.
*
* @param {...*} args - The values to be written to the log.
*/
static error(...args) {
console.error(...args);
}
/**
* Error message logged no more than once.
*
* @param {string} message - The message to log.
* @param {...*} args - The values to be written to the log.
*/
static errorOnce(message, ...args) {
if (!Debug._loggedMessages.has(message)) {
Debug._loggedMessages.add(message);
console.error(message, ...args);
}
}
/**
* Trace message, which is logged to the console if the tracing for the channel is enabled
*
* @param {string} channel - The trace channel
* @param {...*} args - The values to be written to the log.
*/
static trace(channel, ...args) {
if (Tracing.get(channel)) {
console.groupCollapsed(`${channel.padEnd(20, ' ')}|`, ...args);
if (Tracing.stack) {
console.trace();
}
console.groupEnd();
}
}
}
/**
* A helper debug functionality.
*/
class DebugHelper {
/**
* Set a name to the name property of the object. Executes only in the debug build.
*
* @param {object} object - The object to assign the name to.
* @param {string} name - The name to assign.
*/
static setName(object, name) {
if (object) {
object.name = name;
}
}
/**
* Set a label to the label property of the object. Executes only in the debug build.
*
* @param {object} object - The object to assign the name to.
* @param {string} label - The label to assign.
*/
static setLabel(object, label) {
if (object) {
object.label = label;
}
}
/**
* Marks object as destroyed. Executes only in the debug build. To be used along assertDestroyed.
*
* @param {object} object - The object to mark as destroyed.
*/
static setDestroyed(object) {
if (object) {
object.__alreadyDestroyed = true;
}
}
}
export { Debug, DebugHelper };