-
Notifications
You must be signed in to change notification settings - Fork 0
/
console_enhancer.js
76 lines (69 loc) · 2.12 KB
/
console_enhancer.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
/**
Created by Complynx on 22.03.2019,
http://complynx.net
<[email protected]> Daniel Drizhuk
*/
import {getTrace} from "./trace.js";
import {isFunction} from "./type_checks.js";
let wrap = ["info", "log", "debug"];
let bind = ['error', 'warn'];
function msStr(ms){
let sec = "" + (ms/1000.);
return " ".repeat(Math.max(0, 9 - sec.length)) + sec;
}
/**
* Tired of boring plain logs? Try this to enhance your logger readability.
* Useful for modules.
*/
class XConsole{
constructor(name = "XConsole", _console = console){
this.module = name;
this.console = _console;
let self = this;
this.start = Date.now();
for(let i of bind){
this[i] = this[i].bind(this);
}
for(let i of wrap){
this[i] = function () {
return _console[i].apply(_console, XConsole.concatenatePrefix(self.prefix(), arguments));
};
}
for(let i in _console){
if(!(i in this)){
this[i] = function () {
return _console[i].apply(_console, arguments);
};
}
}
if(!this.trace)
this.trace = function () {
let tr = getTrace();
tr.shift();
this.log(tr);
}
}
prefix(){
return ["%c%s %c[%c%s%c]:\t", "color:#aaa;", msStr(Date.now() - this.start), "color:#000;", "color:#00f;", this.module, "color:#000;"];
}
static concatenatePrefix(prefix, args){
args = Array.from(args);
if(typeof args[0] === "string"){
prefix[0] += args.shift();
}
return prefix.concat(args);
}
error(){
let prefix = this.prefix();
prefix[0] += '%c';
prefix.push("color:#a4000f;");
return this.console.error.apply(this.console, XConsole.concatenatePrefix(prefix, arguments));
}
warn(){
let prefix = this.prefix();
prefix[0] += '%c';
prefix.push("color:#715100;");
return this.console.warn.apply(this.console, XConsole.concatenatePrefix(prefix, arguments));
}
}
export {XConsole};