Stop Logger being a singleton

This commit is contained in:
Andras Schmelczer 2025-02-22 15:11:59 +00:00
parent d965265709
commit 3471a9c498
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
12 changed files with 147 additions and 138 deletions

View file

@ -1,47 +1,45 @@
import type { Syncer } from "sync-client";
import type { SyncClient, Syncer } from "sync-client";
import { Logger } from "sync-client";
import type { TAbstractFile } from "obsidian";
import { TFile } from "obsidian";
export class ObsidianFileEventHandler {
public constructor(private readonly syncer: Syncer) {}
public constructor(private readonly client: SyncClient) {}
public async onCreate(file: TAbstractFile): Promise<void> {
if (file instanceof TFile) {
Logger.getInstance().info(`File created: ${file.path}`);
this.client.logger.info(`File created: ${file.path}`);
await this.syncer.syncLocallyCreatedFile(
await this.client.syncer.syncLocallyCreatedFile(
file.path,
new Date(file.stat.ctime)
);
} else {
Logger.getInstance().debug(`Folder created: ${file.path}, ignored`);
this.client.logger.debug(`Folder created: ${file.path}, ignored`);
}
}
public async onDelete(file: TAbstractFile): Promise<void> {
if (file instanceof TFile) {
Logger.getInstance().info(`File deleted: ${file.path}`);
this.client.logger.info(`File deleted: ${file.path}`);
await this.syncer.syncLocallyDeletedFile(file.path);
await this.client.syncer.syncLocallyDeletedFile(file.path);
} else {
Logger.getInstance().debug(`Folder deleted: ${file.path}, ignored`);
this.client.logger.debug(`Folder deleted: ${file.path}, ignored`);
}
}
public async onRename(file: TAbstractFile, oldPath: string): Promise<void> {
if (file instanceof TFile) {
Logger.getInstance().info(
`File renamed: ${oldPath} -> ${file.path}`
);
this.client.logger.info(`File renamed: ${oldPath} -> ${file.path}`);
await this.syncer.syncLocallyUpdatedFile({
await this.client.syncer.syncLocallyUpdatedFile({
oldPath,
relativePath: file.path,
updateTime: new Date(file.stat.ctime)
});
} else {
Logger.getInstance().debug(
this.client.logger.debug(
`Folder renamed: ${oldPath} -> ${file.path}, ignored`
);
}
@ -53,16 +51,14 @@ export class ObsidianFileEventHandler {
return;
}
Logger.getInstance().info(`File modified: ${file.path}`);
this.client.logger.info(`File modified: ${file.path}`);
await this.syncer.syncLocallyUpdatedFile({
await this.client.syncer.syncLocallyUpdatedFile({
relativePath: file.path,
updateTime: new Date(file.stat.ctime)
});
} else {
Logger.getInstance().debug(
`Folder modified: ${file.path}, ignored`
);
this.client.logger.debug(`Folder modified: ${file.path}, ignored`);
}
}
}

View file

@ -10,7 +10,7 @@ import { StatusBar } from "./views/status-bar";
import { LogsView } from "./views/logs-view";
import { StatusDescription } from "./views/status-description";
import { Logger, SyncClient } from "sync-client";
import { SyncClient } from "sync-client";
import { ObsidianFileSystemOperations } from "./obsidian-file-system";
export default class VaultLinkPlugin extends Plugin {
@ -18,8 +18,6 @@ export default class VaultLinkPlugin extends Plugin {
private client!: SyncClient;
public async onload(): Promise<void> {
Logger.getInstance().info("Starting plugin");
this.client = await SyncClient.create(
new ObsidianFileSystemOperations(this.app.vault),
{
@ -28,6 +26,8 @@ export default class VaultLinkPlugin extends Plugin {
}
);
this.client.logger.info("Starting plugin");
const statusDescription = new StatusDescription(this.client);
this.settingsTab = new SyncSettingsTab({
@ -42,12 +42,11 @@ export default class VaultLinkPlugin extends Plugin {
this.registerView(
HistoryView.TYPE,
(leaf) =>
new HistoryView(leaf, this.client.settings, this.client.history)
(leaf) => new HistoryView(leaf, this.client)
);
this.registerView(
LogsView.TYPE,
(leaf) => new LogsView(this, this.client.settings, leaf)
(leaf) => new LogsView(this, this.client, leaf)
);
this.addRibbonIcon(
@ -61,11 +60,10 @@ export default class VaultLinkPlugin extends Plugin {
async (_: MouseEvent) => this.activateView(LogsView.TYPE)
);
const eventHandler = new ObsidianFileEventHandler(this.client.syncer);
const eventHandler = new ObsidianFileEventHandler(this.client);
this.app.workspace.onLayoutReady(async () => {
Logger.getInstance().info("Initialising sync handlers");
this.client.logger.info("Initialising sync handlers");
[
this.app.vault.on(
"create",
@ -87,7 +85,7 @@ export default class VaultLinkPlugin extends Plugin {
this.registerEvent(event);
});
Logger.getInstance().info("Sync handlers initialised");
this.client.logger.info("Sync handlers initialised");
void this.client.syncer.scheduleSyncForOfflineChanges();
});

View file

@ -2,7 +2,7 @@ import type { IconName, WorkspaceLeaf } from "obsidian";
import { ItemView, setIcon } from "obsidian";
import { intlFormatDistance } from "date-fns";
import type { SyncHistory, HistoryEntry, Settings } from "sync-client";
import type { HistoryEntry, SyncClient } from "sync-client";
import { SyncType, SyncSource, SyncStatus, Logger } from "sync-client";
export class HistoryView extends ItemView {
@ -12,15 +12,14 @@ export class HistoryView extends ItemView {
public constructor(
leaf: WorkspaceLeaf,
private readonly settings: Settings,
private readonly history: SyncHistory
private readonly client: SyncClient
) {
super(leaf);
this.icon = HistoryView.ICON;
history.addSyncHistoryUpdateListener(() => {
this.client.history.addSyncHistoryUpdateListener(() => {
this.updateView().catch((_error: unknown) => {
Logger.getInstance().error("Failed to update history view");
this.client.logger.error("Failed to update history view");
});
});
}
@ -94,13 +93,13 @@ export class HistoryView extends ItemView {
container.empty();
container.createEl("h4", { text: "VaultLink History" });
const entries = this.history
const entries = this.client.history
.getEntries()
.reverse()
.filter(
(entry) =>
entry.status !== SyncStatus.NO_OP ||
this.settings.getSettings().displayNoopSyncEvents
this.client.settings.getSettings().displayNoopSyncEvents
);
entries.forEach((entry) => {

View file

@ -1,8 +1,7 @@
import type { WorkspaceLeaf } from "obsidian";
import { ItemView } from "obsidian";
import type VaultLinkPlugin from "src/vault-link-plugin";
import type { Settings } from "sync-client";
import { Logger } from "sync-client";
import type { SyncClient } from "sync-client";
export class LogsView extends ItemView {
public static readonly TYPE = "logs-view";
@ -10,20 +9,24 @@ export class LogsView extends ItemView {
public constructor(
private readonly plugin: VaultLinkPlugin,
private readonly settings: Settings,
private readonly client: SyncClient,
leaf: WorkspaceLeaf
) {
super(leaf);
this.icon = LogsView.ICON;
Logger.getInstance().addOnMessageListener(() => {
this.client.logger.addOnMessageListener(() => {
this.updateView();
});
settings.addOnSettingsChangeHandlers((newSettings, oldSettings) => {
if (newSettings.minimumLogLevel !== oldSettings.minimumLogLevel) {
this.updateView();
this.client.settings.addOnSettingsChangeHandlers(
(newSettings, oldSettings) => {
if (
newSettings.minimumLogLevel !== oldSettings.minimumLogLevel
) {
this.updateView();
}
}
});
);
}
private static formatTimestamp(timestamp: Date): string {
@ -78,8 +81,8 @@ export class LogsView extends ItemView {
}
);
const logs = Logger.getInstance().getMessages(
this.settings.getSettings().minimumLogLevel
const logs = this.client.logger.getMessages(
this.client.settings.getSettings().minimumLogLevel
);
if (logs.length === 0) {