console logger for local dev

This commit is contained in:
Bruno Windels 2021-03-01 15:01:43 +01:00
parent 62863c5eaf
commit 42654dbd4f
8 changed files with 112 additions and 20 deletions

View File

@ -29,7 +29,7 @@
legacyBundle: "lib/olm/olm_legacy.js", legacyBundle: "lib/olm/olm_legacy.js",
wasmBundle: "lib/olm/olm.js", wasmBundle: "lib/olm/olm.js",
} }
})); }, null, {development: true}));
</script> </script>
</body> </body>
</html> </html>

View File

@ -1,5 +1,6 @@
/* /*
Copyright 2020 Bruno Windels <bruno@windels.cloud> Copyright 2020 Bruno Windels <bruno@windels.cloud>
Copyright 2021 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -26,7 +27,7 @@ export class BaseLogger {
log(labelOrValues, logLevel = LogLevel.Info) { log(labelOrValues, logLevel = LogLevel.Info) {
const item = new LogItem(labelOrValues, logLevel, null, this); const item = new LogItem(labelOrValues, logLevel, null, this);
item._end = item._start; item._end = item._start;
this._persistItem(item.serialize(null)); 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. */ /** if item is a log item, wrap the callback in a child of it, otherwise start a new root log item. */
@ -79,12 +80,9 @@ export class BaseLogger {
filter = filter.minLevel(logLevel); filter = filter.minLevel(logLevel);
} }
try { try {
const serialized = item.serialize(filter); this._persistItem(item, filter, false);
if (serialized) {
this._persistItem(serialized);
}
} catch (err) { } catch (err) {
console.error("Could not serialize log item", err); console.error("Could not persist log item", err);
} }
this._openItems.delete(item); this._openItems.delete(item);
}; };
@ -120,11 +118,8 @@ export class BaseLogger {
// for now, serialize with an all-permitting filter // for now, serialize with an all-permitting filter
// as the createFilter function would get a distorted image anyway // as the createFilter function would get a distorted image anyway
// about the duration of the item, etc ... // about the duration of the item, etc ...
const serialized = openItem.serialize(new LogFilter(), 0); // true for force finish
if (serialized) { this._persistItem(openItem, new LogFilter(), true);
serialized.f = true; //(f)orced
this._persistItem(serialized);
}
} catch (err) { } catch (err) {
console.error("Could not serialize log item", err); console.error("Could not serialize log item", err);
} }

View File

