From 238ffb6c403393115f1f5b825cd41aed5fc3bf20 Mon Sep 17 00:00:00 2001 From: Martin Trapp <94928215+martrapp@users.noreply.github.com> Date: Sat, 23 Mar 2024 21:01:05 +0100 Subject: [PATCH] shorten file name in console log (#49) --- components/VtBotDebug.astro | 24 ++++++++++----------- components/VtBotDebug1.astro | 37 -------------------------------- components/{prefix.ts => log.ts} | 9 ++++++++ 3 files changed, 21 insertions(+), 49 deletions(-) rename components/{prefix.ts => log.ts} (72%) diff --git a/components/VtBotDebug.astro b/components/VtBotDebug.astro index 356e8ce..a0e9105 100644 --- a/components/VtBotDebug.astro +++ b/components/VtBotDebug.astro @@ -27,7 +27,7 @@ const active = import.meta.env.DEV || production; } from 'astro:transitions/client'; import { elementsWithStyleProperty } from './css'; import { deriveCSSSelector } from './derive-css-selector'; - import * as prefix from './prefix'; + import * as prefix from './log'; const BOLD = 'font-weight: 800'; const NORMAL = ''; @@ -90,7 +90,7 @@ const active = import.meta.env.DEV || production; css.push(BOLD); css.push(NORMAL); } - (usePrefix ? prefix : console).log(s, ...css); + prefix[usePrefix ? "log": "slog"](s, ...css); }; /* @@ -265,10 +265,10 @@ const active = import.meta.env.DEV || production; s += `${attribute.name}="${attribute.value}" `; } if (script.src) { - console.log(s, ALTERNATE, NORMAL); + prefix.slog(s, ALTERNATE, NORMAL); } else { - console.groupCollapsed(s + 'inline script ', ALTERNATE, NORMAL); - console.log(script.textContent ?? ''); + prefix.sgroupCollapsed(s + 'inline script ', ALTERNATE, NORMAL); + prefix.slog(script.textContent ?? ''); console.groupEnd(); } return idx; @@ -286,24 +286,24 @@ const active = import.meta.env.DEV || production; const keys = Object.keys(anims); if (keys.length === 0) return; if (keys.length > 1) { - console.group(name); + prefix.sgroup(name); keys.sort().forEach((anim) => { const a = anims[anim]!; - console.log(`${anim} from ${stamp(a.start)} to ${stamp(a.end)}`); + prefix.slog(`${anim} from ${stamp(a.start)} to ${stamp(a.end)}`); }); console.groupEnd(); } else { const k = keys[0]!; const a = anims[k]!; - console.log(`${name} ${k} from ${stamp(a.start)} to ${stamp(a.end)}`); + prefix.slog(`${name} ${k} from ${stamp(a.start)} to ${stamp(a.end)}`); } } function logAnimTransition(name: string, idx: number, transition: AnimTransition) { if (idx >= 0) { - console.groupCollapsed(`%ctransition ${++idx} %c${name}`, ALTERNATE, BOLD); + prefix.sgroupCollapsed(`%ctransition ${++idx} %c${name}`, ALTERNATE, BOLD); } else { - console.groupCollapsed(`%cother`, ALTERNATE); + prefix.sgroupCollapsed(`%cother`, ALTERNATE); } const group = `::view-transition-group(${name})`; if (group in transition) { @@ -371,7 +371,7 @@ const active = import.meta.env.DEV || production; }; doCapture(preparationEvent); } else { - console.log('not a TransitionBeforePreparationEvent'); + prefix.slog('not a TransitionBeforePreparationEvent'); } } } @@ -432,7 +432,7 @@ const active = import.meta.env.DEV || production; } ); } else { - console.log('not a TransitionBeforeSwapEvent'); + prefix.slog('not a TransitionBeforeSwapEvent'); } } } diff --git a/components/VtBotDebug1.astro b/components/VtBotDebug1.astro index 8a72ec4..4835dbd 100644 --- a/components/VtBotDebug1.astro +++ b/components/VtBotDebug1.astro @@ -20,43 +20,6 @@ }; }; - /* - const file = (stacktrace) => { - if (!stacktrace) return ''; - const lines = stacktrace.split('\n'); - if (lines[0] === 'Error') lines.shift(); - lines.shift(); - let line = lines.shift(); - while (line && line.endsWith('[native code]')) { - line = lines.shift(); - } - if (!line) return ''; - const match = line.match(/@(.*):\d+:\d+$|at (.*):\d+:\d+$|\((.*):\d+:\d+\)$/); - return match ? match[1] ?? match[2] ?? match[3] : ''; - }; - - - const originalAddEventListener = EventTarget.prototype.addEventListener; - EventTarget.prototype.addEventListener = function (eventName, eventHandler) { - if (this !== window || this !== document) { - const srcFile = file(new Error().stack); - if (srcFile === '') { - console.warn( - 'Could not determine source file for event listener', - this, - eventName, - eventHandler - ); - } - const scripts = window._vtbot_debug.listeningScripts; - const script = (scripts[srcFile] ??= new Map()); - script.has(this) || script.set(this, new Set()); - script.get(this).add('event: ' + eventName); - } - originalAddEventListener.call(this, eventName, eventHandler); - }; - */ - /* * capture the event properties at beginning of the processing phase */ diff --git a/components/prefix.ts b/components/log.ts similarity index 72% rename from components/prefix.ts rename to components/log.ts index 72970d6..820bb54 100644 --- a/components/prefix.ts +++ b/components/log.ts @@ -17,3 +17,12 @@ export function groupCollapsed(...args: any[]) { export function group(...args: any[]) { console.group(PREFIX + (args.shift() ?? ''), PREFIX_ON, PREFIX_OFF, ...args); } +export function slog(...args: any[]) { + console.log(...args); +} +export function sgroupCollapsed(...args: any[]) { + console.groupCollapsed(...args); +} +export function sgroup(...args: any[]) { + console.group(...args); +}