-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathconsole.ts
More file actions
42 lines (36 loc) · 1.41 KB
/
console.ts
File metadata and controls
42 lines (36 loc) · 1.41 KB
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
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/ban-types */
import type { ConsoleLevel, HandlerDataConsole } from '../types-hoist/instrument';
import { CONSOLE_LEVELS, originalConsoleMethods } from '../utils/debug-logger';
import { fill } from '../utils/object';
import { GLOBAL_OBJ } from '../utils/worldwide';
import { addHandler, maybeInstrument, triggerHandlers } from './handlers';
/**
* Add an instrumentation handler for when a console.xxx method is called.
*
* Use at your own risk, this might break without changelog notice, only used internally.
* @hidden
*/
export function addConsoleInstrumentationHandler(handler: (data: HandlerDataConsole) => void): void {
const type = 'console';
addHandler(type, handler);
maybeInstrument(type, instrumentConsole);
}
function instrumentConsole(): void {
if (!('console' in GLOBAL_OBJ)) {
return;
}
CONSOLE_LEVELS.forEach(function (level: ConsoleLevel): void {
if (!(level in GLOBAL_OBJ.console)) {
return;
}
fill(GLOBAL_OBJ.console, level, function (originalConsoleMethod: () => any): Function {
originalConsoleMethods[level] = originalConsoleMethod;
return function (...args: any[]): void {
triggerHandlers('console', { args, level } as HandlerDataConsole);
const log = originalConsoleMethods[level];
log?.apply(GLOBAL_OBJ.console, args);
};
});
});
}