-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
47 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import debug, { Debugger } from 'debug'; | ||
import async_hooks from 'async_hooks'; | ||
|
||
const debugInstances: {[namespace: string]: {[postfix: string]: Debugger}} = {}; | ||
const asyncContext = new Map(); | ||
|
||
async_hooks.createHook({ | ||
init(asyncId, type, triggerAsyncId, resource) { | ||
if (asyncContext.has(triggerAsyncId)) { | ||
asyncContext.set(asyncId, asyncContext.get(triggerAsyncId)); | ||
} | ||
}, | ||
destroy(asyncId) { | ||
asyncContext.delete(asyncId); | ||
} | ||
}).enable(); | ||
|
||
export function withDebugContext<F extends (...args: any[]) => any>(postfix: string, fn: F) { | ||
return async (...args: Parameters<F>): Promise<Awaited<ReturnType<F>>> => { | ||
const asyncId = async_hooks.executionAsyncId(); | ||
asyncContext.set(asyncId, postfix); | ||
try { | ||
return await fn(...args); | ||
} finally { | ||
asyncContext.delete(asyncId); | ||
} | ||
}; | ||
} | ||
|
||
function getContext() { | ||
const asyncId = async_hooks.executionAsyncId(); | ||
return asyncContext.get(asyncId); | ||
} | ||
|
||
export function Debug(namespace: string) { | ||
debugInstances[namespace] = {'': debug(namespace)}; | ||
return function(...args: Parameters<Debugger>) { | ||
if (!debugInstances[namespace][getContext() ?? '']) { | ||
debugInstances[namespace][getContext()] = debug(namespace + getContext()); | ||
} | ||
debugInstances[namespace][getContext() ?? ''](...args); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters