当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Node.js trace_events.getEnabledCategories()用法及代码示例


trace_events.getEnabledCategories()(在 v10.0.0 中添加)方法是 ‘trace_events’ 模块的内置应用程序编程接口,用于显示由所有 currently-enabled 跟踪对象的并集确定的当前启用的跟踪事件类别集。默认情况下,它的状态被 v8 运行时禁用,所有启用的类别都使用 -trace-event-categories 标志获取。

用法:

trace_events.getEnabledCategories()

为了使用这个方法,我们需要使用 (trace_events.createTracing()) 方法创建跟踪事件,我们需要导入 ‘trace_events’ 模块。

const trace_events = require('trace_events');  

参数:此方法不接受任何参数。

返回值<string>:返回以逗号分隔的所有 currently-enabled 跟踪事件类别的列表。



范例1: 文件名:index.js


// Node.js program to demonstrate the 
// trace_events.getEnabledCategories() method 
  
// Using require to access trace_events module 
const trace_events = require('trace_events');
  
// Creating trace events
const trace_event1 = trace_events
    .createTracing({ categories:['node'] });
const trace_event2 = trace_events.createTracing({
    categories:['node.promises.rejections']
});
const { createTracing } = require('trace_events');
  
// Another way to create Tracing events
const trace_event3 = createTracing(
        { categories:['node.vm.script'] });
  
// Enabling the trace_event1
trace_event1.enable();
  
// Enabling the trace_event2
trace_event2.enable();
  
// Printing the enabled categories
console.log("Enabled categories:", 
    trace_events.getEnabledCategories());
  
// Disabling the trace_event1 
trace_event1.disable();
  
// Disabling the trace_event2 
trace_event2.disable();

输出:

Enabled categories node,node.promises.rejections

范例2: 文件名:index.js


// Node.js program to demonstrate the 
// trace_events.getEnabledCategories() method 
  
// Using require to access trace_events module 
const trace_events = require('trace_events');
  
// Tracing categories
const categories = ['myapp.category', 'v8', 'node', 
    'node.async_hooks', 'node.promises.rejections', 
    'node.vm.script', 'node.perf.usertiming',
    'node.perf.timerify'];
  
// Now create tracing for custom trace category.
const tracing = trace_events.createTracing({ categories });
  
// Enabling Tracing event
tracing.enable();
  
// Printing tracing event
console.log(">> ", tracing);
  
// Returns false if any tracing event is enabled 
console.log(">> ", !trace_events.getEnabledCategories());
  
if (trace_events.getEnabledCategories()) {
    console.log(">> Following tracing events are enabled",
        trace_events.getEnabledCategories());
} else {
    console.log(">> None of the Tracing events are enabled");
}
  
// Disabling tracing event
tracing.disable();
  
if (trace_events.getEnabledCategories()) {
    console.log(">> Following tracing events are enabled",
        trace_events.getEnabledCategories());
} else {
    console.log(">> None of the Tracing events are enabled");
}

使用以下命令运行index.js文件:

node index.js

输出:

>>  Tracing { enabled:true,

 categories:’myapp.category,v8,node,node.async_hooks,node.promises.rejections,node.vm.script,node.perf.usertiming,node.perf.timerify’}

>>  false

>> Following tracing events are enabled:myapp.category,node,node.async_hooks,node.perf.timerify,node.perf.usertiming,node.promises.rejections,node.vm.script,v8

>> None of the Tracing events are enabled

参考:https://nodejs.org/api/tracing.html#tracing_trace_events_getenabledcategories




相关用法


注:本文由纯净天空筛选整理自vikas_g大神的英文原创作品 Node.js trace_events.getEnabledCategories() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。