From 36b654a7e10a79ea243a90a9d69b0f6dbd81758a Mon Sep 17 00:00:00 2001 From: bropat Date: Sun, 3 Mar 2024 11:25:47 +0100 Subject: [PATCH] Added some new methods for the new logging implementation Fixed minor issues on new logging implementation --- src/eufysecurity.ts | 22 +++++++++++++++++++--- src/logging.ts | 16 +++++++++++++--- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/eufysecurity.ts b/src/eufysecurity.ts index 06253a16..8b459ca5 100644 --- a/src/eufysecurity.ts +++ b/src/eufysecurity.ts @@ -25,7 +25,7 @@ import { MQTTService } from "./mqtt/service"; import { TalkbackStream } from "./p2p/talkback"; import { PhoneModels } from "./http/const"; import { hexStringScheduleToSchedule, randomNumber } from "./http/utils"; -import { Logger, dummyLogger, InternalLogger, rootMainLogger, setLoggingLevel, LoggingCategories } from "./logging" +import { Logger, dummyLogger, InternalLogger, rootMainLogger, setLoggingLevel, LoggingCategories, getLoggingLevel } from "./logging" import { LogLevel } from "typescript-logging"; import { isCharging } from "./p2p/utils"; @@ -284,15 +284,31 @@ export class EufySecurity extends TypedEmitter { }); } - public updateLogging(category: LoggingCategories, level: LogLevel): void { + public setLoggingLevel(category: LoggingCategories, level: LogLevel): void { if (typeof level === "number" && Object.values(LogLevel).includes(level) && typeof category === "string" && - ["main", "http", "p2p" , "push", "mqtt"].includes(category.toLowerCase())) { + ["all", "main", "http", "p2p" , "push", "mqtt"].includes(category.toLowerCase())) { setLoggingLevel(category, level); } } + public getLoggingLevel(category: LoggingCategories): number { + if (typeof category === "string" && + ["all", "main", "http", "p2p" , "push", "mqtt"].includes(category.toLowerCase())) { + return getLoggingLevel(category); + } + return -1; + } + + public setInternalLogger(logger: Logger): void { + InternalLogger.logger = logger; + } + + public getInternalLogger(): Logger|undefined { + return InternalLogger.logger; + } + public getPushService(): PushNotificationService { return this.pushService; } diff --git a/src/logging.ts b/src/logging.ts index 2ce74c1e..bfa50388 100644 --- a/src/logging.ts +++ b/src/logging.ts @@ -1,8 +1,9 @@ -import { LogLevel as Level } from "typescript-logging"; +import { LogLevel } from "typescript-logging"; import { CategoryProvider } from "typescript-logging-category-style"; export type LoggingCategories = "all" | "main" | "http" | "p2p" | "push" | "mqtt"; -export const LogLevel = Level; + +export { LogLevel }; export interface Logger { trace(message: unknown, ...args: unknown[]): void; @@ -85,7 +86,7 @@ export const rootMQTTLogger = provider.getCategory("mqtt"); export const rootPushLogger = provider.getCategory("push"); export const rootP2PLogger = provider.getCategory("p2p"); -export const setLoggingLevel = function(category: LoggingCategories = "all", level: Level = LogLevel.Off): void { +export const setLoggingLevel = function(category: LoggingCategories = "all", level: LogLevel = LogLevel.Off): void { switch(category) { case "all": provider.updateRuntimeSettings({ @@ -118,4 +119,13 @@ export const setLoggingLevel = function(category: LoggingCategories = "all", lev }); break; } +} + +export const getLoggingLevel = function(category: LoggingCategories = "all"): number { + switch(category) { + case "all": + return provider.runtimeConfig.level; + default: + return provider.getCategory(category).logLevel; + } } \ No newline at end of file