[console.js] Add logging method from common.js
This commit is contained in:
parent
392642751f
commit
fa1a194b71
3 changed files with 18 additions and 16 deletions
|
@ -2,7 +2,7 @@
|
|||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
var Logger = (function () {
|
||||
var logger = (function () {
|
||||
let color = {
|
||||
red: "\033[1;31m",
|
||||
green: "\033[1;32m",
|
||||
|
@ -61,6 +61,16 @@ var Logger = (function () {
|
|||
dumpln(color.green + sep + color.clear);
|
||||
}
|
||||
|
||||
function traceback() {
|
||||
error("===[ Traceback ]");
|
||||
try {
|
||||
throw new Error();
|
||||
} catch (e) {
|
||||
dump(e.stack || e.stacktrace || "");
|
||||
}
|
||||
error("===");
|
||||
}
|
||||
|
||||
return {
|
||||
console: console,
|
||||
dump: dump,
|
||||
|
@ -69,6 +79,7 @@ var Logger = (function () {
|
|||
dumpln: dumpln,
|
||||
comment: comment,
|
||||
testcase: testcase,
|
||||
separator: separator
|
||||
separator: separator,
|
||||
traceback: traceback
|
||||
};
|
||||
})();
|
||||
|
|
|
@ -38,7 +38,7 @@ var random = {
|
|||
range: function (start, limit) {
|
||||
// Returns an integer in [start, limit]. Uniform distribution.
|
||||
if (isNaN(start) || isNaN(limit)) {
|
||||
Utils.traceback();
|
||||
logger.traceback();
|
||||
throw new TypeError("random.range() received a non number type: '" + start + "', '" + limit + "')");
|
||||
}
|
||||
return this.number(limit - start + 1) + start;
|
||||
|
@ -49,7 +49,7 @@ var random = {
|
|||
},
|
||||
item: function (list) {
|
||||
if (!(list instanceof Array || (list !== undefined && typeof list != "string" && list.hasOwnProperty("length")))) {
|
||||
//Utils.traceback();
|
||||
logger.traceback();
|
||||
throw new TypeError("this.item() received a non array type: '" + list + "'");
|
||||
}
|
||||
return list[this.number(list.length)];
|
||||
|
@ -78,14 +78,14 @@ var random = {
|
|||
limit = 2;
|
||||
}
|
||||
if (isNaN(limit)) {
|
||||
Utils.traceback();
|
||||
logger.traceback();
|
||||
throw new TypeError("random.chance() received a non number type: '" + limit + "'");
|
||||
}
|
||||
return this.number(limit) == 1;
|
||||
},
|
||||
choose: function (list, flat) {
|
||||
if (!(list instanceof Array)) {
|
||||
Utils.traceback();
|
||||
logger.traceback();
|
||||
throw new TypeError("random.choose() received a non-array type: '" + list + "'");
|
||||
}
|
||||
let total = 0;
|
||||
|
@ -137,7 +137,7 @@ var random = {
|
|||
},
|
||||
subset: function (list, limit) {
|
||||
if (!(list instanceof Array)) {
|
||||
Utils.traceback();
|
||||
logger.traceback();
|
||||
throw new TypeError("random.some() received a non-array type: '" + list + "'");
|
||||
}
|
||||
if (typeof limit !== 'number') {
|
||||
|
|
|
@ -63,14 +63,5 @@ utils.common = {
|
|||
}
|
||||
}
|
||||
return obj1;
|
||||
},
|
||||
traceback: function () {
|
||||
Logger.error("===[ Traceback ]");
|
||||
try {
|
||||
throw new Error();
|
||||
} catch (e) {
|
||||
Logger.dump(e.stack || e.stacktrace || "");
|
||||
}
|
||||
Logger.error("===");
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue