changeset 50:1fe2cd2c603f

Add optional file logging. This is only for debugging purposes, especially for troubleshooting issues around app shutdown or app reloads.
author Ludovic Chabant <ludovic@chabant.com>
date Mon, 02 Oct 2023 10:02:54 -0700
parents 6e0595be7c0e
children e932f1b73133
files src/main.ts
diffstat 1 files changed, 37 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/main.ts	Mon Oct 02 09:51:27 2023 -0700
+++ b/src/main.ts	Mon Oct 02 10:02:54 2023 -0700
@@ -1,3 +1,7 @@
+import * as fs from 'fs';
+import * as os from 'os';
+import * as path from 'path';
+
 import {
 	App,
 	Editor,
@@ -107,6 +111,13 @@
 	private _globalUninstallers: Function[] = [];
 
 	async onload() {
+		// Enable this for troubleshooting.
+		const enableLogfile: boolean = false;
+		if (enableLogfile) {
+			const outLogPath = path.join(os.tmpdir(), 'obsidian-remember-file-state.log');
+			this.setupLogFile(outLogPath);
+		}
+
 		console.log("RememberFileState: loading plugin");
 
 		await this.loadSettings();
@@ -472,5 +483,31 @@
 			console.debug(`RememberFileState: read ${numLoaded} record from state database.`);
 		}
 	}
+
+	private readonly setupLogFile = function(outLogPath: string) {
+		console.log("RememberFileState: setting up log file: ", outLogPath);
+
+		const makeWrapper = function(origFunc) {
+			return function (data) {
+				origFunc.apply(console, arguments);
+
+				var text: string = "";
+				for (var i: number = 0; i < arguments.length; i++) {
+					if (i > 0) text += " ";
+					text += arguments[i].toString();
+				}
+				text += "\n";
+				fs.appendFileSync(outLogPath, text);
+			};
+		};
+		console.log = makeWrapper(console.log);
+		console.debug = makeWrapper(console.debug);
+		console.info = makeWrapper(console.info);
+		console.warn = makeWrapper(console.warn);
+		console.error = makeWrapper(console.error);
+
+		const banner: string = "\n\nDebug log start\n===============\n";
+		fs.appendFileSync(outLogPath, banner);
+	}
 }