Skip to content

Commit

Permalink
fix: improve promise tracking in debug logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jharvey10 committed Dec 4, 2023
1 parent 2c8d326 commit cc14bc2
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/main/core/log/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ type Level = 'debug' | 'error'

const DRAIN_INTERVAL = 250 // ms

let curPromiseId = 1

/**
* Logger class to log debug and error statements to a given file.
*/
Expand Down Expand Up @@ -89,7 +91,7 @@ export class Logger {
public traceEnter(targetName: string, methodName: string, args: unknown[]) {
const stringArgs = String(args.map(safeStringify))

this.debug(`-> ${targetName}::${methodName}(${stringArgs})`)
this.debug(`--> ${targetName}::${methodName}(${stringArgs})`)
}

/**
Expand All @@ -101,9 +103,16 @@ export class Logger {
*/
public traceExit(targetName: string, methodName: string, result: unknown) {
if (result instanceof Promise) {
const promiseIndex = curPromiseId++
this.debug(`-?- ${targetName}::${methodName}(...): awaiting [Promise${promiseIndex}]`)

result.then(
(value: unknown) => {
this.debug(`<- ${targetName}::${methodName}(...): ${safeStringify(value)}`)
this.debug(
`<-- ${targetName}::${methodName}(...): resolved [Promise${promiseIndex}]: ${safeStringify(
value
)}`
)
},
(err: unknown) => {
this.error(`-x- ${targetName}::${methodName}(...): ${safeStringify(err)}`)
Expand All @@ -116,7 +125,7 @@ export class Logger {
this.error(`-x- ${targetName}::${methodName}(...): ${safeStringify(result)}`)
this.error(result)
} else {
this.debug(`<- ${targetName}::${methodName}(...): ${safeStringify(result)}`)
this.debug(`<-- ${targetName}::${methodName}(...): ${safeStringify(result)}`)
}
}

Expand Down

0 comments on commit cc14bc2

Please sign in to comment.