From 0b4eca47249e9a7dfa9272c40fcfd6c1faf2a0fe Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Wed, 10 Nov 2021 14:29:23 +0530 Subject: [PATCH] Create alias for LogLevel | null --- src/logging/BaseLogger.ts | 12 ++++++------ src/logging/LogFilter.ts | 2 ++ src/logging/LogItem.ts | 14 +++++++------- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/logging/BaseLogger.ts b/src/logging/BaseLogger.ts index 60388f77..bd17afde 100644 --- a/src/logging/BaseLogger.ts +++ b/src/logging/BaseLogger.ts @@ -16,7 +16,7 @@ limitations under the License. */ import {LogItem, LabelOrValues, FilterCreator, LogCallback} from "./LogItem"; -import {LogLevel, LogFilter} from "./LogFilter"; +import {LogLevel, LogFilter, LogLevelOrNull} from "./LogFilter"; import {Platform} from "../platform/web/Platform.js"; @@ -28,14 +28,14 @@ export abstract class BaseLogger { this._platform = platform; } - log(labelOrValues: LabelOrValues, logLevel: number = LogLevel.Info) { + log(labelOrValues: LabelOrValues, logLevel: LogLevel = LogLevel.Info) { const item = new LogItem(labelOrValues, logLevel, null, this); item.end = item.start; this._persistItem(item, null, false); } /** if item is a log item, wrap the callback in a child of it, otherwise start a new root log item. */ - wrapOrRun(item: LogItem, labelOrValues: LabelOrValues, callback: LogCallback, logLevel: LogLevel = null, filterCreator: FilterCreator = null) { + wrapOrRun(item: LogItem, labelOrValues: LabelOrValues, callback: LogCallback, logLevel: LogLevelOrNull = null, filterCreator: FilterCreator = null) { if (item) { return item.wrap(labelOrValues, callback, logLevel, filterCreator); } else { @@ -48,7 +48,7 @@ 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: LogLevel = null, filterCreator: FilterCreator = null) { + runDetached(labelOrValues: LabelOrValues, callback: LogCallback, logLevel: LogLevelOrNull = null, filterCreator: FilterCreator = null) { if (logLevel === null) { logLevel = LogLevel.Info; } @@ -60,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 = null, filterCreator: FilterCreator = null) { + run(labelOrValues: LabelOrValues, callback: LogCallback, logLevel: LogLevelOrNull = null, filterCreator: FilterCreator = null) { if (logLevel === null) { logLevel = LogLevel.Info; } @@ -131,7 +131,7 @@ export abstract class BaseLogger { this._openItems.clear(); } - abstract _persistItem(item: LogItem, filter?: LogFilter, forced?: boolean): void; + abstract _persistItem(item: LogItem, filter?: LogFilter | null, forced?: boolean): void; abstract export(): void; diff --git a/src/logging/LogFilter.ts b/src/logging/LogFilter.ts index 83f71891..3099d826 100644 --- a/src/logging/LogFilter.ts +++ b/src/logging/LogFilter.ts @@ -26,6 +26,8 @@ export enum LogLevel { Off } +export type LogLevelOrNull = LogLevel | null; + export class LogFilter { constructor(parentFilter) { this._parentFilter = parentFilter; diff --git a/src/logging/LogItem.ts b/src/logging/LogItem.ts index a7f05768..094dc7a2 100644 --- a/src/logging/LogItem.ts +++ b/src/logging/LogItem.ts @@ -16,7 +16,7 @@ limitations under the License. */ import {BaseLogger} from "./BaseLogger"; -import {LogLevel, LogFilter} from "./LogFilter"; +import {LogLevel, LogLevelOrNull, LogFilter} from "./LogFilter"; type LogItemWithLabel = { l: string; @@ -64,18 +64,18 @@ export class LogItem { } /** start a new root log item and run it detached mode, see BaseLogger.runDetached */ - runDetached(labelOrValues: LabelOrValues, callback: LogCallback, logLevel: LogLevel, filterCreator: FilterCreator) { + runDetached(labelOrValues: LabelOrValues, callback: LogCallback, logLevel: LogLevelOrNull, filterCreator: FilterCreator) { return this._logger.runDetached(labelOrValues, callback, logLevel, filterCreator); } /** start a new detached root log item and log a reference to it from this item */ - wrapDetached(labelOrValues: LabelOrValues, callback: LogCallback, logLevel: LogLevel, filterCreator: FilterCreator) { + wrapDetached(labelOrValues: LabelOrValues, callback: LogCallback, logLevel: LogLevelOrNull, filterCreator: FilterCreator) { this.refDetached(this.runDetached(labelOrValues, callback, logLevel, filterCreator)); } /** logs a reference to a different log item, usually obtained from runDetached. This is useful if the referenced operation can't be awaited. */ - refDetached(logItem: LogItem, logLevel: LogLevel | null = null) { + refDetached(logItem: LogItem, logLevel: LogLevelOrNull = null) { logItem.ensureRefId(); return this.log({ref: logItem._values.refId as number}, logLevel); } @@ -89,7 +89,7 @@ export class LogItem { /** * Creates a new child item and runs it in `callback`. */ - wrap(labelOrValues: LabelOrValues, callback: LogCallback, logLevel: LogLevel | null = null, filterCreator: FilterCreator = null) { + wrap(labelOrValues: LabelOrValues, callback: LogCallback, logLevel: LogLevelOrNull = null, filterCreator: FilterCreator = null) { const item = this.child(labelOrValues, logLevel, filterCreator); return item.run(callback); } @@ -127,7 +127,7 @@ export class LogItem { * * Hence, the child item is not returned. */ - log(labelOrValues: LabelOrValues, logLevel: LogLevel | null = null) { + log(labelOrValues: LabelOrValues, logLevel: LogLevelOrNull = null) { const item = this.child(labelOrValues, logLevel, null); item.end = item.start; } @@ -270,7 +270,7 @@ export class LogItem { return err; } - child(labelOrValues: LabelOrValues, logLevel: LogLevel | null, filterCreator: FilterCreator) { + child(labelOrValues: LabelOrValues, logLevel: LogLevelOrNull, filterCreator: FilterCreator) { if (this.end !== null) { console.trace("log item is finished, additional logs will likely not be recorded"); }