forked from Adornis/typescript-compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.js
77 lines (62 loc) · 1.47 KB
/
logger.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
const util = Npm.require("util");
const chalk = Npm.require("chalk");
class Logger_ {
constructor() {
this.llevel = process.env.TYPESCRIPT_LOG;
this.disableWarnings = process.env.TYPESCRIPT_DISABLE_WARNINGS;
}
newProfiler(name) {
let profiler = new Profiler(name);
if (this.isProfile) profiler.start();
return profiler;
}
get isDebug() {
return this.llevel >= 2;
}
get isProfile() {
return this.llevel >= 3;
}
get isAssert() {
return this.llevel >= 4;
}
log(msg, ...args) {
if (this.llevel >= 1) {
console.log.apply(null, [msg].concat(args));
}
}
warn(msg, ...other) {
if (!this.disableWarnings) process.stdout.write(chalk.bold.yellow(msg) + chalk.reset(other.join(" ")) + "\n");
}
error(msg, ...other) {
process.stdout.write(chalk.bold.red(msg) + chalk.reset(other.join(" ")) + "\n");
}
info(msg) {
process.stdout.write(chalk.bold.green(msg) + chalk.dim(" ") + "\n");
}
debug(msg, ...args) {
if (this.isDebug) {
this.log.apply(this, msg, args);
}
}
assert(msg, ...args) {
if (this.isAssert) {
this.log.apply(this, msg, args);
}
}
}
Logger = new Logger_();
class Profiler {
constructor(name) {
this.name = name;
}
start() {
console.log("%s started", this.name);
console.time(util.format("%s time", this.name));
this._started = true;
}
end() {
if (this._started) {
console.timeEnd(util.format("%s time", this.name));
}
}
}