@@ -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, timeLog, timeEnd, kNone } = require ( 'internal/util/debuglog' ) ;
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 */ ) {
@@ -373,6 +364,14 @@ function createWriteErrorHandler(instance, streamSymbol) {
373
364
} ;
374
365
}
375
366
367
+ function timeLogImpl ( label , formatted , args ) {
368
+ if ( args === undefined ) {
369
+ this . log ( '%s: %s' , label , formatted ) ;
370
+ } else {
371
+ this . log ( '%s: %s' , label , formatted , ...new SafeArrayIterator ( args ) ) ;
372
+ }
373
+ }
374
+
376
375
const consoleMethods = {
377
376
log ( ...args ) {
378
377
this [ kWriteToConsole ] ( kUseStdout , this [ kFormatForStdout ] ( args ) ) ;
@@ -393,31 +392,21 @@ const consoleMethods = {
393
392
} ,
394
393
395
394
time ( label = 'default' ) {
396
- // Coerces everything other than Symbol to a string
397
- label = `${ label } ` ;
398
- if ( this . _times . has ( label ) ) {
399
- process . emitWarning ( `Label '${ label } ' already exists for console.time()` ) ;
400
- return ;
401
- }
402
- trace ( kTraceBegin , kTraceConsoleCategory , `time::${ label } ` , 0 ) ;
403
- this . _times . set ( label , process . hrtime ( ) ) ;
395
+ time ( this . _times , kTraceConsoleCategory , 'console.time()' , kNone , label , `time::${ label } ` ) ;
404
396
} ,
405
397
406
398
timeEnd ( label = 'default' ) {
407
- // Coerces everything other than Symbol to a string
408
- label = `${ label } ` ;
409
- const found = timeLogImpl ( this , 'timeEnd' , label ) ;
410
- trace ( kTraceEnd , kTraceConsoleCategory , `time::${ label } ` , 0 ) ;
411
- if ( found ) {
412
- this . _times . delete ( label ) ;
413
- }
399
+ if ( this [ kInternalTimeLogImpl ] === undefined )
400
+ this [ kInternalTimeLogImpl ] = FunctionPrototypeBind ( timeLogImpl , this ) ;
401
+
402
+ timeEnd ( this . _times , kTraceConsoleCategory , 'console.timeEnd()' , kNone , this [ kInternalTimeLogImpl ] , label , `time::${ label } ` ) ;
414
403
} ,
415
404
416
405
timeLog ( label = 'default' , ...data ) {
417
- // Coerces everything other than Symbol to a string
418
- label = ` ${ label } ` ;
419
- timeLogImpl ( this , 'timeLog' , label , data ) ;
420
- trace ( kTraceInstant , kTraceConsoleCategory , `time::${ label } ` , 0 ) ;
406
+ if ( this [ kInternalTimeLogImpl ] === undefined )
407
+ this [ kInternalTimeLogImpl ] = FunctionPrototypeBind ( timeLogImpl , this ) ;
408
+
409
+ timeLog ( this . _times , kTraceConsoleCategory , 'console.timeLog()' , kNone , this [ kInternalTimeLogImpl ] , label , `time::${ label } ` , data ) ;
421
410
} ,
422
411
423
412
trace : function trace ( ...args ) {
@@ -611,63 +600,6 @@ const consoleMethods = {
611
600
} ,
612
601
} ;
613
602
614
- // Returns true if label was found
615
- function timeLogImpl ( self , name , label , data ) {
616
- const time = self . _times . get ( label ) ;
617
- if ( time === undefined ) {
618
- process . emitWarning ( `No such label '${ label } ' for console.${ name } ()` ) ;
619
- return false ;
620
- }
621
- const duration = process . hrtime ( time ) ;
622
- const ms = duration [ 0 ] * 1000 + duration [ 1 ] / 1e6 ;
623
-
624
- const formatted = formatTime ( ms ) ;
625
-
626
- if ( data === undefined ) {
627
- self . log ( '%s: %s' , label , formatted ) ;
628
- } else {
629
- self . log ( '%s: %s' , label , formatted , ...new SafeArrayIterator ( data ) ) ;
630
- }
631
- return true ;
632
- }
633
-
634
- function pad ( value ) {
635
- return StringPrototypePadStart ( `${ value } ` , 2 , '0' ) ;
636
- }
637
-
638
- function formatTime ( ms ) {
639
- let hours = 0 ;
640
- let minutes = 0 ;
641
- let seconds = 0 ;
642
-
643
- if ( ms >= kSecond ) {
644
- if ( ms >= kMinute ) {
645
- if ( ms >= kHour ) {
646
- hours = MathFloor ( ms / kHour ) ;
647
- ms = ms % kHour ;
648
- }
649
- minutes = MathFloor ( ms / kMinute ) ;
650
- ms = ms % kMinute ;
651
- }
652
- seconds = ms / kSecond ;
653
- }
654
-
655
- if ( hours !== 0 || minutes !== 0 ) {
656
- ( { 0 : seconds , 1 : ms } = StringPrototypeSplit (
657
- NumberPrototypeToFixed ( seconds , 3 ) ,
658
- '.' ,
659
- ) ) ;
660
- const res = hours !== 0 ? `${ hours } :${ pad ( minutes ) } ` : minutes ;
661
- return `${ res } :${ pad ( seconds ) } .${ ms } (${ hours !== 0 ? 'h:m' : '' } m:ss.mmm)` ;
662
- }
663
-
664
- if ( seconds !== 0 ) {
665
- return `${ NumberPrototypeToFixed ( seconds , 3 ) } s` ;
666
- }
667
-
668
- return `${ Number ( NumberPrototypeToFixed ( ms , 3 ) ) } ms` ;
669
- }
670
-
671
603
const keyKey = 'Key' ;
672
604
const valuesKey = 'Values' ;
673
605
const indexKey = '(index)' ;
@@ -722,5 +654,4 @@ module.exports = {
722
654
kBindStreamsLazy,
723
655
kBindProperties,
724
656
initializeGlobalConsole,
725
- formatTime, // exported for tests
726
657
} ;
0 commit comments