2018-04-04 03:05:33 +02:00
|
|
|
'use strict';
|
|
|
|
|
2019-11-23 10:09:05 +01:00
|
|
|
const {
|
|
|
|
ArrayIsArray,
|
2020-01-02 14:27:54 +01:00
|
|
|
Set,
|
2019-11-30 16:55:29 +01:00
|
|
|
Symbol,
|
2019-11-23 10:09:05 +01:00
|
|
|
} = primordials;
|
|
|
|
|
2018-10-09 01:10:07 +02:00
|
|
|
const { hasTracing } = internalBinding('config');
|
2018-04-04 03:05:33 +02: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 23:47:38 +01:00
|
|
|
const { ownsProcessState } = require('internal/worker');
|
|
|
|
if (!hasTracing || !ownsProcessState)
|
2018-04-04 03:05:33 +02:00
|
|
|
throw new ERR_TRACE_EVENTS_UNAVAILABLE();
|
|
|
|
|
2018-08-06 21:28:33 +02:00
|
|
|
const { CategorySet, getEnabledCategories } = internalBinding('trace_events');
|
2018-04-04 03:05:33 +02:00
|
|
|
const { customInspectSymbol } = require('internal/util');
|
2019-03-20 16:06:14 +01:00
|
|
|
const { format } = require('internal/util/inspect');
|
2018-04-04 03:05:33 +02: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 04:33:23 +02:00
|
|
|
if (typeof depth === 'number' && depth < 0)
|
|
|
|
return this;
|
|
|
|
|
2018-04-04 03:05:33 +02:00
|
|
|
const obj = {
|
|
|
|
enabled: this.enabled,
|
|
|
|
categories: this.categories
|
|
|
|
};
|
|
|
|
return `Tracing ${format(obj)}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function createTracing(options) {
|
2019-06-11 06:19:44 +02:00
|
|
|
if (typeof options !== 'object' || options === null)
|
2018-04-04 03:05:33 +02:00
|
|
|
throw new ERR_INVALID_ARG_TYPE('options', 'object', options);
|
|
|
|
|
2019-11-23 10:09:05 +01:00
|
|
|
if (!ArrayIsArray(options.categories)) {
|
2018-04-04 03:05:33 +02:00
|
|
|
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
|
|
|
|
};
|