Skip to content

Commit

Permalink
shorten file name in console log (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
martrapp authored Mar 23, 2024
1 parent 36a4944 commit 238ffb6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 49 deletions.
24 changes: 12 additions & 12 deletions components/VtBotDebug.astro
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand Down Expand Up @@ -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);
};

/*
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -371,7 +371,7 @@ const active = import.meta.env.DEV || production;
};
doCapture(preparationEvent);
} else {
console.log('not a TransitionBeforePreparationEvent');
prefix.slog('not a TransitionBeforePreparationEvent');
}
}
}
Expand Down Expand Up @@ -432,7 +432,7 @@ const active = import.meta.env.DEV || production;
}
);
} else {
console.log('not a TransitionBeforeSwapEvent');
prefix.slog('not a TransitionBeforeSwapEvent');
}
}
}
Expand Down
37 changes: 0 additions & 37 deletions components/VtBotDebug1.astro
Original file line number Diff line number Diff line change
Expand Up @@ -20,43 +20,6 @@
};
};

/*
const file = (stacktrace) => {
if (!stacktrace) return '<unknown>';
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 '<unknown>';
const match = line.match(/@(.*):\d+:\d+$|at (.*):\d+:\d+$|\((.*):\d+:\d+\)$/);
return match ? match[1] ?? match[2] ?? match[3] : '<unknown>';
};


const originalAddEventListener = EventTarget.prototype.addEventListener;
EventTarget.prototype.addEventListener = function (eventName, eventHandler) {
if (this !== window || this !== document) {
const srcFile = file(new Error().stack);
if (srcFile === '<unknown>') {
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
*/
Expand Down
9 changes: 9 additions & 0 deletions components/prefix.ts → components/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 238ffb6

Please sign in to comment.