@ -0,0 +1,86 @@
/*
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 {BaseLogger} from "./BaseLogger.js";
export class ConsoleLogger extends BaseLogger {
_persistItem(item) {
printToConsole(item);
}
}
const excludedKeysFromTable = ["l", "id"];
function filterValues(values) {
if (!values) {
return null;
}
return Object.entries(values)
.filter(([key]) => !excludedKeysFromTable.includes(key))
.reduce((obj, [key, value]) => {
obj = obj || {};
obj[key] = value;
return obj;
}, null);
}
function printToConsole(item) {
const label = `${itemCaption(item)} (${item.duration}ms)`;
const filteredValues = filterValues(item._values);
const shouldGroup = item._children || filteredValues;
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);
}
if (item._children) {
for(const c of item._children) {
printToConsole(c);
}
}
if (shouldGroup) {
console.groupEnd();
}
}
function itemCaption(item) {
if (item._values?.t === "network") {
return `${item._values?.method} ${item._values?.url}`;
} else if (item._values.l && item._values?.id) {
return `${item._values.l} ${item._values.id}`;
} else if (item._values.l && item._values?.status) {
return `${item._values.l} (${item._values.status})`;
} else if (item._values.l && item.error) {
return `${item._values.l} failed`;
} else if (item._values.ref) {
return `ref ${item._values.ref}`
} else {
return item._values.l || item._values?.type;
}
}

View File

@ -1,5 +1,6 @@
/* /*
Copyright 2020 Bruno Windels <bruno@windels.cloud> Copyright 2020 Bruno Windels <bruno@windels.cloud>
Copyright 2021 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -100,7 +101,8 @@ export class IDBLogger extends BaseLogger {
return openDatabase(this._name, db => db.createObjectStore("logs", {keyPath: "id", autoIncrement: true}), 1); return openDatabase(this._name, db => db.createObjectStore("logs", {keyPath: "id", autoIncrement: true}), 1);
} }
_persistItem(serializedItem) { _persistItem(logItem, filter, forced) {
const serializedItem = logItem.serialize(filter, forced);
this._queuedItems.push({ this._queuedItems.push({
json: JSON.stringify(serializedItem) json: JSON.stringify(serializedItem)
}); });

View File

@ -1,5 +1,5 @@
/* /*
Copyright 2020 Bruno Windels <bruno@windels.cloud> Copyright 2021 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.

View File

@ -1,5 +1,6 @@
/* /*
Copyright 2020 Bruno Windels <bruno@windels.cloud> Copyright 2020 Bruno Windels <bruno@windels.cloud>
Copyright 2021 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -100,7 +101,7 @@ export class LogItem {
} }
} }
serialize(filter, parentStartTime = null) { serialize(filter, parentStartTime = null, forced) {
if (this._filterCreator) { if (this._filterCreator) {
try { try {
filter = this._filterCreator(new LogFilter(filter), this); filter = this._filterCreator(new LogFilter(filter), this);
@ -111,7 +112,7 @@ export class LogItem {
let children; let children;
if (this._children !== null) { if (this._children !== null) {
children = this._children.reduce((array, c) => { children = this._children.reduce((array, c) => {
const s = c.serialize(filter, this._start); const s = c.serialize(filter, this._start, false);
if (s) { if (s) {
if (array === null) { if (array === null) {
array = []; array = [];
@ -142,11 +143,13 @@ export class LogItem {
name: this.error.name name: this.error.name
}; };
} }
if (forced) {
item.f = true; //(f)orced
}
if (children) { if (children) {
// (c)hildren // (c)hildren
item.c = children; item.c = children;
} }
// (f)orced can also be set on an item by the logger
return item; return item;
} }

View File

@ -1,5 +1,5 @@
/* /*
Copyright 2020 Bruno Windels <bruno@windels.cloud> Copyright 2021 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -15,6 +15,7 @@ limitations under the License.
*/ */
import {LogLevel} from "./LogFilter.js"; import {LogLevel} from "./LogFilter.js";
// TODO: add missing methods
export class NullLogger { export class NullLogger {
constructor() { constructor() {
this._item = new NullLogItem(); this._item = new NullLogItem();

View File

@ -22,6 +22,7 @@ import {SettingsStorage} from "./dom/SettingsStorage.js";
import {Encoding} from "./utils/Encoding.js"; import {Encoding} from "./utils/Encoding.js";
import {OlmWorker} from "../../matrix/e2ee/OlmWorker.js"; import {OlmWorker} from "../../matrix/e2ee/OlmWorker.js";
import {IDBLogger} from "../../logging/IDBLogger.js"; import {IDBLogger} from "../../logging/IDBLogger.js";
import {ConsoleLogger} from "../../logging/ConsoleLogger.js";
import {RootView} from "./ui/RootView.js"; import {RootView} from "./ui/RootView.js";
import {Clock} from "./dom/Clock.js"; import {Clock} from "./dom/Clock.js";
import {ServiceWorkerHandler} from "./dom/ServiceWorkerHandler.js"; import {ServiceWorkerHandler} from "./dom/ServiceWorkerHandler.js";
@ -82,14 +83,18 @@ async function loadOlmWorker(paths) {
} }
export class Platform { export class Platform {
constructor(container, paths, cryptoExtras = null) { constructor(container, paths, cryptoExtras = null, options = null) {
this._paths = paths; this._paths = paths;
this._container = container; this._container = container;
this.settingsStorage = new SettingsStorage("hydrogen_setting_v1_"); this.settingsStorage = new SettingsStorage("hydrogen_setting_v1_");
this.clock = new Clock(); this.clock = new Clock();
this.encoding = new Encoding(); this.encoding = new Encoding();
this.random = Math.random; this.random = Math.random;
if (options?.development) {
this.logger = new ConsoleLogger({platform: this});
} else {
this.logger = new IDBLogger({name: "hydrogen_logs", platform: this}); this.logger = new IDBLogger({name: "hydrogen_logs", platform: this});
}
this.history = new History(); this.history = new History();
this.onlineStatus = new OnlineStatus(); this.onlineStatus = new OnlineStatus();
this._serviceWorkerHandler = null; this._serviceWorkerHandler = null;