Skip to content

Commit

Permalink
Added some new methods for the new logging implementation
Browse files Browse the repository at this point in the history
Fixed minor issues on new logging implementation
  • Loading branch information
bropat committed Mar 3, 2024
1 parent 47f24e7 commit 36b654a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
22 changes: 19 additions & 3 deletions src/eufysecurity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -284,15 +284,31 @@ export class EufySecurity extends TypedEmitter<EufySecurityEvents> {
});
}

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;
}
Expand Down
16 changes: 13 additions & 3 deletions src/logging.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 36b654a

Please sign in to comment.