From b525cac25039ab8729bb51af663c5e7b291e1e23 Mon Sep 17 00:00:00 2001 From: silentrald Date: Fri, 21 Feb 2025 23:27:13 +0800 Subject: [PATCH] [chore] removed log decorators --- src/main/decorators/log.decorator.ts | 67 ---------------------------- 1 file changed, 67 deletions(-) delete mode 100644 src/main/decorators/log.decorator.ts diff --git a/src/main/decorators/log.decorator.ts b/src/main/decorators/log.decorator.ts deleted file mode 100644 index dd67a4e5..00000000 --- a/src/main/decorators/log.decorator.ts +++ /dev/null @@ -1,67 +0,0 @@ -import log from "electron-log"; -import { isPromise } from "../../shared/helpers/promise.helpers"; -import { tryit } from "../../shared/helpers/error.helpers"; - -type LogOptions = { - logInput?: boolean; - logOutput?: boolean; - logArgs?: boolean; -}; - -const stringifyArgs = (args: unknown[]) => { - return args?.map(a => JSON.stringify(a)).join(', '); -}; - -const logInfo = (message: string, propertyKey: string, args: unknown[], logArgs: boolean, result: unknown) => { - log.info(`[${message}] ${propertyKey}(${logArgs ? stringifyArgs(args) : ''})`, result); -}; - -const logError = (propertyKey: string, args: unknown[], logArgs: boolean, error: unknown) => { - log.error(`[ERROR] ${propertyKey}(${logArgs ? stringifyArgs(args) : ''})`, error); - throw error; -}; - -export function Log(options?: LogOptions){ - - const logInput = options?.logInput ?? false; - const logOutput = options?.logOutput ?? true; - const logArgs = options?.logArgs ?? true; - - return (_target: unknown, propertyKey: string, descriptor: PropertyDescriptor) => { - const originalMethod = descriptor.value; - - descriptor.value = function func(...args: unknown[]) { - - if (logInput) { - logInfo('INPUT', propertyKey, args, logArgs, null); - } - - const outcome = tryit(() => originalMethod.apply(this, args)); - - if (isPromise(outcome)) { - return outcome.then(({ result, error }) => { - - if (error) { - logError(propertyKey, args, logArgs, error); - } - if (logOutput) { - logInfo('OUTPUT', propertyKey, args, logArgs, result); - } - return result; - }); - } - - const { result, error } = outcome; - - if (error) { - logError(propertyKey, args, logArgs, error); - } - - if (logOutput) { - logInfo('OUTPUT', propertyKey, args, logArgs, result); - } - - return result; - }; - }; -}