comparison src/main.ts @ 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 fae202d7b3de
children e932f1b73133
comparison
equal deleted inserted replaced
49:6e0595be7c0e 50:1fe2cd2c603f
1 import * as fs from 'fs';
2 import * as os from 'os';
3 import * as path from 'path';
4
1 import { 5 import {
2 App, 6 App,
3 Editor, 7 Editor,
4 MarkdownView, 8 MarkdownView,
5 Modal, 9 Modal,
105 private _viewUninstallers: Record<string, Function> = {}; 109 private _viewUninstallers: Record<string, Function> = {};
106 // Functions to unregister any global callbacks on plugin unload. 110 // Functions to unregister any global callbacks on plugin unload.
107 private _globalUninstallers: Function[] = []; 111 private _globalUninstallers: Function[] = [];
108 112
109 async onload() { 113 async onload() {
114 // Enable this for troubleshooting.
115 const enableLogfile: boolean = false;
116 if (enableLogfile) {
117 const outLogPath = path.join(os.tmpdir(), 'obsidian-remember-file-state.log');
118 this.setupLogFile(outLogPath);
119 }
120
110 console.log("RememberFileState: loading plugin"); 121 console.log("RememberFileState: loading plugin");
111 122
112 await this.loadSettings(); 123 await this.loadSettings();
113 124
114 this.data = Object.assign({}, DEFAULT_DATA); 125 this.data = Object.assign({}, DEFAULT_DATA);
470 this.data = JSON.parse(jsonDb); 481 this.data = JSON.parse(jsonDb);
471 const numLoaded = Object.keys(this.data.rememberedFiles).length; 482 const numLoaded = Object.keys(this.data.rememberedFiles).length;
472 console.debug(`RememberFileState: read ${numLoaded} record from state database.`); 483 console.debug(`RememberFileState: read ${numLoaded} record from state database.`);
473 } 484 }
474 } 485 }
486
487 private readonly setupLogFile = function(outLogPath: string) {
488 console.log("RememberFileState: setting up log file: ", outLogPath);
489
490 const makeWrapper = function(origFunc) {
491 return function (data) {
492 origFunc.apply(console, arguments);
493
494 var text: string = "";
495 for (var i: number = 0; i < arguments.length; i++) {
496 if (i > 0) text += " ";
497 text += arguments[i].toString();
498 }
499 text += "\n";
500 fs.appendFileSync(outLogPath, text);
501 };
502 };
503 console.log = makeWrapper(console.log);
504 console.debug = makeWrapper(console.debug);
505 console.info = makeWrapper(console.info);
506 console.warn = makeWrapper(console.warn);
507 console.error = makeWrapper(console.error);
508
509 const banner: string = "\n\nDebug log start\n===============\n";
510 fs.appendFileSync(outLogPath, banner);
511 }
475 } 512 }
476 513