From 30a384fe1e8ddcd7fe627cac55a663e7c6d7e8ea Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Mon, 15 Nov 2021 18:44:25 +0530 Subject: [PATCH] Make LogFilter optional --- src/logging/BaseLogger.ts | 18 +++++++++--------- src/logging/LogItem.ts | 16 ++++++++-------- src/matrix/e2ee/megolm/Decryption.ts | 2 +- src/matrix/storage/idb/StorageFactory.ts | 8 ++------ src/matrix/storage/idb/Transaction.ts | 2 +- src/matrix/storage/idb/schema.ts | 2 +- 6 files changed, 22 insertions(+), 26 deletions(-) diff --git a/src/logging/BaseLogger.ts b/src/logging/BaseLogger.ts index e420c0df..28602b17 100644 --- a/src/logging/BaseLogger.ts +++ b/src/logging/BaseLogger.ts @@ -32,13 +32,13 @@ export abstract class BaseLogger { } log(labelOrValues: LabelOrValues, logLevel: LogLevel = LogLevel.Info) { - const item = new LogItem(labelOrValues, logLevel, null, this); + const item = new LogItem(labelOrValues, logLevel, this); item.end = item.start; this._persistItem(item, undefined, false); } /** 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: LogLevelOrNull = null, filterCreator: FilterCreator = null): unknown { + wrapOrRun(item: ILogItem, labelOrValues: LabelOrValues, callback: LogCallback, logLevel: LogLevelOrNull = null, filterCreator?: FilterCreator): unknown { if (item) { return item.wrap(labelOrValues, callback, logLevel, filterCreator); } else { @@ -51,28 +51,28 @@ export abstract class BaseLogger { Useful to pair with LogItem.refDetached. @return {LogItem} the log item added, useful to pass to LogItem.refDetached */ - runDetached(labelOrValues: LabelOrValues, callback: LogCallback, logLevel: LogLevelOrNull = null, filterCreator: FilterCreator = null): ILogItem { + runDetached(labelOrValues: LabelOrValues, callback: LogCallback, logLevel: LogLevelOrNull = null, filterCreator?: FilterCreator): ILogItem { // todo: Remove jsdoc type? if (logLevel === null) { logLevel = LogLevel.Info; } - const item = new LogItem(labelOrValues, logLevel, null, this); - this._run(item, callback, logLevel!, filterCreator, false /* don't throw, nobody is awaiting */); + const item = new LogItem(labelOrValues, logLevel, this); + this._run(item, callback, logLevel!, false /* don't throw, nobody is awaiting */, filterCreator); return item; } /** 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: LogLevelOrNull = null, filterCreator: FilterCreator = null): unknown { + run(labelOrValues: LabelOrValues, callback: LogCallback, logLevel: LogLevelOrNull = null, filterCreator?: FilterCreator): unknown { if (logLevel === null) { logLevel = LogLevel.Info; } - const item = new LogItem(labelOrValues, logLevel, null, this); - return this._run(item, callback, logLevel!, filterCreator, true); + const item = new LogItem(labelOrValues, logLevel, this); + return this._run(item, callback, logLevel!, true, filterCreator); } - _run(item: ILogItem, callback: LogCallback, logLevel: LogLevel, filterCreator: FilterCreator, shouldThrow: boolean): unknown { + _run(item: ILogItem, callback: LogCallback, logLevel: LogLevel, shouldThrow: boolean, filterCreator?: FilterCreator): unknown { this._openItems.add(item); const finishItem = () => { diff --git a/src/logging/LogItem.ts b/src/logging/LogItem.ts index 54957347..c40d7c08 100644 --- a/src/logging/LogItem.ts +++ b/src/logging/LogItem.ts @@ -43,7 +43,7 @@ export interface ILogItem { children: Array | null; values: LogItemValues; error: Error | null; - wrap(labelOrValues: LabelOrValues, callback: LogCallback, level: LogLevelOrNull, filterCreator: FilterCreator): unknown; + wrap(labelOrValues: LabelOrValues, callback: LogCallback, level: LogLevelOrNull, filterCreator?: FilterCreator): unknown; log(labelOrValues: LabelOrValues, logLevel: LogLevelOrNull): void; set(key: string | object, value: unknown): void; run(callback: LogCallback): unknown; @@ -68,7 +68,7 @@ export type LogItemValues = { } export type LabelOrValues = string | LogItemValues; -export type FilterCreator = ((filter: LogFilter, item: ILogItem) => LogFilter) | null; +export type FilterCreator = ((filter: LogFilter, item: ILogItem) => LogFilter); export type LogCallback = (item: ILogItem) => unknown; export class LogItem implements ILogItem { @@ -78,10 +78,10 @@ export class LogItem implements ILogItem { public end: number | null; private _values: LogItemValues; private _logger: BaseLogger; - private _filterCreator: FilterCreator; + private _filterCreator?: FilterCreator; private _children: Array | null; - constructor(labelOrValues: LabelOrValues, logLevel: LogLevel, filterCreator: FilterCreator, logger: BaseLogger) { + constructor(labelOrValues: LabelOrValues, logLevel: LogLevel, logger: BaseLogger, filterCreator?: FilterCreator) { this._logger = logger; this.start = logger._now(); this.end = null; @@ -119,7 +119,7 @@ export class LogItem implements ILogItem { /** * Creates a new child item and runs it in `callback`. */ - wrap(labelOrValues: LabelOrValues, callback: LogCallback, logLevel: LogLevelOrNull = null, filterCreator: FilterCreator = null): unknown { + wrap(labelOrValues: LabelOrValues, callback: LogCallback, logLevel: LogLevelOrNull = null, filterCreator?: FilterCreator): unknown { const item = this.child(labelOrValues, logLevel, filterCreator); return item.run(callback); } @@ -161,7 +161,7 @@ export class LogItem implements ILogItem { * Hence, the child item is not returned. */ log(labelOrValues: LabelOrValues, logLevel: LogLevelOrNull = null): void { - const item = this.child(labelOrValues, logLevel, null); + const item = this.child(labelOrValues, logLevel); item.end = item.start; } @@ -291,14 +291,14 @@ export class LogItem implements ILogItem { return err; } - child(labelOrValues: LabelOrValues, logLevel: LogLevelOrNull, filterCreator: FilterCreator): ILogItem { + child(labelOrValues: LabelOrValues, logLevel: LogLevelOrNull, filterCreator?: FilterCreator): ILogItem { if (this.end !== null) { console.trace("log item is finished, additional logs will likely not be recorded"); } if (!logLevel) { logLevel = this.logLevel || LogLevel.Info; } - const item = new LogItem(labelOrValues, logLevel, filterCreator, this._logger); + const item = new LogItem(labelOrValues, logLevel, this._logger, filterCreator); if (this._children === null) { this._children = []; } diff --git a/src/matrix/e2ee/megolm/Decryption.ts b/src/matrix/e2ee/megolm/Decryption.ts index 4cb66971..ee96eca1 100644 --- a/src/matrix/e2ee/megolm/Decryption.ts +++ b/src/matrix/e2ee/megolm/Decryption.ts @@ -152,7 +152,7 @@ export class Decryption { log.logLevel = log.level.Warn; log.set("invalid", true); } - }, log.level.Detail, null); + }, log.level.Detail); } return keys; } diff --git a/src/matrix/storage/idb/StorageFactory.ts b/src/matrix/storage/idb/StorageFactory.ts index 5ca6bce4..5c257bfb 100644 --- a/src/matrix/storage/idb/StorageFactory.ts +++ b/src/matrix/storage/idb/StorageFactory.ts @@ -101,12 +101,8 @@ async function createStores(db: IDBDatabase, txn: IDBTransaction, oldVersion: nu async (log) => { for (let i = startIdx; i < version; ++i) { const migrationFunc = schema[i]; - await log.wrap(`v${i + 1}`, (log) => - migrationFunc(db, txn, localStorage, log), null, null - ); + await log.wrap(`v${i + 1}`, (log) => migrationFunc(db, txn, localStorage, log), null); } }, - null, - null - ) as Promise; + null) as Promise; } diff --git a/src/matrix/storage/idb/Transaction.ts b/src/matrix/storage/idb/Transaction.ts index 721ee711..c68a1759 100644 --- a/src/matrix/storage/idb/Transaction.ts +++ b/src/matrix/storage/idb/Transaction.ts @@ -226,7 +226,7 @@ export class Transaction { }; const label = `${this._writeErrors.length} storage write operation(s) failed`; if (parentItem) { - parentItem.wrap(label, callback, null, null); + parentItem.wrap(label, callback, null); } else { this.logger.run(label, callback); } diff --git a/src/matrix/storage/idb/schema.ts b/src/matrix/storage/idb/schema.ts index 62a4cea1..b21bd30f 100644 --- a/src/matrix/storage/idb/schema.ts +++ b/src/matrix/storage/idb/schema.ts @@ -207,7 +207,7 @@ async function fixMissingRoomsInUserIdentities(db: IDBDatabase, txn: IDBTransact // so we'll create a new one on the next message that will be properly shared outboundGroupSessionsStore.delete(roomId); } - }, null, null); + }, null); } }