forked from cocos/cocos-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CCDebugger.js
363 lines (334 loc) · 13.4 KB
/
CCDebugger.js
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
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.
****************************************************************************/
var Enum = require('./cocos2d/core/platform/CCEnum');
// the html element displays log in web page (cc.DebugMode.INFO_FOR_WEB_PAGE)
var logList;
/**
* !#en Enum for debug modes.
* !#zh 调试模式
* @enum DebugMode
*/
cc.DebugMode = Enum({
/**
* !#en The debug mode none.
* !#zh 禁止模式,禁止显示任何日志信息。
* @property NONE
* @type {Number}
* @static
*/
NONE: 0,
/**
* !#en The debug mode info.
* !#zh 信息模式,在 console 中显示所有日志。
* @property INFO
* @type {Number}
* @static
*/
INFO: 1,
/**
* !#en The debug mode warn.
* !#zh 警告模式,在 console 中只显示 warn 级别以上的(包含 error)日志。
* @property WARN
* @type {Number}
* @static
*/
WARN: 2,
/**
* !#en The debug mode error.
* !#zh 错误模式,在 console 中只显示 error 日志。
* @property ERROR
* @type {Number}
* @static
*/
ERROR: 3,
/**
* !#en The debug mode info for web page.
* !#zh 信息模式(仅 WEB 端有效),在画面上输出所有信息。
* @property INFO_FOR_WEB_PAGE
* @type {Number}
* @static
*/
INFO_FOR_WEB_PAGE: 4,
/**
* !#en The debug mode warn for web page.
* !#zh 警告模式(仅 WEB 端有效),在画面上输出 warn 级别以上的(包含 error)信息。
* @property WARN_FOR_WEB_PAGE
* @type {Number}
* @static
*/
WARN_FOR_WEB_PAGE: 5,
/**
* !#en The debug mode error for web page.
* !#zh 错误模式(仅 WEB 端有效),在画面上输出 error 信息。
* @property ERROR_FOR_WEB_PAGE
* @type {Number}
* @static
*/
ERROR_FOR_WEB_PAGE: 6
});
/**
* !#en Init Debug setting.
* !#zh 设置调试模式。
* @method _initDebugSetting
* @param {DebugMode} mode
*/
cc._initDebugSetting = function (mode) {
// reset
cc.log = cc.warn = cc.error = cc.assert = function () { };
if (mode === cc.DebugMode.NONE)
return;
if (!CC_JSB && mode > cc.DebugMode.ERROR) {
//log to web page
function logToWebPage (msg) {
if (!cc._canvas)
return;
if (!logList) {
var logDiv = document.createElement("Div");
logDiv.setAttribute("id", "logInfoDiv");
logDiv.setAttribute("width", "200");
logDiv.setAttribute("height", cc._canvas.height);
var logDivStyle = logDiv.style;
logDivStyle.zIndex = "99999";
logDivStyle.position = "absolute";
logDivStyle.top = logDivStyle.left = "0";
logList = document.createElement("textarea");
logList.setAttribute("rows", "20");
logList.setAttribute("cols", "30");
logList.setAttribute("disabled", "true");
var logListStyle = logList.style;
logListStyle.backgroundColor = "transparent";
logListStyle.borderBottom = "1px solid #cccccc";
logListStyle.borderTopWidth = logListStyle.borderLeftWidth = logListStyle.borderRightWidth = "0px";
logListStyle.borderTopStyle = logListStyle.borderLeftStyle = logListStyle.borderRightStyle = "none";
logListStyle.padding = "0px";
logListStyle.margin = 0;
logDiv.appendChild(logList);
cc._canvas.parentNode.appendChild(logDiv);
}
logList.value = logList.value + msg + "\r\n";
logList.scrollTop = logList.scrollHeight;
}
cc.error = function () {
logToWebPage("ERROR : " + cc.js.formatStr.apply(null, arguments));
};
cc.assert = function (cond, msg) {
'use strict';
if (!cond && msg) {
msg = cc.js.formatStr.apply(null, cc.js.shiftArguments.apply(null, arguments));
logToWebPage("ASSERT: " + msg);
}
};
if (mode !== cc.DebugMode.ERROR_FOR_WEB_PAGE) {
cc.warn = function () {
logToWebPage("WARN : " + cc.js.formatStr.apply(null, arguments));
};
}
if (mode === cc.DebugMode.INFO_FOR_WEB_PAGE) {
cc.log = cc.info = function () {
logToWebPage(cc.js.formatStr.apply(null, arguments));
};
}
}
else if (console && console.log.apply) {//console is null when user doesn't open dev tool on IE9
//log to console
// For JSB
if (!console.error) console.error = console.log;
if (!console.warn) console.warn = console.log;
/**
* !#en
* Outputs an error message to the Cocos Creator Console (editor) or Web Console (runtime).<br/>
* - In Cocos Creator, error is red.<br/>
* - In Chrome, error have a red icon along with red message text.<br/>
* !#zh
* 输出错误消息到 Cocos Creator 编辑器的 Console 或运行时页面端的 Console 中。<br/>
* - 在 Cocos Creator 中,错误信息显示是红色的。<br/>
* - 在 Chrome 中,错误信息有红色的图标以及红色的消息文本。<br/>
*
* @method error
* @param {any} msg - A JavaScript string containing zero or more substitution strings.
* @param {any} ...subst - JavaScript objects with which to replace substitution strings within msg. This gives you additional control over the format of the output.
*/
if (CC_EDITOR) {
cc.error = Editor.error;
}
else if (console.error.bind) {
// use bind to avoid pollute call stacks
cc.error = console.error.bind(console);
}
else {
cc.error = CC_JSB ? console.error : function () {
return console.error.apply(console, arguments);
};
}
cc.assert = function (cond, msg) {
if (!cond) {
if (msg) {
msg = cc.js.formatStr.apply(null, cc.js.shiftArguments.apply(null, arguments));
}
if (CC_DEV) {
debugger;
}
if (CC_TEST) {
ok(false, msg);
}
else {
throw new Error(msg);
}
}
}
}
if (mode !== cc.DebugMode.ERROR) {
/**
* !#en
* Outputs a warning message to the Cocos Creator Console (editor) or Web Console (runtime).
* - In Cocos Creator, warning is yellow.
* - In Chrome, warning have a yellow warning icon with the message text.
* !#zh
* 输出警告消息到 Cocos Creator 编辑器的 Console 或运行时 Web 端的 Console 中。<br/>
* - 在 Cocos Creator 中,警告信息显示是黄色的。<br/>
* - 在 Chrome 中,警告信息有着黄色的图标以及黄色的消息文本。<br/>
* @method warn
* @param {any} msg - A JavaScript string containing zero or more substitution strings.
* @param {any} ...subst - JavaScript objects with which to replace substitution strings within msg. This gives you additional control over the format of the output.
*/
if (CC_EDITOR) {
cc.warn = Editor.warn;
}
else if (console.warn.bind) {
// use bind to avoid pollute call stacks
cc.warn = console.warn.bind(console);
}
else {
cc.warn = CC_JSB ? console.warn : function () {
return console.warn.apply(console, arguments);
};
}
}
if (CC_EDITOR) {
cc.log = Editor.log;
cc.info = Editor.info;
}
else if (mode === cc.DebugMode.INFO) {
/**
* !#en Outputs a message to the Cocos Creator Console (editor) or Web Console (runtime).
* !#zh 输出一条消息到 Cocos Creator 编辑器的 Console 或运行时 Web 端的 Console 中。
* @method log
* @param {String|any} msg - A JavaScript string containing zero or more substitution strings.
* @param {any} ...subst - JavaScript objects with which to replace substitution strings within msg. This gives you additional control over the format of the output.
*/
if (CC_JSB) {
if (scriptEngineType === "JavaScriptCore") {
// console.log has to use `console` as its context for iOS 8~9. Therefore, apply it.
cc.log = function () {
return console.log.apply(console, arguments);
};
} else {
cc.log = console.log;
}
}
else if (console.log.bind) {
// use bind to avoid pollute call stacks
cc.log = console.log.bind(console);
}
else {
cc.log = function () {
return console.log.apply(console, arguments);
};
}
/**
* !#en
* Outputs an informational message to the Cocos Creator Console (editor) or Web Console (runtime).
* - In Cocos Creator, info is blue.
* - In Firefox and Chrome, a small "i" icon is displayed next to these items in the Web Console's log.
* !#zh
* 输出一条信息消息到 Cocos Creator 编辑器的 Console 或运行时 Web 端的 Console 中。
* - 在 Cocos Creator 中,Info 信息显示是蓝色的。<br/>
* - 在 Firefox 和 Chrome 中,Info 信息有着小 “i” 图标。
* @method info
* @param {any} msg - A JavaScript string containing zero or more substitution strings.
* @param {any} ...subst - JavaScript objects with which to replace substitution strings within msg. This gives you additional control over the format of the output.
*/
if (CC_JSB) {
cc.info = (scriptEngineType === "JavaScriptCore") ? function () {
(console.info || console.log).apply(console, arguments);
} : (console.info || console.log);
}
else {
cc.info = function () {
(console.info || console.log).apply(console, arguments);
};
}
}
};
cc._throw = CC_EDITOR ? Editor.error : function (error) {
var stack = error.stack;
if (stack) {
cc.error(CC_JSB ? (error + '\n' + stack) : stack);
}
else {
cc.error(error);
}
};
// define log methods to lookup message ID
const debugInfos = CC_DEBUG && require('./DebugInfos');
const ERROR_MAP_URL = 'https://github.com/cocos-creator/engine/blob/master/EngineErrorMap.md';
function getTypedFormatter (type) {
return function () {
var id = arguments[0];
var msg = CC_DEBUG ? (debugInfos[id] || 'unknown id') : `${type} ${id}, please go to ${ERROR_MAP_URL}#${id} to see details.`;
if (arguments.length === 1) {
return msg;
}
else if (arguments.length === 2) {
return CC_DEBUG ? cc.js.formatStr(msg, arguments[1]) :
msg + ' Arguments: ' + arguments[1];
}
else {
var argsArray = cc.js.shiftArguments.apply(null, arguments);
return CC_DEBUG ? cc.js.formatStr.apply(null, [msg].concat(argsArray)) :
msg + ' Arguments: ' + argsArray.join(', ');
}
};
}
var logFormatter = getTypedFormatter('Log');
cc.logID = function () {
cc.log(logFormatter.apply(null, arguments));
};
var warnFormatter = getTypedFormatter('Warning');
cc.warnID = function () {
cc.warn(warnFormatter.apply(null, arguments));
};
var errorFormatter = getTypedFormatter('Error');
cc.errorID = function () {
cc.error(errorFormatter.apply(null, arguments));
};
var assertFormatter = getTypedFormatter('Assert');
cc.assertID = function (cond) {
'use strict';
if (cond) {
return;
}
cc.assert(false, assertFormatter.apply(null, cc.js.shiftArguments.apply(null, arguments)));
};
cc._getError = getTypedFormatter('ERROR');
// output all info by default before initialized
cc._initDebugSetting(cc.DebugMode.INFO);