From 818812aa2d4d8ec8c41bbb7140363782a47afd1c Mon Sep 17 00:00:00 2001 From: Andras Schmelczer Date: Fri, 20 Dec 2024 17:35:30 +0000 Subject: [PATCH] Fix up logs view --- plugin/src/tracing/logger.ts | 11 +++++--- plugin/src/views/logs-view.ts | 49 +++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 plugin/src/views/logs-view.ts diff --git a/plugin/src/tracing/logger.ts b/plugin/src/tracing/logger.ts index fbaf0a14..2db69e15 100644 --- a/plugin/src/tracing/logger.ts +++ b/plugin/src/tracing/logger.ts @@ -8,22 +8,25 @@ export enum LogLevel { } class LogLine { + public timestamp = new Date(); public constructor(public level: LogLevel, public message: string) {} public toString(): string { - return `${this.formatLevel()}: ${this.message}`; + return `| ${this.formatLevel()} | ${this.timestamp.getHours()}:${this.timestamp.getMinutes()}:${this.timestamp.getSeconds()} | ${ + this.message + }`; } private formatLevel(): string { switch (this.level) { case LogLevel.DEBUG: - return "DEBUG"; + return " DEBUG"; case LogLevel.INFO: - return "INFO"; + return " INFO"; case LogLevel.WARNING: return "WARNING"; case LogLevel.ERROR: - return "ERROR"; + return " ERROR"; default: return "UNKNOWN"; } diff --git a/plugin/src/views/logs-view.ts b/plugin/src/views/logs-view.ts new file mode 100644 index 00000000..bbb45a9e --- /dev/null +++ b/plugin/src/views/logs-view.ts @@ -0,0 +1,49 @@ +import type { WorkspaceLeaf } from "obsidian"; +import { ItemView } from "obsidian"; +import { LogLevel, Logger } from "src/tracing/logger"; + +export class LogsView extends ItemView { + public static readonly TYPE = "logs-view"; + public static readonly ICON = "logs"; + + private timer: NodeJS.Timer | null = null; + + public constructor(leaf: WorkspaceLeaf) { + super(leaf); + this.icon = LogsView.ICON; + } + + public getViewType(): string { + return LogsView.TYPE; + } + + public getDisplayText(): string { + return "VaultLink logs"; + } + + public async onOpen(): Promise { + // eslint-disable-next-line @typescript-eslint/no-misused-promises + this.timer = setInterval(async () => this.updateView(), 250); + } + + public async onClose(): Promise { + if (this.timer) { + clearInterval(this.timer); + this.timer = null; + } + } + + private async updateView(): Promise { + const container = this.containerEl.children[1]; + container.empty(); + + container.createEl("h4", { text: "VaultLink logs" }); + + const messages = Logger.getInstance() + .getMessages(LogLevel.DEBUG) + .map((message) => message.toString()) + .join("\n"); + + container.createEl("pre", { text: messages }); + } +}