This repository has been archived by the owner on Sep 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.ts
275 lines (227 loc) · 8.57 KB
/
index.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// adapted from https://github.com/maraisr/diary since it doesn't support ESM
// mixed with debug colors
import pc from "picocolors";
// needed for preconstruct
// eslint-disable-next-line unicorn/import-style, unicorn/prefer-node-protocol
import util from "util";
import type { CallSite } from "callsites";
import humanize from "humanize-duration";
if (typeof process !== "undefined" && typeof util !== "undefined" && util?.inspect?.defaultOptions) {
if (process.env["DEBUG_DEPTH"]) util.inspect.defaultOptions.depth = Number(process.env["DEBUG_DEPTH"]);
util.inspect.defaultOptions.breakLength = 100;
util.inspect.defaultOptions.maxArrayLength = 50;
util.inspect.defaultOptions.maxStringLength = 200;
// util.inspect.defaultOptions.compact = true;
}
export type LogEvent = {
name: string;
level: LogLevels;
messages: unknown[];
color: number;
};
type LogLevels = "fatal" | "error" | "warn" | "debug" | "info" | "log";
export type Reporter = (event: LogEvent) => void;
const namespaceLists = {
allows: [] as RegExp[],
skips: [] as RegExp[],
};
export const enableLogs = (namespaces: string) => {
let i;
const split = (typeof namespaces === "string" ? namespaces : "").split(/[\s,]+/);
const len = split.length;
const allows = [] as RegExp[];
const skips = [] as RegExp[];
for (i = 0; i < len; i++) {
if (!split[i]) {
// ignore empty strings
continue;
}
namespaces = split[i]!.replace(/\*/g, ".*?");
if (namespaces[0] === "-") {
skips.push(new RegExp("^" + namespaces.slice(1) + "$"));
} else {
allows.push(new RegExp("^" + namespaces + "$"));
}
}
namespaceLists.allows = allows;
namespaceLists.skips = skips;
return namespaceLists;
};
export const disableLogs = () => {
namespaceLists.allows = [];
namespaceLists.skips = [];
};
const isEnabled = (name: string) => {
let len = namespaceLists.skips.length;
for (len = namespaceLists.skips.length; len--; ) {
if (namespaceLists.skips[len]?.test(name)) return false;
}
for (len = namespaceLists.allows.length; len--; ) {
if (namespaceLists.allows[len]?.test(name)) return true;
}
return false;
};
if (typeof process !== "undefined" && process.env["DEBUG"]) enableLogs(process.env["DEBUG"]);
function callsites(): CallSite[] {
const _prepareStackTrace = Error.prepareStackTrace;
Error.prepareStackTrace = (_, stack) => stack;
// @ts-expect-error
const stack = new Error().stack.slice(1); // eslint-disable-line unicorn/error-message
Error.prepareStackTrace = _prepareStackTrace;
return stack as any;
}
// ~ Logger
const logger = (
name: string,
reporter: Reporter,
level: LogLevels,
color: LogEvent["color"],
...messages: unknown[]
): void => {
reporter({ name, level, color, messages });
};
let canUseColors = true;
// @ts-expect-error
if (typeof process !== "undefined" && (process?.env?.["NODE_DISABLE_COLORS"] || process?.env?.["FORCE_COLOR"] == 1))
canUseColors = false;
/**
* taken from debug
* https://github.com/debug-js/debug/blob/d1616622e4d404863c5a98443f755b4006e971dc/src/browser.js#L27
*/
const colorMap = new Map<string, number>();
function selectColor(namespace: string): number {
if (!canUseColors) return 0;
if (colorMap.has(namespace)) return colorMap.get(namespace)!;
let hash = 0;
for (let i = 0; i < namespace.length; i++) {
hash = (hash << 5) - hash + (namespace.codePointAt(i) ?? 0);
hash = Math.trunc(hash); // Convert to 32bit integer
}
const result = colors[Math.abs(hash) % colors.length]!;
colorMap.set(namespace, result);
return result;
}
const colors = [
20, 21, 26, 27, 32, 33, 38, 39, 40, 41, 42, 43, 44, 45, 56, 57, 62, 63, 68, 69, 74, 75, 76, 77, 78, 79, 80, 81, 92,
93, 98, 99, 112, 113, 128, 129, 134, 135, 148, 149, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172,
173, 178, 179, 184, 185, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 214, 215, 220, 221,
];
const withColor = (color: LogEvent["color"], str: string) => {
if (!canUseColors) return str;
const colorCode = "\u001B[3" + (color < 8 ? color : "8;5;" + color);
return `${colorCode};1m${str} \u001B[0m`;
};
// ~ Reporter
const humanizeOptions = {
units: ["m", "s", "ms"],
delimiter: " ",
spacer: "",
round: true,
language: "shortEn",
languages: {
shortEn: {
m: () => "min",
s: () => "sec",
ms: () => "ms",
},
},
} as humanize.Options;
// eslint-disable-next-line @typescript-eslint/naming-convention
let DEBUG_STACK = false;
if (typeof process !== "undefined" && process.env["DEBUG_STACK"]) DEBUG_STACK = true;
let prevTime = Date.now();
export const default_reporter: Reporter = (event) => {
const fn = console[event.level === "fatal" ? "error" : event.level];
const color = event.color;
// Set `diff` timestamp
const now = Date.now();
const diff = now - prevTime;
prevTime = now;
let stack: CallSite | undefined;
if (DEBUG_STACK) {
const fullstack = callsites();
const stackIndex = fullstack.findIndex((s) => s.getFileName() !== __filename);
stack = fullstack[stackIndex];
}
fn(
pc.bold(withColor(color, event.name)),
stack
? pc.dim("[" + stack.getFileName() + ":" + stack.getLineNumber() + ":" + stack.getColumnNumber() + "]")
: "",
...event.messages,
pc.bold(withColor(color, `+${humanize(diff, humanizeOptions)}`))
);
};
// ~ Public api
const scopedCache = new Map<string, CreateLoggerReturn>();
export function createLogger(name: string, _onEmit?: Reporter): CreateLoggerReturn {
const onEmit = _onEmit ?? default_reporter;
const color = selectColor(name);
const isLogEnabled = isEnabled(name);
const logFn = isLogEnabled
? (...messages: unknown[]) => logger(name, onEmit, "debug", color, ...messages)
: // eslint-disable-next-line @typescript-eslint/no-empty-function
() => {};
return Object.assign(logFn, {
namespace: name,
extend: (scoped: string) => createLogger(`${name}:${scoped}`, onEmit),
scoped: (scoped: string, ...messages: unknown[]) => {
if (!isEnabled(name + scoped)) return;
const namespace = `${name}:${scoped}`;
const loggerFn = scopedCache.get(namespace) ?? createLogger(namespace, onEmit);
if (!scopedCache.has(namespace)) scopedCache.set(namespace, loggerFn);
loggerFn(...messages);
},
lazy: (fn: () => any) => {
if (!isLogEnabled) return;
logFn(fn());
},
lazyScoped: (scoped: string, fn: () => any) => {
if (!isEnabled(name + scoped)) return;
const namespace = `${name}:${scoped}`;
const loggerFn = scopedCache.get(namespace) ?? createLogger(namespace, onEmit);
if (!scopedCache.has(namespace)) scopedCache.set(namespace, loggerFn);
loggerFn(fn());
},
isEnabled: (namespace?: string) => isEnabled(namespace ?? name),
});
}
type CreateLoggerReturn = ((...args: unknown[]) => void) & {
namespace: string;
extend: (scoped: string) => CreateLoggerReturn;
scoped: (scoped: string, ...messages: unknown[]) => void;
lazy: (fn: () => any) => void;
lazyScoped: (scoped: string, fn: () => any) => void;
isEnabled: (namespace?: string) => boolean;
};
/**
* taken from vite
* https://github.com/vitejs/vite/blob/167753d3754507430600a1bc2b100ca321b17a86/packages/vite/src/node/utils.ts#L360
*/
// export function timeFrom(start: number, subtract = 0): string {
// const time: number | string = performance.now() - start - subtract;
// const timeString = (time.toFixed(2) + "ms").padEnd(5, " ");
// if (time < 10) {
// return pc.green(timeString);
// } else if (time < 50) {
// return pc.yellow(timeString);
// } else {
// return pc.red(timeString);
// }
// }
// const printColors = () => {
// for (let i = 0; i < 16; i++) {
// for (let j = 0; j < 16; j++) {
// const color = i * 16 + j;
// process.stdout.write(`\u001B[38;5;${color}m${String(color).padEnd(4)}\u001B[0m`);
// }
// console.log("");
// }
// for (let i = 0; i < 16; i++) {
// for (let j = 0; j < 16; j++) {
// const color = i * 16 + j;
// process.stdout.write(`\u001B[48;5;${color}m${String(color).padEnd(4)}\u001B[0m`);
// }
// console.log("");
// }
// }