2021-03-01 15:01:43 +01:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2022-05-06 15:54:45 +02:00
|
|
|
import type {ILogger, ILogItem, LogItemValues, ILogReporter} from "./types";
|
|
|
|
import type {LogItem} from "./LogItem";
|
|
|
|
|
|
|
|
export class ConsoleReporter implements ILogReporter {
|
|
|
|
private logger?: ILogger;
|
|
|
|
|
|
|
|
reportItem(item: ILogItem): void {
|
|
|
|
printToConsole(item as LogItem);
|
2021-03-01 15:01:43 +01:00
|
|
|
}
|
2021-11-09 09:23:07 +01:00
|
|
|
|
2022-05-06 15:54:45 +02:00
|
|
|
setLogger(logger: ILogger) {
|
|
|
|
this.logger = logger;
|
2021-11-09 09:23:07 +01:00
|
|
|
}
|
2022-03-30 15:19:07 +02:00
|
|
|
|
|
|
|
printOpenItems(): void {
|
2022-05-06 15:54:45 +02:00
|
|
|
if (!this.logger) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (const item of this.logger.getOpenRootItems()) {
|
|
|
|
this.reportItem(item);
|
2022-03-30 15:19:07 +02:00
|
|
|
}
|
|
|
|
}
|
2021-03-01 15:01:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const excludedKeysFromTable = ["l", "id"];
|
2021-11-14 11:54:16 +01:00
|
|
|
function filterValues(values: LogItemValues): LogItemValues | null {
|
2021-03-01 15:01:43 +01:00
|
|
|
return Object.entries(values)
|
|
|
|
.filter(([key]) => !excludedKeysFromTable.includes(key))
|
2021-11-14 11:54:16 +01:00
|
|
|
.reduce((obj: LogItemValues, [key, value]) => {
|
2021-03-01 15:01:43 +01:00
|
|
|
obj = obj || {};
|
|
|
|
obj[key] = value;
|
|
|
|
return obj;
|
|
|
|
}, null);
|
|
|
|
}
|
|
|
|
|
2021-11-17 14:33:58 +01:00
|
|
|
function printToConsole(item: LogItem): void {
|
2022-04-11 15:54:31 +02:00
|
|
|
const label = `${itemCaption(item)} (@${item.start}ms, duration: ${item.duration}ms)`;
|
2021-11-14 11:54:16 +01:00
|
|
|
const filteredValues = filterValues(item.values);
|
|
|
|
const shouldGroup = item.children || filteredValues;
|
2021-03-01 15:01:43 +01:00
|
|
|
if (shouldGroup) {
|
|
|
|
if (item.error) {
|
|
|
|
console.group(label);
|
|
|
|
} else {
|
|
|
|
console.groupCollapsed(label);
|
|
|
|
}
|
|
|
|
if (item.error) {
|
|
|
|
console.error(item.error);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (item.error) {
|
|
|
|
console.error(item.error);
|
|
|
|
} else {
|
|
|
|
console.log(label);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (filteredValues) {
|
|
|
|
console.table(filteredValues);
|
|
|
|
}
|
2021-11-14 11:54:16 +01:00
|
|
|
if (item.children) {
|
|
|
|
for(const c of item.children) {
|
2021-03-01 15:01:43 +01:00
|
|
|
printToConsole(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (shouldGroup) {
|
|
|
|
console.groupEnd();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-15 12:59:08 +01:00
|
|
|
function itemCaption(item: ILogItem): string {
|
2021-11-14 11:54:16 +01:00
|
|
|
if (item.values.t === "network") {
|
|
|
|
return `${item.values.method} ${item.values.url}`;
|
|
|
|
} else if (item.values.l && typeof item.values.id !== "undefined") {
|
|
|
|
return `${item.values.l} ${item.values.id}`;
|
|
|
|
} else if (item.values.l && typeof item.values.status !== "undefined") {
|
|
|
|
return `${item.values.l} (${item.values.status})`;
|
2022-05-04 18:44:11 +02:00
|
|
|
} else if (item.values.l && typeof item.values.type !== "undefined") {
|
|
|
|
return `${item.values.l} (${item.values.type})`;
|
2021-11-14 11:54:16 +01:00
|
|
|
} else if (item.values.l && item.error) {
|
|
|
|
return `${item.values.l} failed`;
|
|
|
|
} else if (typeof item.values.ref !== "undefined") {
|
|
|
|
return `ref ${item.values.ref}`;
|
2021-03-01 15:01:43 +01:00
|
|
|
} else {
|
2021-11-14 11:54:16 +01:00
|
|
|
return item.values.l || item.values.type;
|
2021-03-01 15:01:43 +01:00
|
|
|
}
|
|
|
|
}
|