From 9a17a1d97fff6f3db5d0a92369a7cbc92c10a175 Mon Sep 17 00:00:00 2001 From: Michael Peters Date: Sun, 7 Nov 2021 16:39:59 -0600 Subject: [PATCH] added an easy comment for call-stacks in the _queryServer logs --- client/webapp/client-controller.ts | 6 +++++- logger/logger.ts | 14 ++++++++++---- stack-trace/stack-trace.ts | 7 ++----- tsconfig.json | 2 +- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/client/webapp/client-controller.ts b/client/webapp/client-controller.ts index af727e8..9f3421f 100644 --- a/client/webapp/client-controller.ts +++ b/client/webapp/client-controller.ts @@ -3,6 +3,8 @@ const electronConsole = electronRemote.getGlobal('console') as Console; import Logger from '../../logger/logger'; const LOG = Logger.create(__filename, electronConsole); +import * as StackTrace from '../../stack-trace/stack-trace'; + import * as crypto from 'crypto'; import { EventEmitter } from 'events'; @@ -359,7 +361,9 @@ export default class ClientController extends EventEmitter { async _queryServer(endpoint: string, ...args: any[]): Promise { // NOTE: socket.io may cause client-side memory leaks if the ack function is never called await this.ensureVerified(5000); - LOG.silly('querying s#' + this.id + ' @' + endpoint + ' / [' + args.map(arg => LOG.inspect(arg)).join(', ') + ']'); + let message = `querying s#${this.id} @${endpoint}(${args.map(arg => LOG.inspect(arg)).join(', ')})`; + LOG.silly(message); + //if (endpoint === 'fetch-messages-recent') LOG.silly(null, new Error('call stack')); return await new Promise((resolve, reject) => { this._socketEmitTimeout(5000, endpoint, ...args, async (errMsg: string, serverData: any) => { if (errMsg) { diff --git a/logger/logger.ts b/logger/logger.ts index 07b2ac9..0ce9806 100644 --- a/logger/logger.ts +++ b/logger/logger.ts @@ -11,6 +11,8 @@ import * as StackTrace from '../stack-trace/stack-trace'; const LINE_NUM_LENGTH = 3; const MAX_FILE_NAME_PADDING = 22; +const baseDir = path.join(__dirname, '..', '..'); + /** Adds padding to the left of a string */ function padLeft(string: string, padding: string, minLength: number) { while (string.length < minLength) { @@ -183,6 +185,13 @@ export default class Logger { }); } + public getTSStackFrames(err: Error): StackTrace.Frame[] { + let jsFrames: StackTrace.Frame[] = StackTrace.parse(err); + let tsFrames = jsFrames.map(jsFrame => tryCreateTSFrame(this.sourceMaps, jsFrame)); + tsFrames = tsFrames.map(tsFrame => StackTrace.relativizeFilePath(tsFrame, baseDir)); + return tsFrames; + } + /** Logs a message and potentially corresponding data */ private log(level: LoggerLevel, message: string | null, data?: Error | any): void { let frames: StackTrace.Frame[] = StackTrace.parse(new Error()); @@ -216,11 +225,8 @@ export default class Logger { } if (data instanceof Error) { if (data.stack) { - const baseDir = path.join(__dirname, '..', '..'); - let errorFrames = StackTrace.parse(data); + let errorFrames = this.getTSStackFrames(data); let tsStackLines = errorFrames - .map((frame: StackTrace.Frame) => tryCreateTSFrame(this.sourceMaps, frame)) - .map((frame: StackTrace.Frame) => StackTrace.relativizeFilePath(frame, baseDir)) .map((frame: StackTrace.Frame) => StackTrace.getFrameLine(frame)); out += `${prefix}# ${data.name}: ${data.message}`; out += `\n${prefix}# ${tsStackLines.join(`\n${prefix}# `)}`; diff --git a/stack-trace/stack-trace.ts b/stack-trace/stack-trace.ts index 215e6e0..9ac6696 100644 --- a/stack-trace/stack-trace.ts +++ b/stack-trace/stack-trace.ts @@ -15,7 +15,7 @@ export function parse(err: Error): Frame[] { const lines = err.stack.split('\n').slice(1); return lines - .map(function(line: string): Frame | null { + .map(function(line: string): Frame { if (line.match(/^\s*[-]{4,}$/)) { return { fileName: line }; } @@ -36,10 +36,7 @@ export function parse(err: Error): Frame[] { // chop of parenthesis chop off 'at' and file path return parseData(pieces[pieces.length - 1].slice(1, -1), pieces.slice(1, -1).join(' ')); } - }) - .filter(function(callSite): boolean { - return !!callSite; - }) as Frame[]; + }); } export function relativizeFilePath(frame: Frame, baseDir: string): Frame { diff --git a/tsconfig.json b/tsconfig.json index 04e6cd3..16a1689 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,6 @@ { "compilerOptions": { - "target": "ES6", + "target": "ES2017", "module": "CommonJS", "moduleResolution": "node", "sourceMap": true,