From c4c23e55587ae61380c70a9b793541d56b11c387 Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Fri, 19 Feb 2016 15:02:17 +0800 Subject: [PATCH] refactor CCLogs --- CCDebugger.js | 72 +++++++++++++++++++++++++++++++-------------------- predefine.js | 12 +++------ 2 files changed, 48 insertions(+), 36 deletions(-) diff --git a/CCDebugger.js b/CCDebugger.js index e59b724c39..a027b183af 100644 --- a/CCDebugger.js +++ b/CCDebugger.js @@ -326,11 +326,13 @@ cc._formatString = function (arg) { return arg; }; +var Enum = require('./cocos2d/core/value-types/CCEnum'); + /** * Enum for debug modes. * @enum DebugMode */ -cc.DebugMode = cc.Enum({ +cc.DebugMode = Enum({ /** * @property {number} NONE - The debug mode none. */ @@ -371,6 +373,9 @@ cc.DebugMode = cc.Enum({ * @param {DebugMode} mode */ cc._initDebugSetting = function (mode) { + // reset + cc.log = cc.warn = cc.error = cc._throw = cc.assert = function () {}; + if(mode === cc.DebugMode.NONE) return; @@ -414,12 +419,15 @@ cc._initDebugSetting = function (mode) { * @param {any} obj - 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 (console.error.bind) { + 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 = function(){ + cc.error = function () { return console.error.apply(console, arguments); }; } @@ -430,7 +438,7 @@ cc._initDebugSetting = function (mode) { throw new Error(msg); } }; - if(mode !== cc.DebugMode.ERROR) + if (mode !== cc.DebugMode.ERROR) { /** * Outputs a warning message to the Cocos Creator Console (editor) or Web Console (runtime). * - In Cocos Creator, warning is yellow. @@ -439,37 +447,45 @@ cc._initDebugSetting = function (mode) { * @param {any} obj - 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 (console.warn.bind) { + 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 = function() { + cc.warn = function () { return console.warn.apply(console, arguments); }; } - if(mode === cc.DebugMode.INFO) { - /** - * Outputs a message to the Cocos Creator Console (editor) or Web Console (runtime). - * @method log - * @param {any} obj - 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. - */ - cc.log = function () { - return console.log.apply(console, arguments); - }; - /** - * 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. - * @method info - * @param {any} obj - 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. - */ - cc.info = function () { - (console.info || console.log).apply(console, arguments); - }; - } + } + if (CC_EDITOR) { + cc.log = Editor.log; + cc.info = Editor.info; + } + else if (mode === cc.DebugMode.INFO) { + /** + * Outputs a message to the Cocos Creator Console (editor) or Web Console (runtime). + * @method log + * @param {any} obj - 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. + */ + cc.log = function () { + return console.log.apply(console, arguments); + }; + /** + * 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. + * @method info + * @param {any} obj - 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. + */ + cc.info = function () { + (console.info || console.log).apply(console, arguments); + }; + } } cc._throw = function (error) { cc.error(error.stack || error); diff --git a/predefine.js b/predefine.js index 6903db375d..01ee24c25b 100644 --- a/predefine.js +++ b/predefine.js @@ -32,9 +32,6 @@ cc = {}; // The namespace for original nodes rendering in scene graph. _ccsg = {}; -//to make sure the cc.log, cc.warn, cc.error, cc._throw and cc.assert would not throw error before init by debugger mode. -cc.log = cc.warn = cc.error = cc._throw = cc.assert = function () {}; - if (CC_DEV) { /** * contains internal apis for unit tests @@ -43,6 +40,10 @@ if (CC_DEV) { cc._Test = {}; } +// output all info before initialized +require('./CCDebugger'); +cc._initDebugSetting(cc.DebugMode.INFO); + // polyfills /* require('./polyfill/bind'); */ require('./polyfill/string'); @@ -57,11 +58,6 @@ require('./cocos2d/core/event'); require('./cocos2d/core/platform/CCSys'); require('./cocos2d/core/platform/CCLoader'); require('./cocos2d/core/load-pipeline'); -require('./CCDebugger'); -if (CC_DEV) { - // output all errors before initialized - cc._initDebugSetting(1); // DEBUG_MODE_INFO -} require('./cocos2d/core/CCDirector'); require('./cocos2d/core/CCDirectorWebGL'); require('./cocos2d/core/CCDirectorCanvas');