-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug.ts
127 lines (112 loc) · 3.11 KB
/
debug.ts
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import * as colors from "./colors.ts";
export type ColorFunction = (message: string) => string;
export const colorFunctions: ColorFunction[] = [
colors.red,
colors.green,
colors.yellow,
colors.blue,
colors.magenta,
colors.cyan,
];
function hashCode(s: string): number {
let h = 0;
const l = s.length;
let i = 0;
if (l > 0) while (i < l) h = ((h << 5) - h + s.charCodeAt(i++)) | 0;
return h;
}
function generateColor(message: string): ColorFunction {
const hash = Math.abs(hashCode(message));
return colorFunctions[hash % colorFunctions.length];
}
export interface Debug {
(fmt: string, ...args: unknown[]): void;
self: Debugger;
}
export class Debugger {
manager: DebugManager;
ns: string;
color: ColorFunction;
last: number;
enabled: boolean;
constructor(manager: DebugManager, namespace: string) {
this.manager = manager;
this.ns = namespace;
this.color = generateColor(namespace);
this.last = 0;
this.enabled = manager.enabled.some((r) => r.test(namespace));
}
log(fmt: string, ...args: unknown[]): void {
if (!this.enabled) return;
const diff = Date.now() - (this.last || Date.now());
fmt = format(fmt, ...args);
const msg = `${this.color(this.ns)} ${fmt} ${this.color(`+${diff}ms`)}`;
console.debug(msg);
this.last = Date.now();
}
}
export function format(f: string, ...args: unknown[]) {
let i = 0;
const len = args.length;
let str = String(f).replace(/%[sdjoO%]/g, (x: string): string => {
if (x === "%%") return "%";
if (i >= len) return x;
switch (x) {
case "%s":
return String(args[i++]);
case "%d":
return Number(args[i++]).toString();
case "%o":
return Deno.inspect(args[i++])
.split("\n")
.map((_) => _.trim())
.join(" ");
case "%O":
return Deno.inspect(args[i++]);
case "%j":
try {
return JSON.stringify(args[i++]);
} catch {
return "[Circular]";
}
default:
return x;
}
});
for (const x of args.splice(i)) {
if (x === null || !(typeof x === "object" && x !== null)) {
str += " " + x;
} else {
str += " " + Deno.inspect(x);
}
}
return str;
}
class DebugManager {
debuggers: Map<string, Debugger>;
enabled: RegExp[];
constructor(enabled?: RegExp[]) {
this.debuggers = new Map();
this.enabled = enabled ?? [];
}
}
function extract(opts?: string): RegExp[] {
if (!opts || opts.length === 0) return [];
opts = opts.replace(/\s/g, "").replace(/\*/g, ".+");
return opts.split(",").map((rule) => new RegExp(`^${rule}$`));
}
let manager: DebugManager;
export function withoutEnv(enabled?: RegExp[] | string) {
if (!enabled) enabled = [];
if (typeof enabled === "string") enabled = extract(enabled);
manager = new DebugManager(enabled);
}
export function debug(namespace: string): Debug {
if (!manager) manager = new DebugManager(extract(Deno.env.get("DEBUG")));
const dbg = new Debugger(manager, namespace);
manager.debuggers.set(namespace, dbg);
const de: Debug = Object.assign(dbg.log.bind(dbg), {
self: dbg,
});
return de;
}