@@ -20,7 +20,7 @@ const {
20
20
CHAR_LOWERCASE_N : kTraceInstant ,
21
21
} = require ( 'internal/constants' ) ;
22
22
const { inspect, format, formatWithOptions } = require ( 'internal/util/inspect' ) ;
23
- const { isTraceCategoryEnabled , trace } = internalBinding ( 'trace_events' ) ;
23
+ const { getCategoryEnabledBuffer , trace } = internalBinding ( 'trace_events' ) ;
24
24
25
25
// `debugImpls` and `testEnabled` are deliberately not initialized so any call
26
26
// to `debuglog()` before `initializeDebugEnv()` is called will throw.
@@ -372,18 +372,34 @@ function debugWithTimer(set, cb) {
372
372
) ;
373
373
}
374
374
375
- const kTraceCategory = `node,node.${ StringPrototypeToLowerCase ( set ) } ` ;
375
+ const traceCategory = `node,node.${ StringPrototypeToLowerCase ( set ) } ` ;
376
+ let traceCategoryBuffer ;
376
377
let debugLogCategoryEnabled = false ;
377
- let traceCategoryEnabled = false ;
378
378
let timerFlags = kNone ;
379
379
380
+ const skipAll = kSkipLog | kSkipTrace ;
381
+
382
+ function ensureTimerFlagsAreUpdated ( ) {
383
+ timerFlags &= ~ kSkipTrace ;
384
+
385
+ if ( traceCategoryBuffer [ 0 ] === 0 ) {
386
+ timerFlags |= kSkipTrace ;
387
+ }
388
+ }
389
+
380
390
/**
381
391
* @type {TimerStart }
382
392
*/
383
393
function internalStartTimer ( logLabel , traceLabel ) {
394
+ ensureTimerFlagsAreUpdated ( ) ;
395
+
396
+ if ( timerFlags === skipAll ) {
397
+ return ;
398
+ }
399
+
384
400
time (
385
401
tracesStores [ set ] ,
386
- kTraceCategory ,
402
+ traceCategory ,
387
403
'debuglog.time' ,
388
404
timerFlags ,
389
405
logLabel ,
@@ -395,9 +411,15 @@ function debugWithTimer(set, cb) {
395
411
* @type {TimerEnd }
396
412
*/
397
413
function internalEndTimer ( logLabel , traceLabel ) {
414
+ ensureTimerFlagsAreUpdated ( ) ;
415
+
416
+ if ( timerFlags === skipAll ) {
417
+ return ;
418
+ }
419
+
398
420
timeEnd (
399
421
tracesStores [ set ] ,
400
- kTraceCategory ,
422
+ traceCategory ,
401
423
'debuglog.timeEnd' ,
402
424
timerFlags ,
403
425
logImpl ,
@@ -410,9 +432,15 @@ function debugWithTimer(set, cb) {
410
432
* @type {TimerLog }
411
433
*/
412
434
function internalLogTimer ( logLabel , traceLabel , args ) {
435
+ ensureTimerFlagsAreUpdated ( ) ;
436
+
437
+ if ( timerFlags === skipAll ) {
438
+ return ;
439
+ }
440
+
413
441
timeLog (
414
442
tracesStores [ set ] ,
415
- kTraceCategory ,
443
+ traceCategory ,
416
444
'debuglog.timeLog' ,
417
445
timerFlags ,
418
446
logImpl ,
@@ -428,21 +456,19 @@ function debugWithTimer(set, cb) {
428
456
}
429
457
emitWarningIfNeeded ( set ) ;
430
458
debugLogCategoryEnabled = testEnabled ( set ) ;
431
- traceCategoryEnabled = isTraceCategoryEnabled ( kTraceCategory ) ;
459
+ traceCategoryBuffer = getCategoryEnabledBuffer ( traceCategory ) ;
460
+
461
+ timerFlags = kNone ;
432
462
433
463
if ( ! debugLogCategoryEnabled ) {
434
464
timerFlags |= kSkipLog ;
435
465
}
436
466
437
- if ( ! traceCategoryEnabled ) {
467
+ if ( traceCategoryBuffer [ 0 ] === 0 ) {
438
468
timerFlags |= kSkipTrace ;
439
469
}
440
470
441
- // TODO(H4ad): support traceCategory being enabled dynamically
442
- if ( debugLogCategoryEnabled || traceCategoryEnabled )
443
- cb ( internalStartTimer , internalEndTimer , internalLogTimer ) ;
444
- else
445
- cb ( noop , noop , noop ) ;
471
+ cb ( internalStartTimer , internalEndTimer , internalLogTimer ) ;
446
472
}
447
473
448
474
/**
@@ -451,7 +477,7 @@ function debugWithTimer(set, cb) {
451
477
const startTimer = ( logLabel , traceLabel ) => {
452
478
init ( ) ;
453
479
454
- if ( debugLogCategoryEnabled || traceCategoryEnabled )
480
+ if ( timerFlags !== skipAll )
455
481
internalStartTimer ( logLabel , traceLabel ) ;
456
482
} ;
457
483
@@ -461,7 +487,7 @@ function debugWithTimer(set, cb) {
461
487
const endTimer = ( logLabel , traceLabel ) => {
462
488
init ( ) ;
463
489
464
- if ( debugLogCategoryEnabled || traceCategoryEnabled )
490
+ if ( timerFlags !== skipAll )
465
491
internalEndTimer ( logLabel , traceLabel ) ;
466
492
} ;
467
493
@@ -471,7 +497,7 @@ function debugWithTimer(set, cb) {
471
497
const logTimer = ( logLabel , traceLabel , args ) => {
472
498
init ( ) ;
473
499
474
- if ( debugLogCategoryEnabled || traceCategoryEnabled )
500
+ if ( timerFlags !== skipAll )
475
501
internalLogTimer ( logLabel , traceLabel , args ) ;
476
502
} ;
477
503
0 commit comments