frontend: rework logging system

This commit is contained in:
hippoz 2022-05-05 20:52:35 +03:00
parent 243036f29e
commit 144877e682
Signed by: hippoz
GPG key ID: 7C52899193467641
5 changed files with 20 additions and 28 deletions

View file

@ -1,4 +1,4 @@
import logging from "./logging"; import logger from "./logging";
import { getItem } from "./storage"; import { getItem } from "./storage";
export const GatewayErrors = { export const GatewayErrors = {
@ -35,7 +35,7 @@ export const GatewayEventType = {
BadAuth: -3, BadAuth: -3,
} }
const log = logging.logger("Gateway", true); const log = logger("Gateway");
export default { export default {
ws: null, ws: null,

View file

@ -1,9 +1,8 @@
export default { import { getItem } from "./storage";
sinks: new Map(),
logger(sink, enabled=false) { export default function logger(sink) {
this.sinks.set(sink, enabled);
return (...args) => { return (...args) => {
if (this.sinks.get(sink)) { if (getItem(`loggingSink:${sink}`)) {
console.log( console.log(
`%c[${sink}]`, `%c[${sink}]`,
"color: #8f3f71; font-weight: bold;", "color: #8f3f71; font-weight: bold;",
@ -11,11 +10,4 @@ export default {
); );
} }
}; };
},
enableSink(sink) {
this.sinks.set(sink, true);
},
disableSink(sink) {
this.sinks.set(sink, false);
} }
};

View file

@ -1,12 +1,10 @@
import Main from './components/Main.svelte'; import Main from './components/Main.svelte';
import gateway from './gateway'; import gateway from './gateway';
import { getItem, init } from './storage'; import { getItem, init } from './storage';
import logging from "./logging";
import { authWithToken, useAuthHandlers } from './auth'; import { authWithToken, useAuthHandlers } from './auth';
import { initResponsiveHandlers } from './responsive'; import { initResponsiveHandlers } from './responsive';
window.__waffle = { window.__waffle = {
logging,
gateway gateway
}; };

View file

@ -2,7 +2,9 @@ const defaults = {
apiBase: `${window.location.origin}/api/v1`, apiBase: `${window.location.origin}/api/v1`,
gatewayBase: `${location.protocol === "https:" ? "wss" : "ws"}://${location.host}/gateway`, gatewayBase: `${location.protocol === "https:" ? "wss" : "ws"}://${location.host}/gateway`,
doAnimations: true, doAnimations: true,
token: "" token: "",
"loggingSink:Gateway": false,
"loggingSink:Store": false
}; };
const store = new Map(Object.entries(defaults)); const store = new Map(Object.entries(defaults));
const persistentProvider = localStorage; const persistentProvider = localStorage;

View file

@ -1,9 +1,9 @@
import gateway, { GatewayEventType } from "./gateway"; import gateway, { GatewayEventType } from "./gateway";
import logging from "./logging"; import logger from "./logging";
import request from "./request"; import request from "./request";
import { apiRoute } from "./storage"; import { apiRoute } from "./storage";
const storeLog = logging.logger("Store", true); const storeLog = logger("Store");
class Store { class Store {
constructor(value=null, name="[unnamed]") { constructor(value=null, name="[unnamed]") {