Move files

This commit is contained in:
Andras Schmelczer 2025-02-19 20:47:52 +00:00
parent 6bb051460e
commit dd6f63f357
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
50 changed files with 72 additions and 78 deletions

View file

@ -0,0 +1,98 @@
export enum LogLevel {
DEBUG = "DEBUG",
INFO = "INFO",
WARNING = "WARNING",
ERROR = "ERROR"
}
const LOG_LEVEL_ORDER = {
[LogLevel.DEBUG]: 0,
[LogLevel.INFO]: 1,
[LogLevel.WARNING]: 2,
[LogLevel.ERROR]: 3
};
class LogLine {
public timestamp = new Date();
public constructor(
public level: LogLevel,
public message: string
) {}
}
export class Logger {
private static readonly MAX_MESSAGES = 1000;
private static instance: Logger | null = null;
private readonly messages: LogLine[] = [];
private readonly onMessageListeners: ((
status: LogLine | undefined
) => void)[] = [];
private constructor() {} // eslint-disable-line @typescript-eslint/no-empty-function
public static getInstance(): Logger {
if (!Logger.instance) {
Logger.instance = new Logger();
}
return Logger.instance;
}
public debug(message: string): void {
console.debug(message);
this.pushMessage(message, LogLevel.DEBUG);
}
public info(message: string): void {
console.info(message);
this.pushMessage(message, LogLevel.INFO);
}
public warn(message: string): void {
console.warn(message);
this.pushMessage(message, LogLevel.WARNING);
}
public error(message: string): void {
console.error(message);
this.pushMessage(message, LogLevel.ERROR);
}
public getMessages(mininumSeverity: LogLevel): LogLine[] {
return this.messages.filter(
(message) =>
LOG_LEVEL_ORDER[message.level] >=
LOG_LEVEL_ORDER[mininumSeverity]
);
}
public addOnMessageListener(
listener: (message: LogLine | undefined) => void
): void {
this.onMessageListeners.push(listener);
}
public reset(): void {
this.messages.length = 0;
this.onMessageListeners.forEach((listener) => {
listener(undefined);
});
}
private pushMessage(message: string, level: LogLevel): void {
const logLine = new LogLine(level, message);
this.messages.push(logLine);
while (this.messages.length > Logger.MAX_MESSAGES) {
this.messages.shift();
}
this.onMessageListeners.forEach((listener) => {
listener(logLine);
});
}
}

View file

@ -0,0 +1,103 @@
import type { RelativePath } from "src/database/document-metadata";
import { Logger } from "./logger";
export interface CommonHistoryEntry {
status: SyncStatus;
relativePath: RelativePath;
message: string;
type?: SyncType;
source?: SyncSource;
}
export enum SyncType {
CREATE = "CREATE",
UPDATE = "UPDATE",
DELETE = "DELETE"
}
export enum SyncSource {
PUSH = "PUSH",
PULL = "PULL"
}
export enum SyncStatus {
NO_OP = "NO_OP",
SUCCESS = "SUCCESS",
ERROR = "ERROR"
}
export type HistoryEntry = CommonHistoryEntry & { timestamp: Date };
export interface HistoryStats {
success: number;
error: number;
}
export class SyncHistory {
private static readonly MAX_ENTRIES = 5000;
private readonly entries: HistoryEntry[] = [];
private readonly syncHistoryUpdateListeners: ((
status: HistoryStats
) => void)[] = [];
private status: HistoryStats = {
success: 0,
error: 0
};
public getEntries(): HistoryEntry[] {
return [...this.entries];
}
public reset(): void {
this.entries.length = 0;
this.status = {
success: 0,
error: 0
};
this.syncHistoryUpdateListeners.forEach((listener) => {
listener(this.status);
});
}
public addSyncHistoryUpdateListener(
listener: (stats: HistoryStats) => void
): void {
this.syncHistoryUpdateListeners.push(listener);
listener({ ...this.status });
}
public addHistoryEntry(entry: CommonHistoryEntry): void {
const historyEntry = {
...entry,
timestamp: new Date()
};
this.entries.push(historyEntry);
if (entry.status === SyncStatus.SUCCESS) {
this.status.success++;
Logger.getInstance().info(
`History entry: ${entry.relativePath} - ${entry.message}`
);
} else if (entry.status === SyncStatus.ERROR) {
this.status.error++;
Logger.getInstance().error(
`Error syncing file: ${entry.relativePath} - ${entry.message}`
);
} else {
Logger.getInstance().debug(
`No-op syncing file: ${entry.relativePath} - ${entry.message}`
);
}
this.syncHistoryUpdateListeners.forEach((listener) => {
listener(this.status);
});
if (this.entries.length > SyncHistory.MAX_ENTRIES) {
this.entries.shift();
}
}
}