@@ -12,9 +12,6 @@ const {
12
12
Boolean,
13
13
ErrorCaptureStackTrace,
14
14
FunctionPrototypeBind,
15
- MathFloor,
16
- Number,
17
- NumberPrototypeToFixed,
18
15
ObjectDefineProperties,
19
16
ObjectDefineProperty,
20
17
ObjectKeys,
@@ -29,10 +26,8 @@ const {
29
26
SafeWeakMap,
30
27
SafeSet,
31
28
StringPrototypeIncludes,
32
- StringPrototypePadStart,
33
29
StringPrototypeRepeat,
34
30
StringPrototypeSlice,
35
- StringPrototypeSplit,
36
31
Symbol,
37
32
SymbolHasInstance,
38
33
SymbolToStringTag,
@@ -62,18 +57,13 @@ const {
62
57
isTypedArray, isSet, isMap, isSetIterator, isMapIterator,
63
58
} = require ( 'internal/util/types' ) ;
64
59
const {
65
- CHAR_LOWERCASE_B : kTraceBegin ,
66
- CHAR_LOWERCASE_E : kTraceEnd ,
67
- CHAR_LOWERCASE_N : kTraceInstant ,
68
60
CHAR_UPPERCASE_C : kTraceCount ,
69
61
} = require ( 'internal/constants' ) ;
70
62
const kCounts = Symbol ( 'counts' ) ;
63
+ const { time, timeEnd, timeLog } = require ( 'internal/util/trace_timer' ) ;
71
64
72
65
const kTraceConsoleCategory = 'node,node.console' ;
73
66
74
- const kSecond = 1000 ;
75
- const kMinute = 60 * kSecond ;
76
- const kHour = 60 * kMinute ;
77
67
const kMaxGroupIndentation = 1000 ;
78
68
79
69
// Lazy loaded for startup performance.
@@ -99,6 +89,7 @@ const kBindStreamsEager = Symbol('kBindStreamsEager');
99
89
const kBindStreamsLazy = Symbol ( 'kBindStreamsLazy' ) ;
100
90
const kUseStdout = Symbol ( 'kUseStdout' ) ;
101
91
const kUseStderr = Symbol ( 'kUseStderr' ) ;
92
+ const kInternalTimeLogImpl = Symbol ( 'kInternalTimeLogImpl' ) ;
102
93
103
94
const optionsMap = new SafeWeakMap ( ) ;
104
95
function Console ( options /* or: stdout, stderr, ignoreErrors = true */ ) {
@@ -145,7 +136,7 @@ function Console(options /* or: stdout, stderr, ignoreErrors = true */) {
145
136
validateObject ( inspectOptions , 'options.inspectOptions' ) ;
146
137
147
138
if ( inspectOptions . colors !== undefined &&
148
- options . colorMode !== undefined ) {
139
+ options . colorMode !== undefined ) {
149
140
throw new ERR_INCOMPATIBLE_OPTION_PAIR (
150
141
'options.inspectOptions.color' , 'colorMode' ) ;
151
142
}
@@ -374,6 +365,14 @@ function createWriteErrorHandler(instance, streamSymbol) {
374
365
} ;
375
366
}
376
367
368
+ function timeLogImpl ( label , formatted , args ) {
369
+ if ( args === undefined ) {
370
+ this . log ( '%s: %s' , label , formatted ) ;
371
+ } else {
372
+ this . log ( '%s: %s' , label , formatted , ...new SafeArrayIterator ( args ) ) ;
373
+ }
374
+ }
375
+
377
376
const consoleMethods = {
378
377
log ( ...args ) {
379
378
this [ kWriteToConsole ] ( kUseStdout , this [ kFormatForStdout ] ( args ) ) ;
@@ -394,31 +393,21 @@ const consoleMethods = {
394
393
} ,
395
394
396
395
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 ( ) ) ;
396
+ time ( this . _times , kTraceConsoleCategory , 'console.time()' , label ) ;
405
397
} ,
406
398
407
399
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
- }
400
+ if ( this [ kInternalTimeLogImpl ] === undefined )
401
+ this [ kInternalTimeLogImpl ] = FunctionPrototypeBind ( timeLogImpl , this ) ;
402
+
403
+ timeEnd ( this . _times , kTraceConsoleCategory , 'console.timeEnd()' , this [ kInternalTimeLogImpl ] , label ) ;
415
404
} ,
416
405
417
406
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 ) ;
407
+ if ( this [ kInternalTimeLogImpl ] === undefined )
408
+ this [ kInternalTimeLogImpl ] = FunctionPrototypeBind ( timeLogImpl , this ) ;
409
+
410
+ timeLog ( this . _times , kTraceConsoleCategory , 'console.timeLog()' , this [ kInternalTimeLogImpl ] , label , data ) ;
422
411
} ,
423
412
424
413
trace : function trace ( ...args ) {
@@ -515,9 +504,9 @@ const consoleMethods = {
515
504
516
505
const _inspect = ( v ) => {
517
506
const depth = v !== null &&
518
- typeof v === 'object' &&
519
- ! isArray ( v ) &&
520
- ObjectKeys ( v ) . length > 2 ? - 1 : 0 ;
507
+ typeof v === 'object' &&
508
+ ! isArray ( v ) &&
509
+ ObjectKeys ( v ) . length > 2 ? - 1 : 0 ;
521
510
const opt = {
522
511
depth,
523
512
maxArrayLength : 3 ,
@@ -587,7 +576,7 @@ const consoleMethods = {
587
576
for ( ; i < indexKeyArray . length ; i ++ ) {
588
577
const item = tabularData [ indexKeyArray [ i ] ] ;
589
578
const primitive = item === null ||
590
- ( typeof item !== 'function' && typeof item !== 'object' ) ;
579
+ ( typeof item !== 'function' && typeof item !== 'object' ) ;
591
580
if ( properties === undefined && primitive ) {
592
581
hasPrimitives = true ;
593
582
valuesKeyArray [ i ] = _inspect ( item ) ;
@@ -596,7 +585,7 @@ const consoleMethods = {
596
585
for ( const key of keys ) {
597
586
map [ key ] ??= [ ] ;
598
587
if ( ( primitive && properties ) ||
599
- ! ObjectPrototypeHasOwnProperty ( item , key ) )
588
+ ! ObjectPrototypeHasOwnProperty ( item , key ) )
600
589
map [ key ] [ i ] = '' ;
601
590
else
602
591
map [ key ] [ i ] = _inspect ( item [ key ] ) ;
@@ -617,71 +606,14 @@ const consoleMethods = {
617
606
} ,
618
607
} ;
619
608
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
609
const keyKey = 'Key' ;
678
610
const valuesKey = 'Values' ;
679
611
const indexKey = '(index)' ;
680
612
const iterKey = '(iteration index)' ;
681
613
682
614
const isArray = ( v ) => ArrayIsArray ( v ) || isTypedArray ( v ) || isBuffer ( v ) ;
683
615
684
- function noop ( ) { }
616
+ function noop ( ) { }
685
617
686
618
for ( const method of ReflectOwnKeys ( consoleMethods ) )
687
619
Console . prototype [ method ] = consoleMethods [ method ] ;
@@ -728,5 +660,4 @@ module.exports = {
728
660
kBindStreamsLazy,
729
661
kBindProperties,
730
662
initializeGlobalConsole,
731
- formatTime, // exported for tests
732
663
} ;
0 commit comments