# HG changeset patch # User Ludovic Chabant # Date 1696266174 25200 # Node ID 1fe2cd2c603f6faf6d563ee1fe0ca0243eaa3156 # Parent 6e0595be7c0ef9db21ffea144b5b367d1bfa1d56 Add optional file logging. This is only for debugging purposes, especially for troubleshooting issues around app shutdown or app reloads. diff -r 6e0595be7c0e -r 1fe2cd2c603f src/main.ts --- 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); + } }