1 |
|
2 | export type DebugFunction = (...args: any[]) => void
|
3 | export type DebugInitializer = (name: string) => DebugFunction
|
4 |
|
5 | const debuggers: { [key: string]: DebugFunction } = {}
|
6 |
|
7 | export function noopDebug(): DebugFunction {
|
8 | return function () {}
|
9 | }
|
10 |
|
11 | let defaultInitializer: DebugInitializer = noopDebug
|
12 |
|
13 | export function setDebug(debug: DebugInitializer) {
|
14 | defaultInitializer = debug
|
15 |
|
16 | Object.keys(debuggers).forEach((name) => {
|
17 | debuggers[name] = debug(name)
|
18 | })
|
19 | }
|
20 |
|
21 | export function createDebug(name: string) {
|
22 | if (!debuggers[name]) {
|
23 | debuggers[name] = defaultInitializer(name)
|
24 | }
|
25 |
|
26 | return (...args: any[]) => debuggers[name](...args)
|
27 | }
|