[console.js] Add logging method from common.js

This commit is contained in:
Christoph Diehl 2017-04-23 00:14:04 +03:00
parent 392642751f
commit fa1a194b71
3 changed files with 18 additions and 16 deletions

View file

@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * 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/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
var Logger = (function () { var logger = (function () {
let color = { let color = {
red: "\033[1;31m", red: "\033[1;31m",
green: "\033[1;32m", green: "\033[1;32m",
@ -61,6 +61,16 @@ var Logger = (function () {
dumpln(color.green + sep + color.clear); dumpln(color.green + sep + color.clear);
} }
function traceback() {
error("===[ Traceback ]");
try {
throw new Error();
} catch (e) {
dump(e.stack || e.stacktrace || "");
}
error("===");
}
return { return {
console: console, console: console,
dump: dump, dump: dump,
@ -69,6 +79,7 @@ var Logger = (function () {
dumpln: dumpln, dumpln: dumpln,
comment: comment, comment: comment,
testcase: testcase, testcase: testcase,
separator: separator separator: separator,
traceback: traceback
}; };
})(); })();

View file

@ -38,7 +38,7 @@ var random = {
range: function (start, limit) { range: function (start, limit) {
// Returns an integer in [start, limit]. Uniform distribution. // Returns an integer in [start, limit]. Uniform distribution.
if (isNaN(start) || isNaN(limit)) { if (isNaN(start) || isNaN(limit)) {
Utils.traceback(); logger.traceback();
throw new TypeError("random.range() received a non number type: '" + start + "', '" + limit + "')"); throw new TypeError("random.range() received a non number type: '" + start + "', '" + limit + "')");
} }
return this.number(limit - start + 1) + start; return this.number(limit - start + 1) + start;
@ -49,7 +49,7 @@ var random = {
}, },
item: function (list) { item: function (list) {
if (!(list instanceof Array || (list !== undefined && typeof list != "string" && list.hasOwnProperty("length")))) { 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 + "'"); throw new TypeError("this.item() received a non array type: '" + list + "'");
} }
return list[this.number(list.length)]; return list[this.number(list.length)];
@ -78,14 +78,14 @@ var random = {
limit = 2; limit = 2;
} }
if (isNaN(limit)) { if (isNaN(limit)) {
Utils.traceback(); logger.traceback();
throw new TypeError("random.chance() received a non number type: '" + limit + "'"); throw new TypeError("random.chance() received a non number type: '" + limit + "'");
} }
return this.number(limit) == 1; return this.number(limit) == 1;
}, },
choose: function (list, flat) { choose: function (list, flat) {
if (!(list instanceof Array)) { if (!(list instanceof Array)) {
Utils.traceback(); logger.traceback();
throw new TypeError("random.choose() received a non-array type: '" + list + "'"); throw new TypeError("random.choose() received a non-array type: '" + list + "'");
} }
let total = 0; let total = 0;
@ -137,7 +137,7 @@ var random = {
}, },
subset: function (list, limit) { subset: function (list, limit) {
if (!(list instanceof Array)) { if (!(list instanceof Array)) {
Utils.traceback(); logger.traceback();
throw new TypeError("random.some() received a non-array type: '" + list + "'"); throw new TypeError("random.some() received a non-array type: '" + list + "'");
} }
if (typeof limit !== 'number') { if (typeof limit !== 'number') {

View file

@ -63,14 +63,5 @@ utils.common = {
} }
} }
return obj1; return obj1;
},
traceback: function () {
Logger.error("===[ Traceback ]");
try {
throw new Error();
} catch (e) {
Logger.dump(e.stack || e.stacktrace || "");
}
Logger.error("===");
} }
}; };