/* Copyright 2021 The Matrix.org Foundation C.I.C. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import {LogLevel} from "./LogFilter"; import type {ILogger, ILogExport, ILogItem, LabelOrValues, LogCallback, LogItemValues} from "./types"; function noop (): void {} export class NullLogger implements ILogger { public readonly item: ILogItem = new NullLogItem(this); log(): void {} child(): ILogItem { return this.item; } run(_, callback: LogCallback): T { return callback(this.item); } wrapOrRun(item: ILogItem | undefined, _, callback: LogCallback): T { if (item) { return item.wrap(_, callback); } else { return this.run(_, callback); } } runDetached(_, callback): ILogItem { new Promise(r => r(callback(this.item))).then(noop, noop); return this.item; } async export(): Promise { return undefined; } get level(): typeof LogLevel { return LogLevel; } } export class NullLogItem implements ILogItem { public readonly logger: ILogger; public readonly logLevel: LogLevel; public children?: Array; public values: LogItemValues; public error?: Error; constructor(logger: ILogger) { this.logger = logger; } wrap(_: LabelOrValues, callback: LogCallback): T { return this.run(callback); } run(callback: LogCallback): T { return callback(this); } log(): ILogItem { return this; } set(): ILogItem { return this; } runDetached(_: LabelOrValues, callback: LogCallback): ILogItem { new Promise(r => r(callback(this))).then(noop, noop); return this; } wrapDetached(_: LabelOrValues, _callback: LogCallback): void { return this.refDetached(); } refDetached(): void {} ensureRefId(): void {} get level(): typeof LogLevel { return LogLevel; } get duration(): 0 { return 0; } catch(err: Error): Error { return err; } child(): ILogItem { return this; } finish(): void {} forceFinish(): void {} serialize(): undefined { return undefined; } } export const Instance = new NullLogger();