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-17 16:08:17 +05:30
|
|
|
import type {ILogger, ILogExport, ILogItem, LabelOrValues, LogCallback, LogItemValues} from "./types";
|
2021-02-12 18:36:09 +05:30
|
|
|
|
2021-11-15 17:29:08 +05:30
|
|
|
function noop (): void {}
|
2021-03-15 17:25:46 +05:30
|
|
|
|
2021-11-17 16:08:17 +05:30
|
|
|
export class NullLogger implements ILogger {
|
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-17 16:08:17 +05:30
|
|
|
run<T>(_, callback: LogCallback<T>): T {
|
2021-03-15 17:25:46 +05:30
|
|
|
return callback(this.item);
|
|
|
|
}
|
|
|
|
|
2021-11-17 16:08:17 +05:30
|
|
|
wrapOrRun<T>(item: ILogItem | undefined, _, callback: LogCallback<T>): T {
|
2021-03-15 17:25:46 +05:30
|
|
|
if (item) {
|
2021-11-16 12:23:06 +05:30
|
|
|
return item.wrap(_, callback);
|
2021-03-15 17:25:46 +05:30
|
|
|
} else {
|
2021-11-16 12:23:06 +05:30
|
|
|
return this.run(_, callback);
|
2021-03-15 17:25:46 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-17 16:08:17 +05:30
|
|
|
runDetached(_, callback): ILogItem {
|
2021-03-15 17:25:46 +05:30
|
|
|
new Promise(r => r(callback(this.item))).then(noop, noop);
|
2021-11-17 16:08:17 +05:30
|
|
|
return this.item;
|
2021-02-12 18:36:09 +05:30
|
|
|
}
|
|
|
|
|
2021-11-17 16:08:17 +05:30
|
|
|
async export(): Promise<ILogExport | undefined> {
|
|
|
|
return undefined;
|
2021-02-12 18:36:09 +05:30
|
|
|
}
|
|
|
|
|
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;
|
2021-11-15 19:17:49 +05:30
|
|
|
public children?: Array<ILogItem>;
|
2021-11-15 17:29:08 +05:30
|
|
|
public values: LogItemValues;
|
2021-11-15 19:17:49 +05:30
|
|
|
public error?: Error;
|
2021-11-15 17:29:08 +05:30
|
|
|
|
|
|
|
constructor(logger: NullLogger) {
|
2021-09-22 00:28:50 +05:30
|
|
|
this.logger = logger;
|
|
|
|
}
|
|
|
|
|
2021-11-17 13:22:19 +05:30
|
|
|
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-12-09 23:14:44 +05:30
|
|
|
log(): ILogItem {
|
|
|
|
return this;
|
|
|
|
}
|
2022-02-10 21:14:40 +05:30
|
|
|
set(): ILogItem { return this; }
|
2021-03-15 17:25:46 +05:30
|
|
|
|
2021-11-15 22:47:38 +05:30
|
|
|
runDetached(_: LabelOrValues, callback: LogCallback<unknown>): ILogItem {
|
2021-03-15 17:25:46 +05:30
|
|
|
new Promise(r => r(callback(this))).then(noop, noop);
|
2021-11-15 17:29:08 +05:30
|
|
|
return this;
|
2021-03-15 17:25:46 +05:30
|
|
|
}
|
|
|
|
|
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();
|
2021-03-15 17:25:46 +05:30
|
|
|
}
|
|
|
|
|
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-09-18 03:49:16 +05:30
|
|
|
|
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 {
|
2021-03-15 17:25:46 +05:30
|
|
|
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();
|