@@ -13,9 +13,6 @@ const {
13
13
Boolean,
14
14
ErrorCaptureStackTrace,
15
15
FunctionPrototypeBind,
16
- MathFloor,
17
- Number,
18
- NumberPrototypeToFixed,
19
16
ObjectDefineProperties,
20
17
ObjectDefineProperty,
21
18
ObjectKeys,
@@ -30,10 +27,8 @@ const {
30
27
SafeSet,
31
28
SafeWeakMap,
32
29
StringPrototypeIncludes,
33
- StringPrototypePadStart,
34
30
StringPrototypeRepeat,
35
31
StringPrototypeSlice,
36
- StringPrototypeSplit,
37
32
Symbol,
38
33
SymbolHasInstance,
39
34
SymbolToStringTag,
@@ -63,19 +58,14 @@ const {
63
58
isTypedArray, isSet, isMap, isSetIterator, isMapIterator,
64
59
} = require ( 'internal/util/types' ) ;
65
60
const {
66
- CHAR_LOWERCASE_B : kTraceBegin ,
67
- CHAR_LOWERCASE_E : kTraceEnd ,
68
- CHAR_LOWERCASE_N : kTraceInstant ,
69
61
CHAR_UPPERCASE_C : kTraceCount ,
70
62
} = require ( 'internal/constants' ) ;
71
63
const { styleText } = require ( 'util' ) ;
72
64
const kCounts = Symbol ( 'counts' ) ;
65
+ const { time, timeLog, timeEnd, kNone } = require ( 'internal/util/debuglog' ) ;
73
66
74
67
const kTraceConsoleCategory = 'node,node.console' ;
75
68
76
- const kSecond = 1000 ;
77
- const kMinute = 60 * kSecond ;
78
- const kHour = 60 * kMinute ;
79
69
const kMaxGroupIndentation = 1000 ;
80
70
81
71
// Lazy loaded for startup performance.
@@ -101,6 +91,7 @@ const kBindStreamsEager = Symbol('kBindStreamsEager');
101
91
const kBindStreamsLazy = Symbol ( 'kBindStreamsLazy' ) ;
102
92
const kUseStdout = Symbol ( 'kUseStdout' ) ;
103
93
const kUseStderr = Symbol ( 'kUseStderr' ) ;
94
+ const kInternalTimeLogImpl = Symbol ( 'kInternalTimeLogImpl' ) ;
104
95
105
96
const optionsMap = new SafeWeakMap ( ) ;
106
97
function Console ( options /* or: stdout, stderr, ignoreErrors = true */ ) {
@@ -381,6 +372,14 @@ function createWriteErrorHandler(instance, streamSymbol) {
381
372
} ;
382
373
}
383
374
375
+ function timeLogImpl ( label , formatted , args ) {
376
+ if ( args === undefined ) {
377
+ this . log ( '%s: %s' , label , formatted ) ;
378
+ } else {
379
+ this . log ( '%s: %s' , label , formatted , ...new SafeArrayIterator ( args ) ) ;
380
+ }
381
+ }
382
+
384
383
const consoleMethods = {
385
384
log ( ...args ) {
386
385
this [ kWriteToConsole ] ( kUseStdout , this [ kFormatForStdout ] ( args ) ) ;
@@ -404,31 +403,21 @@ const consoleMethods = {
404
403
} ,
405
404
406
405
time ( label = 'default' ) {
407
- // Coerces everything other than Symbol to a string
408
- label = `${ label } ` ;
409
- if ( this . _times . has ( label ) ) {
410
- process . emitWarning ( `Label '${ label } ' already exists for console.time()` ) ;
411
- return ;
412
- }
413
- trace ( kTraceBegin , kTraceConsoleCategory , `time::${ label } ` , 0 ) ;
414
- this . _times . set ( label , process . hrtime ( ) ) ;
406
+ time ( this . _times , kTraceConsoleCategory , 'console.time()' , kNone , label , `time::${ label } ` ) ;
415
407
} ,
416
408
417
409
timeEnd ( label = 'default' ) {
418
- // Coerces everything other than Symbol to a string
419
- label = `${ label } ` ;
420
- const found = timeLogImpl ( this , 'timeEnd' , label ) ;
421
- trace ( kTraceEnd , kTraceConsoleCategory , `time::${ label } ` , 0 ) ;
422
- if ( found ) {
423
- this . _times . delete ( label ) ;
424
- }
410
+ if ( this [ kInternalTimeLogImpl ] === undefined )
411
+ this [ kInternalTimeLogImpl ] = FunctionPrototypeBind ( timeLogImpl , this ) ;
412
+
413
+ timeEnd ( this . _times , kTraceConsoleCategory , 'console.timeEnd()' , kNone , this [ kInternalTimeLogImpl ] , label , `time::${ label } ` ) ;
425
414
} ,
426
415
427
416
timeLog ( label = 'default' , ...data ) {
428
- // Coerces everything other than Symbol to a string
429
- label = ` ${ label } ` ;
430
- timeLogImpl ( this , 'timeLog' , label , data ) ;
431
- trace ( kTraceInstant , kTraceConsoleCategory , `time::${ label } ` , 0 ) ;
417
+ if ( this [ kInternalTimeLogImpl ] === undefined )
418
+ this [ kInternalTimeLogImpl ] = FunctionPrototypeBind ( timeLogImpl , this ) ;
419
+
420
+ timeLog ( this . _times , kTraceConsoleCategory , 'console.timeLog()' , kNone , this [ kInternalTimeLogImpl ] , label , `time::${ label } ` , data ) ;
432
421
} ,
433
422
434
423
trace : function trace ( ...args ) {
@@ -627,63 +616,6 @@ const consoleMethods = {
627
616
} ,
628
617
} ;
629
618
630
- // Returns true if label was found
631
- function timeLogImpl ( self , name , label , data ) {
632
- const time = self . _times . get ( label ) ;
633
- if ( time === undefined ) {
634
- process . emitWarning ( `No such label '${ label } ' for console.${ name } ()` ) ;
635
- return false ;
636
- }
637
- const duration = process . hrtime ( time ) ;
638
- const ms = duration [ 0 ] * 1000 + duration [ 1 ] / 1e6 ;
639
-
640
- const formatted = formatTime ( ms ) ;
641
-
642
- if ( data === undefined ) {
643
- self . log ( '%s: %s' , label , formatted ) ;
644
- } else {
645
- self . log ( '%s: %s' , label , formatted , ...new SafeArrayIterator ( data ) ) ;
646
- }
647
- return true ;
648
- }
649
-
650
- function pad ( value ) {
651
- return StringPrototypePadStart ( `${ value } ` , 2 , '0' ) ;
652
- }
653
-
654
- function formatTime ( ms ) {
655
- let hours = 0 ;
656
- let minutes = 0 ;
657
- let seconds = 0 ;
658
-
659
- if ( ms >= kSecond ) {
660
- if ( ms >= kMinute ) {
661
- if ( ms >= kHour ) {
662
- hours = MathFloor ( ms / kHour ) ;
663
- ms = ms % kHour ;
664
- }
665
- minutes = MathFloor ( ms / kMinute ) ;
666
- ms = ms % kMinute ;
667
- }
668
- seconds = ms / kSecond ;
669
- }
670
-
671
- if ( hours !== 0 || minutes !== 0 ) {
672
- ( { 0 : seconds , 1 : ms } = StringPrototypeSplit (
673
- NumberPrototypeToFixed ( seconds , 3 ) ,
674
- '.' ,
675
- ) ) ;
676
- const res = hours !== 0 ? `${ hours } :${ pad ( minutes ) } ` : minutes ;
677
- return `${ res } :${ pad ( seconds ) } .${ ms } (${ hours !== 0 ? 'h:m' : '' } m:ss.mmm)` ;
678
- }
679
-
680
- if ( seconds !== 0 ) {
681
- return `${ NumberPrototypeToFixed ( seconds , 3 ) } s` ;
682
- }
683
-
684
- return `${ Number ( NumberPrototypeToFixed ( ms , 3 ) ) } ms` ;
685
- }
686
-
687
619
const keyKey = 'Key' ;
688
620
const valuesKey = 'Values' ;
689
621
const indexKey = '(index)' ;
@@ -743,5 +675,4 @@ module.exports = {
743
675
kBindStreamsLazy,
744
676
kBindProperties,
745
677
initializeGlobalConsole,
746
- formatTime, // exported for tests
747
678
} ;
0 commit comments