import { LogLevel , Logger } from "src/shared/model/common" ; import { ONE_SECOND_MS } from "src/shared/constants" ; import { getPlainErrorProps } from "src/shared/util" ; import { isProtonApiError , resolveIpcMainApi , sanitizeProtonApiError } from "src/electron-preload/lib/util" ; let apiClient ; const resolveApiCallParameters = ( input ) : { level : LogLevel ; args : unknown [ ] ; } => { const level = input . level === "error" && input . args . some ( ( value ) => Object ( value ) . message === "Uncaught Error: An object could not be cloned." ) ? "warn" : input . level ; return { level , args : input . args } ; } ; const log = ( level , ... args ) : void => { const apiCall = ( apiClient ??= resolveIpcMainApi ( { timeoutMs : ONE_SECOND_MS * 3 } ) ) ( "log" ) ; apiCall ( resolveApiCallParameters ( { level , args } ) ) . catch ( async ( ) => { return apiCall ( resolveApiCallParameters ( { level , args : args . map ( ( arg ) => getPlainErrorProps ( arg ) ) } ) ) . catch ( ( ) => { } ) ; } ) ; } ; const sanitizeLoggedArgument = ( arg ) : unknown => { if ( isProtonApiError ( arg ) ) { return sanitizeProtonApiError ( arg ) ; } return arg ; } ; export const LOGGER = ( [ "error" , "warn" , "info" , "verbose" , "debug" , "silly" ] as const ) . reduce ( ( accumulator , level ) => { const doLog = log . bind ( null , level ) ; accumulator [ level ] = ( ... args ) => { doLog ( ... args . map ( sanitizeLoggedArgument ) ) ; } ; return accumulator ; } , { } as Logger ) ;