Skip to content

Commit 991f49b

Browse files
committed
named func
1 parent a1244bc commit 991f49b

File tree

1 file changed

+88
-64
lines changed

1 file changed

+88
-64
lines changed

src/addons/consoleCatcher.ts

Lines changed: 88 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,24 @@
44
import safeStringify from 'safe-stringify';
55
import type { ConsoleLogEvent } from '@hawk.so/types';
66

7-
const createConsoleCatcher = (): {
7+
/**
8+
* Creates a console interceptor that captures and formats console output
9+
*/
10+
function createConsoleCatcher(): {
811
initConsoleCatcher: () => void;
912
addErrorEvent: (event: ErrorEvent | PromiseRejectionEvent) => void;
1013
getConsoleLogStack: () => ConsoleLogEvent[];
11-
} => {
14+
} {
1215
const MAX_LOGS = 20;
1316
const consoleOutput: ConsoleLogEvent[] = [];
1417
let isInitialized = false;
1518

1619
/**
1720
* Converts any argument to its string representation
1821
*
19-
* @param arg - Console arguments
22+
* @param arg - Value to convert to string
2023
*/
21-
const stringifyArg = (arg: unknown): string => {
24+
function stringifyArg(arg: unknown): string {
2225
if (typeof arg === 'string') {
2326
return arg;
2427
}
@@ -27,19 +30,22 @@ const createConsoleCatcher = (): {
2730
}
2831

2932
return safeStringify(arg);
30-
};
33+
}
3134

3235
/**
3336
* Formats console arguments handling %c directives
3437
*
35-
* @param args - Console arguments that may include %c style directives
38+
* @param args - Console arguments that may include style directives
3639
*/
37-
const formatConsoleArgs = (
38-
args: unknown[]
39-
): { message: string; styles: string[] } => {
40+
function formatConsoleArgs(args: unknown[]): {
41+
message: string;
42+
styles: string[];
43+
} {
4044
if (args.length === 0) {
41-
return { message: '',
42-
styles: [] };
45+
return {
46+
message: '',
47+
styles: [],
48+
};
4349
}
4450

4551
const firstArg = args[0];
@@ -77,18 +83,28 @@ const createConsoleCatcher = (): {
7783
message: message + (remainingArgs ? ' ' + remainingArgs : ''),
7884
styles,
7985
};
80-
};
86+
}
8187

82-
const addToConsoleOutput = (logEvent: ConsoleLogEvent): void => {
88+
/**
89+
* Adds a console log event to the output buffer
90+
*
91+
* @param logEvent - The console log event to be added to the output buffer
92+
*/
93+
function addToConsoleOutput(logEvent: ConsoleLogEvent): void {
8394
if (consoleOutput.length >= MAX_LOGS) {
8495
consoleOutput.shift();
8596
}
8697
consoleOutput.push(logEvent);
87-
};
98+
}
8899

89-
const createConsoleEventFromError = (
100+
/**
101+
* Creates a console log event from an error or promise rejection
102+
*
103+
* @param event - The error event or promise rejection event to convert
104+
*/
105+
function createConsoleEventFromError(
90106
event: ErrorEvent | PromiseRejectionEvent
91-
): ConsoleLogEvent => {
107+
): ConsoleLogEvent {
92108
if (event instanceof ErrorEvent) {
93109
return {
94110
method: 'error',
@@ -110,63 +126,71 @@ const createConsoleCatcher = (): {
110126
stack: event.reason?.stack || '',
111127
fileLine: '',
112128
};
113-
};
129+
}
114130

115-
return {
116-
initConsoleCatcher(): void {
117-
if (isInitialized) {
131+
/**
132+
* Initializes the console interceptor by overriding default console methods
133+
*/
134+
function initConsoleCatcher(): void {
135+
if (isInitialized) {
136+
return;
137+
}
138+
139+
isInitialized = true;
140+
const consoleMethods: string[] = ['log', 'warn', 'error', 'info', 'debug'];
141+
142+
consoleMethods.forEach(function overrideConsoleMethod(method) {
143+
if (typeof window.console[method] !== 'function') {
118144
return;
119145
}
120146

121-
isInitialized = true;
122-
const consoleMethods: string[] = [
123-
'log',
124-
'warn',
125-
'error',
126-
'info',
127-
'debug',
128-
];
129-
130-
consoleMethods.forEach((method) => {
131-
if (typeof window.console[method] !== 'function') {
132-
return;
133-
}
134-
135-
const oldFunction = window.console[method].bind(window.console);
136-
137-
window.console[method] = function (...args: unknown[]): void {
138-
const stack =
139-
new Error().stack?.split('\n').slice(2)
140-
.join('\n') || '';
141-
const { message, styles } = formatConsoleArgs(args);
142-
143-
const logEvent: ConsoleLogEvent = {
144-
method,
145-
timestamp: new Date(),
146-
type: method,
147-
message,
148-
stack,
149-
fileLine: stack.split('\n')[0]?.trim(),
150-
styles,
151-
};
152-
153-
addToConsoleOutput(logEvent);
154-
oldFunction(...args);
147+
const oldFunction = window.console[method].bind(window.console);
148+
149+
window.console[method] = function (...args: unknown[]): void {
150+
const stack = new Error().stack?.split('\n').slice(2)
151+
.join('\n') || '';
152+
const { message, styles } = formatConsoleArgs(args);
153+
154+
const logEvent: ConsoleLogEvent = {
155+
method,
156+
timestamp: new Date(),
157+
type: method,
158+
message,
159+
stack,
160+
fileLine: stack.split('\n')[0]?.trim(),
161+
styles,
155162
};
156-
});
157-
},
158163

159-
addErrorEvent(event: ErrorEvent | PromiseRejectionEvent): void {
160-
const logEvent = createConsoleEventFromError(event);
164+
addToConsoleOutput(logEvent);
165+
oldFunction(...args);
166+
};
167+
});
168+
}
161169

162-
addToConsoleOutput(logEvent);
163-
},
170+
/**
171+
* Handles error events by converting them to console log events
172+
*
173+
* @param event - The error or promise rejection event to handle
174+
*/
175+
function addErrorEvent(event: ErrorEvent | PromiseRejectionEvent): void {
176+
const logEvent = createConsoleEventFromError(event);
164177

165-
getConsoleLogStack(): ConsoleLogEvent[] {
166-
return [ ...consoleOutput ];
167-
},
178+
addToConsoleOutput(logEvent);
179+
}
180+
181+
/**
182+
* Returns the current console output buffer
183+
*/
184+
function getConsoleLogStack(): ConsoleLogEvent[] {
185+
return [ ...consoleOutput ];
186+
}
187+
188+
return {
189+
initConsoleCatcher,
190+
addErrorEvent,
191+
getConsoleLogStack,
168192
};
169-
};
193+
}
170194

171195
const consoleCatcher = createConsoleCatcher();
172196

0 commit comments

Comments
 (0)