1+ import { flagEnabled } from "./scramjet" ;
2+ import type { URLMeta } from "./shared/rewriters/url" ;
3+
14export 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} ;
0 commit comments