Skip to content

Commit

Permalink
Merged update from dev-1039-log-debug-to-file
Browse files Browse the repository at this point in the history
  • Loading branch information
ronzulu committed Aug 6, 2024
1 parent 36ca05d commit 85de8b6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
15 changes: 14 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import { TextDirection } from "./util/TextDirection";
import { convertToStringOrEmpty } from "./util/utils";
import { logger } from "./util/logger";

export const versionString: string = "Branch: master v1.13-beta.8";
export const versionString: string = "Branch: master v1.13-beta.9";

export default class SRPlugin extends Plugin {
private statusBar: HTMLElement;
Expand All @@ -61,6 +61,10 @@ export default class SRPlugin extends Plugin {
await this.initLogicClasses();

this.initGuiItems();

window.onunhandledrejection = (event) => {
logger.log(`Unhandled promise rejection: ${event.reason}`);
};
}

private async initLogicClasses(): Promise<void> {
Expand Down Expand Up @@ -353,6 +357,15 @@ export default class SRPlugin extends Plugin {
}

async sync(): Promise<void> {
try {
await this.doSync();
}
catch (e) {
logger.error("sync()", e);
}
}

async doSync(): Promise<void> {
if (this.osrAppCore.syncLock) {
return;
}
Expand Down
18 changes: 15 additions & 3 deletions src/util/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,22 @@ export class logger {
static setVault(vault: Vault): void {
logger._vault = vault;
}

static async setDestination(settings: SRSettings): Promise<void> {
logger._dest = logger.convertStrToLoggerDestination(settings.debugLoggerDestination);
const dateStr: string = globalDateProvider.now.format("YYYYMMDD");
logger._filename = settings.debugLoggerFilename.replace("{DATE}", dateStr);
await logger.log(`Obsidian: SpacedRepetition: ${versionString}`);
await logger.log(`\r\n---\r\n## Obsidian: SpacedRepetition: ${versionString}\r\n`);
}

static error(str: string, e: Error) {
if (e && e.stack == null) {
//
if (e.message) str += `: ${e.message}`;
if (e.name) str += `: ${e.name}`;
}
logger.log(`ERROR: ${str}`);
if (e?.stack) logger.log(`STACK: ${e.stack}`);
}

static async log(str: string): Promise<void> {
Expand All @@ -48,7 +58,9 @@ export class logger {
await this._vault.adapter.append(filename, output);
} else {
const dir: string = path.dirname(filename);
await logger._vault.createFolder(dir);
if (!await logger._vault.adapter.exists(dir)) {
await logger._vault.createFolder(dir);
}
await logger._vault.create(filename, output);
}
} catch (e) {
Expand Down

0 comments on commit 85de8b6

Please sign in to comment.