@@ -68,12 +68,10 @@ const {
68
68
CHAR_UPPERCASE_C : kTraceCount ,
69
69
} = require ( 'internal/constants' ) ;
70
70
const kCounts = Symbol ( 'counts' ) ;
71
+ const { time, timeEnd, timeLog } = require ( 'internal/util/trace_timer' ) ;
71
72
72
73
const kTraceConsoleCategory = 'node,node.console' ;
73
74
74
- const kSecond = 1000 ;
75
- const kMinute = 60 * kSecond ;
76
- const kHour = 60 * kMinute ;
77
75
const kMaxGroupIndentation = 1000 ;
78
76
79
77
// Lazy loaded for startup performance.
@@ -99,6 +97,7 @@ const kBindStreamsEager = Symbol('kBindStreamsEager');
99
97
const kBindStreamsLazy = Symbol ( 'kBindStreamsLazy' ) ;
100
98
const kUseStdout = Symbol ( 'kUseStdout' ) ;
101
99
const kUseStderr = Symbol ( 'kUseStderr' ) ;
100
+ const kInternalTimeLogImpl = Symbol ( 'kInternalTimeLogImpl' ) ;
102
101
103
102
const optionsMap = new SafeWeakMap ( ) ;
104
103
function Console ( options /* or: stdout, stderr, ignoreErrors = true */ ) {
@@ -138,14 +137,14 @@ function Console(options /* or: stdout, stderr, ignoreErrors = true */) {
138
137
139
138
if ( groupIndentation !== undefined ) {
140
139
validateInteger ( groupIndentation , 'groupIndentation' ,
141
- 0 , kMaxGroupIndentation ) ;
140
+ 0 , kMaxGroupIndentation ) ;
142
141
}
143
142
144
143
if ( inspectOptions !== undefined ) {
145
144
validateObject ( inspectOptions , 'options.inspectOptions' ) ;
146
145
147
146
if ( inspectOptions . colors !== undefined &&
148
- options . colorMode !== undefined ) {
147
+ options . colorMode !== undefined ) {
149
148
throw new ERR_INCOMPATIBLE_OPTION_PAIR (
150
149
'options.inspectOptions.color' , 'colorMode' ) ;
151
150
}
@@ -190,7 +189,7 @@ ObjectDefineProperties(Console.prototype, {
190
189
__proto__ : null ,
191
190
...consolePropAttributes ,
192
191
// Eager version for the Console constructor
193
- value : function ( stdout , stderr ) {
192
+ value : function ( stdout , stderr ) {
194
193
ObjectDefineProperties ( this , {
195
194
'_stdout' : { __proto__ : null , ...consolePropAttributes , value : stdout } ,
196
195
'_stderr' : { __proto__ : null , ...consolePropAttributes , value : stderr } ,
@@ -202,7 +201,7 @@ ObjectDefineProperties(Console.prototype, {
202
201
...consolePropAttributes ,
203
202
// Lazily load the stdout and stderr from an object so we don't
204
203
// create the stdio streams when they are not even accessed
205
- value : function ( object ) {
204
+ value : function ( object ) {
206
205
let stdout ;
207
206
let stderr ;
208
207
ObjectDefineProperties ( this , {
@@ -232,7 +231,7 @@ ObjectDefineProperties(Console.prototype, {
232
231
[ kBindProperties ] : {
233
232
__proto__ : null ,
234
233
...consolePropAttributes ,
235
- value : function ( ignoreErrors , colorMode , groupIndentation = 2 ) {
234
+ value : function ( ignoreErrors , colorMode , groupIndentation = 2 ) {
236
235
ObjectDefineProperties ( this , {
237
236
'_stdoutErrorHandler' : {
238
237
__proto__ : null ,
@@ -273,7 +272,7 @@ ObjectDefineProperties(Console.prototype, {
273
272
[ kWriteToConsole ] : {
274
273
__proto__ : null ,
275
274
...consolePropAttributes ,
276
- value : function ( streamSymbol , string ) {
275
+ value : function ( streamSymbol , string ) {
277
276
const ignoreErrors = this . _ignoreErrors ;
278
277
const groupIndent = this [ kGroupIndent ] ;
279
278
@@ -316,7 +315,7 @@ ObjectDefineProperties(Console.prototype, {
316
315
[ kGetInspectOptions ] : {
317
316
__proto__ : null ,
318
317
...consolePropAttributes ,
319
- value : function ( stream ) {
318
+ value : function ( stream ) {
320
319
let color = this [ kColorMode ] ;
321
320
if ( color === 'auto' ) {
322
321
color = lazyUtilColors ( ) . shouldColorize ( stream ) ;
@@ -336,7 +335,7 @@ ObjectDefineProperties(Console.prototype, {
336
335
[ kFormatForStdout ] : {
337
336
__proto__ : null ,
338
337
...consolePropAttributes ,
339
- value : function ( args ) {
338
+ value : function ( args ) {
340
339
const opts = this [ kGetInspectOptions ] ( this . _stdout ) ;
341
340
ArrayPrototypeUnshift ( args , opts ) ;
342
341
return ReflectApply ( formatWithOptions , null , args ) ;
@@ -345,7 +344,7 @@ ObjectDefineProperties(Console.prototype, {
345
344
[ kFormatForStderr ] : {
346
345
__proto__ : null ,
347
346
...consolePropAttributes ,
348
- value : function ( args ) {
347
+ value : function ( args ) {
349
348
const opts = this [ kGetInspectOptions ] ( this . _stderr ) ;
350
349
ArrayPrototypeUnshift ( args , opts ) ;
351
350
return ReflectApply ( formatWithOptions , null , args ) ;
@@ -374,6 +373,14 @@ function createWriteErrorHandler(instance, streamSymbol) {
374
373
} ;
375
374
}
376
375
376
+ function timeLogImpl ( label , formatted , args ) {
377
+ if ( args === undefined ) {
378
+ this . log ( '%s: %s' , label , formatted ) ;
379
+ } else {
380
+ this . log ( '%s: %s' , label , formatted , ...new SafeArrayIterator ( args ) ) ;
381
+ }
382
+ }
383
+
377
384
const consoleMethods = {
378
385
log ( ...args ) {
379
386
this [ kWriteToConsole ] ( kUseStdout , this [ kFormatForStdout ] ( args ) ) ;
@@ -394,31 +401,21 @@ const consoleMethods = {
394
401
} ,
395
402
396
403
time ( label = 'default' ) {
397
- // Coerces everything other than Symbol to a string
398
- label = `${ label } ` ;
399
- if ( this . _times . has ( label ) ) {
400
- process . emitWarning ( `Label '${ label } ' already exists for console.time()` ) ;
401
- return ;
402
- }
403
- trace ( kTraceBegin , kTraceConsoleCategory , `time::${ label } ` , 0 ) ;
404
- this . _times . set ( label , process . hrtime ( ) ) ;
404
+ time ( this . _times , kTraceConsoleCategory , 'console.time()' , label ) ;
405
405
} ,
406
406
407
407
timeEnd ( label = 'default' ) {
408
- // Coerces everything other than Symbol to a string
409
- label = `${ label } ` ;
410
- const found = timeLogImpl ( this , 'timeEnd' , label ) ;
411
- trace ( kTraceEnd , kTraceConsoleCategory , `time::${ label } ` , 0 ) ;
412
- if ( found ) {
413
- this . _times . delete ( label ) ;
414
- }
408
+ if ( this [ kInternalTimeLogImpl ] === undefined )
409
+ this [ kInternalTimeLogImpl ] = FunctionPrototypeBind ( timeLogImpl , this )
410
+
411
+ timeEnd ( this . _times , kTraceConsoleCategory , 'console.timeEnd()' , this [ kInternalTimeLogImpl ] , label ) ;
415
412
} ,
416
413
417
414
timeLog ( label = 'default' , ...data ) {
418
- // Coerces everything other than Symbol to a string
419
- label = ` ${ label } ` ;
420
- timeLogImpl ( this , 'timeLog' , label , data ) ;
421
- trace ( kTraceInstant , kTraceConsoleCategory , `time:: ${ label } ` , 0 ) ;
415
+ if ( this [ kInternalTimeLogImpl ] === undefined )
416
+ this [ kInternalTimeLogImpl ] = FunctionPrototypeBind ( timeLogImpl , this )
417
+
418
+ timeLog ( this . _times , kTraceConsoleCategory , 'console.timeLog()' , this [ kInternalTimeLogImpl ] , label , data ) ;
422
419
} ,
423
420
424
421
trace : function trace ( ...args ) {
@@ -515,9 +512,9 @@ const consoleMethods = {
515
512
516
513
const _inspect = ( v ) => {
517
514
const depth = v !== null &&
518
- typeof v === 'object' &&
519
- ! isArray ( v ) &&
520
- ObjectKeys ( v ) . length > 2 ? - 1 : 0 ;
515
+ typeof v === 'object' &&
516
+ ! isArray ( v ) &&
517
+ ObjectKeys ( v ) . length > 2 ? - 1 : 0 ;
521
518
const opt = {
522
519
depth,
523
520
maxArrayLength : 3 ,
@@ -587,7 +584,7 @@ const consoleMethods = {
587
584
for ( ; i < indexKeyArray . length ; i ++ ) {
588
585
const item = tabularData [ indexKeyArray [ i ] ] ;
589
586
const primitive = item === null ||
590
- ( typeof item !== 'function' && typeof item !== 'object' ) ;
587
+ ( typeof item !== 'function' && typeof item !== 'object' ) ;
591
588
if ( properties === undefined && primitive ) {
592
589
hasPrimitives = true ;
593
590
valuesKeyArray [ i ] = _inspect ( item ) ;
@@ -596,7 +593,7 @@ const consoleMethods = {
596
593
for ( const key of keys ) {
597
594
map [ key ] ??= [ ] ;
598
595
if ( ( primitive && properties ) ||
599
- ! ObjectPrototypeHasOwnProperty ( item , key ) )
596
+ ! ObjectPrototypeHasOwnProperty ( item , key ) )
600
597
map [ key ] [ i ] = '' ;
601
598
else
602
599
map [ key ] [ i ] = _inspect ( item [ key ] ) ;
@@ -617,71 +614,14 @@ const consoleMethods = {
617
614
} ,
618
615
} ;
619
616
620
- // Returns true if label was found
621
- function timeLogImpl ( self , name , label , data ) {
622
- const time = self . _times . get ( label ) ;
623
- if ( time === undefined ) {
624
- process . emitWarning ( `No such label '${ label } ' for console.${ name } ()` ) ;
625
- return false ;
626
- }
627
- const duration = process . hrtime ( time ) ;
628
- const ms = duration [ 0 ] * 1000 + duration [ 1 ] / 1e6 ;
629
-
630
- const formatted = formatTime ( ms ) ;
631
-
632
- if ( data === undefined ) {
633
- self . log ( '%s: %s' , label , formatted ) ;
634
- } else {
635
- self . log ( '%s: %s' , label , formatted , ...new SafeArrayIterator ( data ) ) ;
636
- }
637
- return true ;
638
- }
639
-
640
- function pad ( value ) {
641
- return StringPrototypePadStart ( `${ value } ` , 2 , '0' ) ;
642
- }
643
-
644
- function formatTime ( ms ) {
645
- let hours = 0 ;
646
- let minutes = 0 ;
647
- let seconds = 0 ;
648
-
649
- if ( ms >= kSecond ) {
650
- if ( ms >= kMinute ) {
651
- if ( ms >= kHour ) {
652
- hours = MathFloor ( ms / kHour ) ;
653
- ms = ms % kHour ;
654
- }
655
- minutes = MathFloor ( ms / kMinute ) ;
656
- ms = ms % kMinute ;
657
- }
658
- seconds = ms / kSecond ;
659
- }
660
-
661
- if ( hours !== 0 || minutes !== 0 ) {
662
- ( { 0 : seconds , 1 : ms } = StringPrototypeSplit (
663
- NumberPrototypeToFixed ( seconds , 3 ) ,
664
- '.' ,
665
- ) ) ;
666
- const res = hours !== 0 ? `${ hours } :${ pad ( minutes ) } ` : minutes ;
667
- return `${ res } :${ pad ( seconds ) } .${ ms } (${ hours !== 0 ? 'h:m' : '' } m:ss.mmm)` ;
668
- }
669
-
670
- if ( seconds !== 0 ) {
671
- return `${ NumberPrototypeToFixed ( seconds , 3 ) } s` ;
672
- }
673
-
674
- return `${ Number ( NumberPrototypeToFixed ( ms , 3 ) ) } ms` ;
675
- }
676
-
677
617
const keyKey = 'Key' ;
678
618
const valuesKey = 'Values' ;
679
619
const indexKey = '(index)' ;
680
620
const iterKey = '(iteration index)' ;
681
621
682
622
const isArray = ( v ) => ArrayIsArray ( v ) || isTypedArray ( v ) || isBuffer ( v ) ;
683
623
684
- function noop ( ) { }
624
+ function noop ( ) { }
685
625
686
626
for ( const method of ReflectOwnKeys ( consoleMethods ) )
687
627
Console . prototype [ method ] = consoleMethods [ method ] ;
@@ -728,5 +668,4 @@ module.exports = {
728
668
kBindStreamsLazy,
729
669
kBindProperties,
730
670
initializeGlobalConsole,
731
- formatTime, // exported for tests
732
671
} ;
0 commit comments