@@ -20,7 +20,7 @@ const {
2020 CHAR_LOWERCASE_N : kTraceInstant ,
2121} = require ( 'internal/constants' ) ;
2222const { inspect, format, formatWithOptions } = require ( 'internal/util/inspect' ) ;
23- const { isTraceCategoryEnabled , trace } = internalBinding ( 'trace_events' ) ;
23+ const { getCategoryEnabledBuffer , trace } = internalBinding ( 'trace_events' ) ;
2424
2525// `debugImpls` and `testEnabled` are deliberately not initialized so any call
2626// to `debuglog()` before `initializeDebugEnv()` is called will throw.
@@ -372,18 +372,34 @@ function debugWithTimer(set, cb) {
372372 ) ;
373373 }
374374
375- const kTraceCategory = `node,node.${ StringPrototypeToLowerCase ( set ) } ` ;
375+ const traceCategory = `node,node.${ StringPrototypeToLowerCase ( set ) } ` ;
376+ let traceCategoryBuffer ;
376377 let debugLogCategoryEnabled = false ;
377- let traceCategoryEnabled = false ;
378378 let timerFlags = kNone ;
379379
380+ const skipAll = kSkipLog | kSkipTrace ;
381+
382+ function ensureTimerFlagsAreUpdated ( ) {
383+ timerFlags &= ~ kSkipTrace ;
384+
385+ if ( traceCategoryBuffer [ 0 ] === 0 ) {
386+ timerFlags |= kSkipTrace ;
387+ }
388+ }
389+
380390 /**
381391 * @type {TimerStart }
382392 */
383393 function internalStartTimer ( logLabel , traceLabel ) {
394+ ensureTimerFlagsAreUpdated ( ) ;
395+
396+ if ( timerFlags === skipAll ) {
397+ return ;
398+ }
399+
384400 time (
385401 tracesStores [ set ] ,
386- kTraceCategory ,
402+ traceCategory ,
387403 'debuglog.time' ,
388404 timerFlags ,
389405 logLabel ,
@@ -395,9 +411,15 @@ function debugWithTimer(set, cb) {
395411 * @type {TimerEnd }
396412 */
397413 function internalEndTimer ( logLabel , traceLabel ) {
414+ ensureTimerFlagsAreUpdated ( ) ;
415+
416+ if ( timerFlags === skipAll ) {
417+ return ;
418+ }
419+
398420 timeEnd (
399421 tracesStores [ set ] ,
400- kTraceCategory ,
422+ traceCategory ,
401423 'debuglog.timeEnd' ,
402424 timerFlags ,
403425 logImpl ,
@@ -410,9 +432,15 @@ function debugWithTimer(set, cb) {
410432 * @type {TimerLog }
411433 */
412434 function internalLogTimer ( logLabel , traceLabel , args ) {
435+ ensureTimerFlagsAreUpdated ( ) ;
436+
437+ if ( timerFlags === skipAll ) {
438+ return ;
439+ }
440+
413441 timeLog (
414442 tracesStores [ set ] ,
415- kTraceCategory ,
443+ traceCategory ,
416444 'debuglog.timeLog' ,
417445 timerFlags ,
418446 logImpl ,
@@ -428,21 +456,19 @@ function debugWithTimer(set, cb) {
428456 }
429457 emitWarningIfNeeded ( set ) ;
430458 debugLogCategoryEnabled = testEnabled ( set ) ;
431- traceCategoryEnabled = isTraceCategoryEnabled ( kTraceCategory ) ;
459+ traceCategoryBuffer = getCategoryEnabledBuffer ( traceCategory ) ;
460+
461+ timerFlags = kNone ;
432462
433463 if ( ! debugLogCategoryEnabled ) {
434464 timerFlags |= kSkipLog ;
435465 }
436466
437- if ( ! traceCategoryEnabled ) {
467+ if ( traceCategoryBuffer [ 0 ] === 0 ) {
438468 timerFlags |= kSkipTrace ;
439469 }
440470
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 ) ;
446472 }
447473
448474 /**
@@ -451,7 +477,7 @@ function debugWithTimer(set, cb) {
451477 const startTimer = ( logLabel , traceLabel ) => {
452478 init ( ) ;
453479
454- if ( debugLogCategoryEnabled || traceCategoryEnabled )
480+ if ( timerFlags !== skipAll )
455481 internalStartTimer ( logLabel , traceLabel ) ;
456482 } ;
457483
@@ -461,7 +487,7 @@ function debugWithTimer(set, cb) {
461487 const endTimer = ( logLabel , traceLabel ) => {
462488 init ( ) ;
463489
464- if ( debugLogCategoryEnabled || traceCategoryEnabled )
490+ if ( timerFlags !== skipAll )
465491 internalEndTimer ( logLabel , traceLabel ) ;
466492 } ;
467493
@@ -471,7 +497,7 @@ function debugWithTimer(set, cb) {
471497 const logTimer = ( logLabel , traceLabel , args ) => {
472498 init ( ) ;
473499
474- if ( debugLogCategoryEnabled || traceCategoryEnabled )
500+ if ( timerFlags !== skipAll )
475501 internalLogTimer ( logLabel , traceLabel , args ) ;
476502 } ;
477503
0 commit comments