2018-04-04 01:05:33 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-10-08 23:10:07 +00:00
|
|
|
const { hasTracing } = internalBinding('config');
|
2018-04-04 01:05:33 +00:00
|
|
|
const kHandle = Symbol('handle');
|
|
|
|
const kEnabled = Symbol('enabled');
|
|
|
|
const kCategories = Symbol('categories');
|
|
|
|
|
|
|
|
const kMaxTracingCount = 10;
|
|
|
|
|
|
|
|
const {
|
|
|
|
ERR_TRACE_EVENTS_CATEGORY_REQUIRED,
|
|
|
|
ERR_TRACE_EVENTS_UNAVAILABLE,
|
|
|
|
ERR_INVALID_ARG_TYPE
|
|
|
|
} = require('internal/errors').codes;
|
|
|
|
|
2019-02-01 22:47:38 +00:00
|
|
|
const { ownsProcessState } = require('internal/worker');
|
|
|
|
if (!hasTracing || !ownsProcessState)
|
2018-04-04 01:05:33 +00:00
|
|
|
throw new ERR_TRACE_EVENTS_UNAVAILABLE();
|
|
|
|
|
2018-08-06 19:28:33 +00:00
|
|
|
const { CategorySet, getEnabledCategories } = internalBinding('trace_events');
|
2018-04-04 01:05:33 +00:00
|
|
|
const { customInspectSymbol } = require('internal/util');
|
2019-03-20 15:06:14 +00:00
|
|
|
const { format } = require('internal/util/inspect');
|
2018-04-04 01:05:33 +00:00
|
|
|
|
|
|
|
const enabledTracingObjects = new Set();
|
|
|
|
|
|
|
|
class Tracing {
|
|
|
|
constructor(categories) {
|
|
|
|
this[kHandle] = new CategorySet(categories);
|
|
|
|
this[kCategories] = categories;
|
|
|
|
this[kEnabled] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
enable() {
|
|
|
|
if (!this[kEnabled]) {
|
|
|
|
this[kEnabled] = true;
|
|
|
|
this[kHandle].enable();
|
|
|
|
enabledTracingObjects.add(this);
|
|
|
|
if (enabledTracingObjects.size > kMaxTracingCount) {
|
|
|
|
process.emitWarning(
|
|
|
|
'Possible trace_events memory leak detected. There are more than ' +
|
|
|
|
`${kMaxTracingCount} enabled Tracing objects.`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
disable() {
|
|
|
|
if (this[kEnabled]) {
|
|
|
|
this[kEnabled] = false;
|
|
|
|
this[kHandle].disable();
|
|
|
|
enabledTracingObjects.delete(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get enabled() {
|
|
|
|
return this[kEnabled];
|
|
|
|
}
|
|
|
|
|
|
|
|
get categories() {
|
|
|
|
return this[kCategories].join(',');
|
|
|
|
}
|
|
|
|
|
|
|
|
[customInspectSymbol](depth, opts) {
|
2019-06-03 02:33:23 +00:00
|
|
|
if (typeof depth === 'number' && depth < 0)
|
|
|
|
return this;
|
|
|
|
|
2018-04-04 01:05:33 +00:00
|
|
|
const obj = {
|
|
|
|
enabled: this.enabled,
|
|
|
|
categories: this.categories
|
|
|
|
};
|
|
|
|
return `Tracing ${format(obj)}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function createTracing(options) {
|
2019-06-11 04:19:44 +00:00
|
|
|
if (typeof options !== 'object' || options === null)
|
2018-04-04 01:05:33 +00:00
|
|
|
throw new ERR_INVALID_ARG_TYPE('options', 'object', options);
|
|
|
|
|
|
|
|
if (!Array.isArray(options.categories)) {
|
|
|
|
throw new ERR_INVALID_ARG_TYPE('options.categories', 'string[]',
|
|
|
|
options.categories);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.categories.length <= 0)
|
|
|
|
throw new ERR_TRACE_EVENTS_CATEGORY_REQUIRED();
|
|
|
|
|
|
|
|
return new Tracing(options.categories);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
createTracing,
|
|
|
|
getEnabledCategories
|
|
|
|
};
|