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) { 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; item.end = item.start;
this._persistItem(item, undefined, false); 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. */ /** 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) { if (item) {
return item.wrap(labelOrValues, callback, logLevel, filterCreator); return item.wrap(labelOrValues, callback, logLevel, filterCreator);
} else { } else {
@ -51,28 +51,28 @@ export abstract class BaseLogger {
Useful to pair with LogItem.refDetached. Useful to pair with LogItem.refDetached.
@return {LogItem} the log item added, useful to pass to 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? // todo: Remove jsdoc type?
if (logLevel === null) { if (logLevel === null) {
logLevel = LogLevel.Info; logLevel = LogLevel.Info;
} }
const item = new LogItem(labelOrValues, logLevel, null, this); const item = new LogItem(labelOrValues, logLevel, this);
this._run(item, callback, logLevel!, filterCreator, false /* don't throw, nobody is awaiting */); this._run(item, callback, logLevel!, false /* don't throw, nobody is awaiting */, filterCreator);
return item; return item;
} }
/** run a callback wrapped in a log operation. /** run a callback wrapped in a log operation.
Errors and duration are transparently logged, also for async operations. Errors and duration are transparently logged, also for async operations.
Whatever the callback returns is returned here. */ 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) { if (logLevel === null) {
logLevel = LogLevel.Info; logLevel = LogLevel.Info;
} }
const item = new LogItem(labelOrValues, logLevel, null, this); const item = new LogItem(labelOrValues, logLevel, this);
return this._run(item, callback, logLevel!, filterCreator, true); 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); this._openItems.add(item);
const finishItem = () => { const finishItem = () => {

View file

@ -43,7 +43,7 @@ export interface ILogItem {
children: Array<ILogItem> | null; children: Array<ILogItem> | null;
values: LogItemValues; values: LogItemValues;
error: Error | null; 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; log(labelOrValues: LabelOrValues, logLevel: LogLevelOrNull): void;
set(key: string | object, value: unknown): void; set(key: string | object, value: unknown): void;
run(callback: LogCallback): unknown; run(callback: LogCallback): unknown;
@ -68,7 +68,7 @@ export type LogItemValues = {
} }
export type LabelOrValues = string | 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 type LogCallback = (item: ILogItem) => unknown;
export class LogItem implements ILogItem { export class LogItem implements ILogItem {
@ -78,10 +78,10 @@ export class LogItem implements ILogItem {
public end: number | null; public end: number | null;
private _values: LogItemValues; private _values: LogItemValues;
private _logger: BaseLogger; private _logger: BaseLogger;
private _filterCreator: FilterCreator; private _filterCreator?: FilterCreator;
private _children: Array<LogItem> | null; 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._logger = logger;
this.start = logger._now(); this.start = logger._now();
this.end = null; this.end = null;
@ -119,7 +119,7 @@ export class LogItem implements ILogItem {
/** /**
* Creates a new child item and runs it in `callback`. * 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); const item = this.child(labelOrValues, logLevel, filterCreator);
return item.run(callback); return item.run(callback);
} }
@ -161,7 +161,7 @@ export class LogItem implements ILogItem {
* Hence, the child item is not returned. * Hence, the child item is not returned.
*/ */
log(labelOrValues: LabelOrValues, logLevel: LogLevelOrNull = null): void { log(labelOrValues: LabelOrValues, logLevel: LogLevelOrNull = null): void {
const item = this.child(labelOrValues, logLevel, null); const item = this.child(labelOrValues, logLevel);
item.end = item.start; item.end = item.start;
} }
@ -291,14 +291,14 @@ export class LogItem implements ILogItem {
return err; return err;
} }
child(labelOrValues: LabelOrValues, logLevel: LogLevelOrNull, filterCreator: FilterCreator): ILogItem { child(labelOrValues: LabelOrValues, logLevel: LogLevelOrNull, filterCreator?: FilterCreator): ILogItem {
if (this.end !== null) { if (this.end !== null) {
console.trace("log item is finished, additional logs will likely not be recorded"); console.trace("log item is finished, additional logs will likely not be recorded");
} }
if (!logLevel) { if (!logLevel) {
logLevel = this.logLevel || LogLevel.Info; 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) { if (this._children === null) {
this._children = []; this._children = [];
} }

View file

@ -152,7 +152,7 @@ export class Decryption {
log.logLevel = log.level.Warn; log.logLevel = log.level.Warn;
log.set("invalid", true); log.set("invalid", true);
} }
}, log.level.Detail, null); }, log.level.Detail);
} }
return keys; return keys;
} }

View file

@ -101,12 +101,8 @@ async function createStores(db: IDBDatabase, txn: IDBTransaction, oldVersion: nu
async (log) => { async (log) => {
for (let i = startIdx; i < version; ++i) { for (let i = startIdx; i < version; ++i) {
const migrationFunc = schema[i]; const migrationFunc = schema[i];
await log.wrap(`v${i + 1}`, (log) => await log.wrap(`v${i + 1}`, (log) => migrationFunc(db, txn, localStorage, log), null);
migrationFunc(db, txn, localStorage, log), null, 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`; const label = `${this._writeErrors.length} storage write operation(s) failed`;
if (parentItem) { if (parentItem) {
parentItem.wrap(label, callback, null, null); parentItem.wrap(label, callback, null);
} else { } else {
this.logger.run(label, callback); 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 // so we'll create a new one on the next message that will be properly shared
outboundGroupSessionsStore.delete(roomId); outboundGroupSessionsStore.delete(roomId);
} }
}, null, null); }, null);
} }
} }