Skip to content

Commit d73c229

Browse files
committed
improve logging logic and add a dbg.time call for easy tracking
1 parent 9b46bce commit d73c229

File tree

5 files changed

+41
-61
lines changed

5 files changed

+41
-61
lines changed

src/client/shared/sourcemaps.ts

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -191,23 +191,7 @@ export default function (client: ScramjetClient, self: Self) {
191191
value: (buf: Array<number>, tag: string) => {
192192
const before = performance.now();
193193
registerRewrites(buf, tag);
194-
const after = performance.now();
195-
196-
const duration = after - before;
197-
198-
if (flagEnabled("rewriterLogs", new URL(location.href))) {
199-
let timespan: string;
200-
if (duration < 1) {
201-
timespan = "BLAZINGLY FAST";
202-
} else if (duration < 500) {
203-
timespan = "decent speed";
204-
} else {
205-
timespan = "really slow";
206-
}
207-
console.log(
208-
`js rewrite parsing for scramtag ${tag} was ${timespan} (${duration.toFixed(2)}ms)`
209-
);
210-
}
194+
dbg.time(client.meta, before, `scramtag parse for ${tag}`);
211195
},
212196
enumerable: false,
213197
writable: false,
@@ -221,23 +205,7 @@ export default function (client: ScramjetClient, self: Self) {
221205
apply(ctx) {
222206
const before = performance.now();
223207
doUnrewrite(ctx);
224-
const after = performance.now();
225-
226-
const duration = after - before;
227-
228-
if (flagEnabled("rewriterLogs", new URL(location.href))) {
229-
let timespan: string;
230-
if (duration < 1) {
231-
timespan = "BLAZINGLY FAST";
232-
} else if (duration < 500) {
233-
timespan = "decent speed";
234-
} else {
235-
timespan = "really slow";
236-
}
237-
console.log(
238-
`js unrewrite for function was ${timespan} (${duration.toFixed(2)}ms)`
239-
);
240-
}
208+
dbg.time(client.meta, before, `scramtag unrewrite for ${ctx.fn.name}`);
241209
},
242210
});
243211
}

src/global.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ declare const dbg: {
33
warn: (message: string, ...args: any[]) => void;
44
error: (message: string, ...args: any[]) => void;
55
debug: (message: string, ...args: any[]) => void;
6+
time: (meta: URLMeta, before: number, type: string) => void;
67
};
78

89
declare const COMMITHASH: string;

src/log.ts

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { flagEnabled } from "./scramjet";
2+
import type { URLMeta } from "./shared/rewriters/url";
3+
14
export default {
25
fmt: function (severity: string, message: string, ...args: any[]) {
36
const old = Error.prepareStackTrace;
@@ -30,6 +33,9 @@ export default {
3033

3134
Error.prepareStackTrace = old;
3235

36+
this.print(severity, fmt, message, ...args);
37+
},
38+
print(severity: string, tag: string, message: string, ...args: any[]) {
3339
const fn = console[severity] || console.log;
3440
const bg = {
3541
log: "#000",
@@ -51,15 +57,15 @@ export default {
5157
}[severity];
5258

5359
fn(
54-
`%c${fmt}%c ${message}`,
60+
`%c${tag}%c ${message}`,
5561
`
56-
background-color: ${bg};
57-
color: ${fg};
58-
padding: ${padding}px;
59-
font-weight: bold;
60-
font-family: monospace;
61-
font-size: 0.9em;
62-
`,
62+
background-color: ${bg};
63+
color: ${fg};
64+
padding: ${padding}px;
65+
font-weight: bold;
66+
font-family: monospace;
67+
font-size: 0.9em;
68+
`,
6369
`${severity === "debug" ? "color: gray" : ""}`,
6470
...args
6571
);
@@ -76,4 +82,23 @@ export default {
7682
debug: function (message: string, ...args: any[]) {
7783
this.fmt("debug", message, ...args);
7884
},
85+
time(meta: URLMeta, before: number, type: string) {
86+
if (!flagEnabled("rewriterLogs", meta.base)) return;
87+
const after = performance.now();
88+
const duration = after - before;
89+
90+
let timespan: string;
91+
if (duration < 1) {
92+
timespan = "BLAZINGLY FAST";
93+
} else if (duration < 500) {
94+
timespan = "decent speed";
95+
} else {
96+
timespan = "really slow";
97+
}
98+
this.print(
99+
"debug",
100+
"[time]",
101+
`${type} was ${timespan} (${duration.toFixed(2)}ms)`
102+
);
103+
},
79104
};

src/shared/rewriters/html.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,14 @@ export function rewriteHtml(
9898
fromTop: boolean = false
9999
) {
100100
const before = performance.now();
101-
let ret = rewriteHtmlInner(html, cookieStore, meta, fromTop);
102-
const after = performance.now();
101+
const ret = rewriteHtmlInner(html, cookieStore, meta, fromTop);
102+
dbg.time(meta, before, "html rewrite");
103103
// let wasm = rewriteHtmlWasm(html, cookieStore, meta, fromTop);
104104
// let js = after - before;
105-
console.log(`html rewrite took ${after - before}ms`);
106105
// console.log(
107106
// `html rewrite took ${js}ms in js and ${wasm}ms in wasm, ${((wasm - js) / js) * 100}%`
108107
// );
108+
109109
return ret;
110110
}
111111

src/shared/rewriters/js.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ function rewriteJsWasm(
4040

4141
return { js: input, tag: "", map: null };
4242
}
43-
const after = performance.now();
43+
dbg.time(meta, before, `oxc rewrite for "${source || "(unknown)"}"`);
44+
4445
let { js, map, scramtag, errors } = out;
45-
let duration = after - before;
4646

4747
if (flagEnabled("sourcemaps", meta.base) && !globalThis.clients) {
4848
globalThis[globalThis.$scramjet.config.globals.pushsourcemapfn](
@@ -59,20 +59,6 @@ function rewriteJsWasm(
5959
}
6060
}
6161

62-
if (flagEnabled("rewriterLogs", meta.base)) {
63-
let timespan: string;
64-
if (duration < 1) {
65-
timespan = "BLAZINGLY FAST";
66-
} else if (duration < 500) {
67-
timespan = "decent speed";
68-
} else {
69-
timespan = "really slow";
70-
}
71-
console.log(
72-
`oxc rewrite for "${source || "(unknown)"}" was ${timespan} (${duration}ms)`
73-
);
74-
}
75-
7662
return {
7763
js: typeof input === "string" ? decoder.decode(js) : js,
7864
tag: scramtag,

0 commit comments

Comments
 (0)