frontend: improve logging

This commit is contained in:
hippoz 2022-04-23 23:07:46 +03:00
parent 2574e7e0e6
commit 15e57c2372
Signed by: hippoz
GPG key ID: 7C52899193467641
4 changed files with 39 additions and 8 deletions

View file

@ -1,3 +1,4 @@
import logging from "./logging";
import { getAuthToken, getItem } from "./storage"; import { getAuthToken, getItem } from "./storage";
export const GatewayPayloadType = { export const GatewayPayloadType = {
@ -22,6 +23,8 @@ export const GatewayEventType = {
Close: -4 Close: -4
} }
const log = logging.logger("Gateway");
export default { export default {
ws: null, ws: null,
authenticated: false, authenticated: false,
@ -33,8 +36,6 @@ export default {
reconnectTimeout: null, reconnectTimeout: null,
handlers: new Map(), handlers: new Map(),
init() { init() {
window.__WAFFLE_GATEWAY = this;
const token = getAuthToken(); const token = getAuthToken();
if (!token) { if (!token) {
return false; return false;
@ -46,7 +47,7 @@ export default {
} }
this.open = true; this.open = true;
this.dispatch(GatewayEventType.Open, null); this.dispatch(GatewayEventType.Open, null);
console.log("[gateway] open"); log("open");
}; };
this.ws.onmessage = (event) => { this.ws.onmessage = (event) => {
const payload = JSON.parse(event.data); const payload = JSON.parse(event.data);
@ -65,7 +66,7 @@ export default {
}); });
}, payload.d.pingInterval); }, payload.d.pingInterval);
console.log("[gateway] hello"); log("hello");
break; break;
} }
case GatewayPayloadType.Ready: { case GatewayPayloadType.Ready: {
@ -74,7 +75,7 @@ export default {
this.reconnectDelay = 400; this.reconnectDelay = 400;
console.log("[gateway] ready"); log("ready");
break; break;
} }
} }
@ -98,7 +99,7 @@ export default {
this.dispatch(GatewayEventType.Close, null); this.dispatch(GatewayEventType.Close, null);
console.log("[gateway] close"); log("close");
}; };
return true; return true;

21
frontend/src/logging.js Normal file
View file

@ -0,0 +1,21 @@
export default {
sinks: new Map(),
logger(sink, enabled=false) {
this.sinks.set(sink, enabled);
return (...args) => {
if (this.sinks.get(sink)) {
console.log(
`%c[${sink}]`,
"color: #8f3f71; font-weight: bold;",
...args
);
}
};
},
enableSink(sink) {
this.sinks.set(sink, true);
},
disableSink(sink) {
this.sinks.set(sink, false);
}
};

View file

@ -1,6 +1,12 @@
import App from './components/App.svelte'; import App from './components/App.svelte';
import gateway from './gateway'; import gateway from './gateway';
import { initStorageDefaults } from './storage'; import { initStorageDefaults } from './storage';
import logging from "./logging";
window.__waffle = {
logging,
gateway
};
initStorageDefaults(); initStorageDefaults();
gateway.init(); gateway.init();

View file

@ -1,8 +1,11 @@
import { writable } from "svelte/store"; import { writable } from "svelte/store";
import gateway, { GatewayEventType } from "./gateway"; import gateway, { GatewayEventType } from "./gateway";
import logging from "./logging";
import request from "./request"; import request from "./request";
import { apiRoute } from "./storage"; import { apiRoute } from "./storage";
const storeLog = logging.logger("Store");
class Store { class Store {
constructor(value=null) { constructor(value=null) {
this._handlers = []; this._handlers = [];
@ -13,14 +16,14 @@ class Store {
const newLength = this._handlers.push(handler); const newLength = this._handlers.push(handler);
const handlerIndex = newLength - 1; const handlerIndex = newLength - 1;
handler(this.value); handler(this.value);
console.log(this.value); storeLog("Subscription initialized with value", this.value);
return () => { return () => {
this._handlers.splice(handlerIndex, 1); this._handlers.splice(handlerIndex, 1);
}; };
} }
updated() { updated() {
console.log(this.value); storeLog(`updated(): calling ${this._handlers.length} handlers - value changed`, this.value);
this._handlers.forEach(e => { this._handlers.forEach(e => {
e(this.value); e(this.value);
}); });