hydrogen-web/src/logging/NullLogger.ts

110 lines
2.6 KiB
TypeScript
Raw Normal View History

2021-02-12 18:36:09 +05:30
/*
2021-03-01 19:31:43 +05:30
Copyright 2021 The Matrix.org Foundation C.I.C.
2021-02-12 18:36:09 +05:30
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.
*/
2021-11-10 12:36:56 +05:30
import {LogLevel} from "./LogFilter";
2021-11-15 17:29:08 +05:30
import type {ILogItem, LabelOrValues, LogCallback, LogItemValues} from "./LogItem";
2021-02-12 18:36:09 +05:30
2021-11-15 17:29:08 +05:30
function noop (): void {}
2021-02-12 18:36:09 +05:30
export class NullLogger {
2021-11-15 17:29:08 +05:30
public readonly item: ILogItem = new NullLogItem(this);
2021-02-12 18:36:09 +05:30
2021-11-15 17:29:08 +05:30
log(): void {}
2021-02-22 15:18:46 +05:30
2021-11-16 12:23:06 +05:30
run<T>(_, callback: LogCallback<T>): T | Promise<T> {
return callback(this.item);
}
2021-11-16 12:23:06 +05:30
wrapOrRun<T>(item: ILogItem, _, callback: LogCallback<T>): T | Promise<T> {
if (item) {
2021-11-16 12:23:06 +05:30
return item.wrap(_, callback);
} else {
2021-11-16 12:23:06 +05:30
return this.run(_, callback);
}
}
runDetached(_, callback) {
new Promise(r => r(callback(this.item))).then(noop, noop);
2021-02-12 18:36:09 +05:30
}
2021-11-16 12:23:06 +05:30
async export(): Promise<null> {
2021-02-12 18:36:09 +05:30
return null;
}
2021-11-16 12:23:06 +05:30
get level(): typeof LogLevel {
2021-02-12 18:36:09 +05:30
return LogLevel;
}
}
2021-11-15 17:29:08 +05:30
export class NullLogItem implements ILogItem {
public readonly logger: NullLogger;
public readonly logLevel: LogLevel;
public children?: Array<ILogItem>;
2021-11-15 17:29:08 +05:30
public values: LogItemValues;
public error?: Error;
2021-11-15 17:29:08 +05:30
constructor(logger: NullLogger) {
this.logger = logger;
}
wrap<T>(_: LabelOrValues, callback: LogCallback<T>): T {
2021-02-16 20:05:30 +05:30
return callback(this);
2021-02-12 18:36:09 +05:30
}
2021-11-16 12:23:06 +05:30
2021-11-15 17:29:08 +05:30
log(): void {}
set(): void {}
2021-11-15 22:47:38 +05:30
runDetached(_: LabelOrValues, callback: LogCallback<unknown>): ILogItem {
new Promise(r => r(callback(this))).then(noop, noop);
2021-11-15 17:29:08 +05:30
return this;
}
2021-11-15 22:47:38 +05:30
wrapDetached(_: LabelOrValues, _callback: LogCallback<unknown>): void {
2021-11-15 17:29:08 +05:30
return this.refDetached();
}
run<T>(callback: LogCallback<T>): T {
return callback(this);
}
2021-11-15 17:29:08 +05:30
refDetached(): void {}
2021-02-12 18:36:09 +05:30
2021-11-15 17:29:08 +05:30
ensureRefId(): void {}
2021-11-15 17:29:08 +05:30
get level(): typeof LogLevel {
2021-02-12 18:36:09 +05:30
return LogLevel;
}
2021-02-12 23:05:33 +05:30
2021-11-15 17:29:08 +05:30
get duration(): 0 {
return 0;
}
2021-11-15 17:29:08 +05:30
catch(err: Error): Error {
2021-02-12 23:05:33 +05:30
return err;
}
2021-11-16 12:23:06 +05:30
child(): ILogItem {
2021-02-12 23:05:33 +05:30
return this;
}
2021-11-15 17:29:08 +05:30
finish(): void {}
2021-11-16 12:23:06 +05:30
serialize(): undefined {
2021-11-15 19:32:16 +05:30
return undefined;
2021-11-15 17:29:08 +05:30
}
2021-02-12 18:36:09 +05:30
}
2021-03-15 19:53:35 +05:30
export const Instance = new NullLogger();