文件
chen 301194963b
一些检查失败了
automerge / automerge (push) Has been cancelled
react-native-android-build-apk / AssembleRelease (push) Has been cancelled
Build macOS M1 / Main (push) Has been cancelled
Joplin Continuous Integration / Main (macos-15-intel) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-11-vs2026-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-2025) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04-arm) (push) Has been cancelled
Joplin UI tests / Main (ubuntu-22.04) (push) Has been cancelled
Joplin UI tests / Main (windows-2025) (push) Has been cancelled
Joplin UI tests / Mobile (push) Has been cancelled
Close stale issues / ProcessStaleIssues (push) Has been cancelled
feat: Initial clean release of CHM
2026-11-04 11:29:39 +08:00

55 行
1.9 KiB
TypeScript

import { LogLevel } from '../../types';
/* eslint-disable no-console */
type OnLogCallback = (level: LogLevel, message: string)=> void;
const wrapConsoleLog = (onLog: OnLogCallback) => {
// inOnLogCallback keeps track of whether onLog is currently being called or not.
// This helps prevent StackOverflows if onLog itself calls console.log/console.error.
let inOnLogCallback = false;
const wrapLogFunction = (key: keyof typeof console, logLevel: LogLevel) => {
const originalLog = (console[key] as ((...args: unknown[])=> void) | undefined) ?? (()=>{});
console[key] = function(...args: unknown[]) {
originalLog.call(this, ...args);
if (inOnLogCallback) return;
// Work around
// Uncaught (in promise) TypeError: Cannot convert object to primitive value
// when console.log is called with an object that
// can't be converted to a string.
let argsString;
try {
argsString = args.join(' ');
} catch (error) {
argsString = `Error converting console arguments to string: ${error}`;
}
// Long messages can be slow to transfer over IPC and make other messages difficult
// to find/read in React Native's output.
const maxLength = 4096;
if (argsString.length > maxLength) {
argsString = `${argsString.substring(0, maxLength)}...`;
}
try {
inOnLogCallback = true;
onLog(logLevel, argsString);
} finally {
inOnLogCallback = false;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Assigning a generic (...args: unknown[]) function to a specific console method (which is the union of all method types) requires a cast through `any`
} as any;
};
wrapLogFunction('timeLog', LogLevel.Debug);
wrapLogFunction('log', LogLevel.Debug);
wrapLogFunction('warn', LogLevel.Warn);
wrapLogFunction('info', LogLevel.Info);
wrapLogFunction('error', LogLevel.Error);
};
export default wrapConsoleLog;