Make LogFilter optional

This commit is contained in:
RMidhunSuresh 2021-11-15 18:44:25 +05:30
parent 520e0f1b89
commit 30a384fe1e
6 changed files with 22 additions and 26 deletions

View file

@ -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 = () => {

View file

@ -43,7 +43,7 @@ export interface ILogItem {
children: Array<ILogItem> | 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<LogItem> | 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 = [];
}

View file

@ -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;
}

View file

@ -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<void>;
null) as Promise<void>;
}

View file

@ -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);
}

View file

@ -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);
}
}