diff --git a/src/logging/BaseLogger.ts b/src/logging/BaseLogger.ts index c9ce6aaf..999db4cf 100644 --- a/src/logging/BaseLogger.ts +++ b/src/logging/BaseLogger.ts @@ -20,8 +20,6 @@ import {LogLevel, LogFilter} from "./LogFilter"; import type {FilterCreator, LabelOrValues, LogCallback, ILogItem} from "./LogItem"; import type {Platform} from "../platform/web/Platform.js"; -type RunResult = T | void | Promise; - export abstract class BaseLogger { protected _openItems: Set = new Set(); protected _platform: Platform; @@ -37,7 +35,7 @@ export abstract class BaseLogger { } /** if item is a log item, wrap the callback in a child of it, otherwise start a new root log item. */ - wrapOrRun(item: ILogItem, labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): RunResult { + wrapOrRun(item: ILogItem, labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): T { if (item) { return item.wrap(labelOrValues, callback, logLevel, filterCreator); } else { @@ -62,7 +60,7 @@ export abstract class BaseLogger { /** run a callback wrapped in a log operation. Errors and duration are transparently logged, also for async operations. Whatever the callback returns is returned here. */ - run(labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): RunResult { + run(labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): T { if (!logLevel) { logLevel = LogLevel.Info; } @@ -70,7 +68,9 @@ export abstract class BaseLogger { return this._run(item, callback, logLevel!, true, filterCreator); } - _run(item: ILogItem, callback: LogCallback, logLevel: LogLevel, shouldThrow: boolean, filterCreator?: FilterCreator): RunResult { + _run(item: ILogItem, callback: LogCallback, logLevel: LogLevel, wantResult: true, filterCreator?: FilterCreator): T; + _run(item: ILogItem, callback: LogCallback, logLevel: LogLevel, wantResult: false, filterCreator?: FilterCreator): void; + _run(item: ILogItem, callback: LogCallback, logLevel: LogLevel, wantResult: boolean, filterCreator?: FilterCreator): T | void { this._openItems.add(item); const finishItem = () => { @@ -94,24 +94,28 @@ export abstract class BaseLogger { }; try { - const result = item.run(callback); + let result = item.run(callback); if (result instanceof Promise) { - return result.then(promiseResult => { + result = result.then(promiseResult => { finishItem(); return promiseResult; }, err => { finishItem(); - if (shouldThrow) { + if (wantResult) { throw err; } - }); - } else { + }) as unknown as T; + if (wantResult) { + return result; + } + } + if(wantResult) { finishItem(); return result; } } catch (err) { finishItem(); - if (shouldThrow) { + if (wantResult) { throw err; } } diff --git a/src/logging/LogItem.ts b/src/logging/LogItem.ts index 30a6294a..c3d32ef5 100644 --- a/src/logging/LogItem.ts +++ b/src/logging/LogItem.ts @@ -42,10 +42,10 @@ export interface ILogItem { children?: Array; values: LogItemValues; error?: Error; - wrap(labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): T | Promise; + wrap(labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): T; log(labelOrValues: LabelOrValues, logLevel?: LogLevel): void; set(key: string | object, value: unknown): void; - run(callback: LogCallback): T | Promise; + run(callback: LogCallback): T; runDetached(labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): ILogItem; wrapDetached(labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): void; refDetached(logItem: ILogItem, logLevel?: LogLevel): void; @@ -115,7 +115,7 @@ export class LogItem implements ILogItem { /** * Creates a new child item and runs it in `callback`. */ - wrap(labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): T | Promise { + wrap(labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): T { const item = this.child(labelOrValues, logLevel, filterCreator); return item.run(callback); } @@ -235,7 +235,7 @@ export class LogItem implements ILogItem { * @param {Function} callback [description] * @return {[type]} [description] */ - run(callback: LogCallback): T | Promise { + run(callback: LogCallback): T { if (this.end) { console.trace("log item is finished, additional logs will likely not be recorded"); } @@ -248,7 +248,7 @@ export class LogItem implements ILogItem { return promiseResult; }, err => { throw this.catch(err); - }); + }) as unknown as T; } else { this.finish(); return result; diff --git a/src/logging/NullLogger.ts b/src/logging/NullLogger.ts index d1ac2a03..eba453e6 100644 --- a/src/logging/NullLogger.ts +++ b/src/logging/NullLogger.ts @@ -59,7 +59,7 @@ export class NullLogItem implements ILogItem { this.logger = logger; } - wrap(_: LabelOrValues, callback: LogCallback): T | Promise { + wrap(_: LabelOrValues, callback: LogCallback): T { return callback(this); } @@ -75,7 +75,7 @@ export class NullLogItem implements ILogItem { return this.refDetached(); } - run(callback: LogCallback): T | Promise { + run(callback: LogCallback): T { return callback(this); }