提交 8308dbcf authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

Avoid dynamic Logger instances

上级 7de1577f
import { EnhancedEventEmitter } from './EnhancedEventEmitter';
export declare class Channel extends EnhancedEventEmitter {
private readonly _workerLogger;
private _closed;
private readonly _producerSocket;
private readonly _consumerSocket;
......
{"version":3,"file":"Channel.d.ts","sourceRoot":"","sources":["../src/Channel.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAiB9D,qBAAa,OAAQ,SAAQ,oBAAoB;IAGhD,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAS;IAGvC,OAAO,CAAC,OAAO,CAAS;IAGxB,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAS;IAGzC,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAS;IAGzC,OAAO,CAAC,OAAO,CAAK;IAGpB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAgC;IAGvD,OAAO,CAAC,WAAW,CAAC,CAAS;IAE7B;;OAEG;gBAEF,EACC,cAAc,EACd,cAAc,EACd,GAAG,EACH,EACD;QACC,cAAc,EAAE,GAAG,CAAC;QACpB,cAAc,EAAE,GAAG,CAAC;QACpB,GAAG,EAAE,MAAM,CAAC;KACZ;IAwHF;;OAEG;IACH,KAAK,IAAI,IAAI;IAsCb;;OAEG;IACG,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC;IA8D1E,OAAO,CAAC,eAAe;CAyDvB"}
\ No newline at end of file
{"version":3,"file":"Channel.d.ts","sourceRoot":"","sources":["../src/Channel.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAmB9D,qBAAa,OAAQ,SAAQ,oBAAoB;IAGhD,OAAO,CAAC,OAAO,CAAS;IAGxB,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAS;IAGzC,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAS;IAGzC,OAAO,CAAC,OAAO,CAAK;IAGpB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAgC;IAGvD,OAAO,CAAC,WAAW,CAAC,CAAS;IAE7B;;OAEG;gBAEF,EACC,cAAc,EACd,cAAc,EACd,GAAG,EACH,EACD;QACC,cAAc,EAAE,GAAG,CAAC;QACpB,cAAc,EAAE,GAAG,CAAC;QACpB,GAAG,EAAE,MAAM,CAAC;KACZ;IAuHF;;OAEG;IACH,KAAK,IAAI,IAAI;IAmCb;;OAEG;IACG,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC;IA8D1E,OAAO,CAAC,eAAe;CAyDvB"}
\ No newline at end of file
......@@ -21,6 +21,7 @@ const netstring = __importStar(require("netstring"));
const Logger_1 = require("./Logger");
const EnhancedEventEmitter_1 = require("./EnhancedEventEmitter");
const errors_1 = require("./errors");
const logger = new Logger_1.Logger('Channel');
// netstring length for a 4194304 bytes payload.
const NS_MESSAGE_MAX_LEN = 4194313;
const NS_PAYLOAD_MAX_LEN = 4194304;
......@@ -29,15 +30,14 @@ class Channel extends EnhancedEventEmitter_1.EnhancedEventEmitter {
* @private
*/
constructor({ producerSocket, consumerSocket, pid }) {
super(new Logger_1.Logger(`Channel[pid:${pid}]`));
super();
// Closed flag.
this._closed = false;
// Next id for messages sent to the worker process.
this._nextId = 0;
// Map of pending sent requests.
this._sents = new Map();
this._logger.debug('constructor()');
this._workerLogger = new Logger_1.Logger(`worker[pid:${pid}]`);
logger.debug('constructor()');
this._producerSocket = producerSocket;
this._consumerSocket = consumerSocket;
// Read Channel responses/notifications from the worker.
......@@ -49,7 +49,7 @@ class Channel extends EnhancedEventEmitter_1.EnhancedEventEmitter {
this._recvBuffer = Buffer.concat([this._recvBuffer, buffer], this._recvBuffer.length + buffer.length);
}
if (this._recvBuffer.length > NS_PAYLOAD_MAX_LEN) {
this._logger.error('receiving buffer is full, discarding all data into it');
logger.error('receiving buffer is full, discarding all data into it');
// Reset the buffer and exit.
this._recvBuffer = null;
return;
......@@ -61,7 +61,7 @@ class Channel extends EnhancedEventEmitter_1.EnhancedEventEmitter {
nsPayload = netstring.nsPayload(this._recvBuffer);
}
catch (error) {
this._logger.error('invalid netstring data received from the worker process: %s', String(error));
logger.error('invalid netstring data received from the worker process: %s', String(error));
// Reset the buffer and exit.
this._recvBuffer = undefined;
return;
......@@ -78,15 +78,15 @@ class Channel extends EnhancedEventEmitter_1.EnhancedEventEmitter {
break;
// 68 = 'D' (a debug log).
case 68:
this._workerLogger.debug(nsPayload.toString('utf8', 1));
logger.debug(`[pid:${pid}] ${nsPayload.toString('utf8', 1)}`);
break;
// 87 = 'W' (a warn log).
case 87:
this._workerLogger.warn(nsPayload.toString('utf8', 1));
logger.warn(`[pid:${pid}] ${nsPayload.toString('utf8', 1)}`);
break;
// 69 = 'E' (an error log).
case 69:
this._workerLogger.error(nsPayload.toString('utf8', 1));
logger.error(`[pid:${pid} ${nsPayload.toString('utf8', 1)}`);
break;
// 88 = 'X' (a dump log).
case 88:
......@@ -99,7 +99,7 @@ class Channel extends EnhancedEventEmitter_1.EnhancedEventEmitter {
}
}
catch (error) {
this._logger.error('received invalid message from the worker process: %s', String(error));
logger.error('received invalid message from the worker process: %s', String(error));
}
// Remove the read payload from the buffer.
this._recvBuffer =
......@@ -110,10 +110,10 @@ class Channel extends EnhancedEventEmitter_1.EnhancedEventEmitter {
}
}
});
this._consumerSocket.on('end', () => this._logger.debug('Consumer Channel ended by the worker process'));
this._consumerSocket.on('error', (error) => this._logger.error('Consumer Channel error: %s', String(error)));
this._producerSocket.on('end', () => this._logger.debug('Producer Channel ended by the worker process'));
this._producerSocket.on('error', (error) => this._logger.error('Producer Channel error: %s', String(error)));
this._consumerSocket.on('end', () => logger.debug('Consumer Channel ended by the worker process'));
this._consumerSocket.on('error', (error) => logger.error('Consumer Channel error: %s', String(error)));
this._producerSocket.on('end', () => logger.debug('Producer Channel ended by the worker process'));
this._producerSocket.on('error', (error) => logger.error('Producer Channel error: %s', String(error)));
}
/**
* @private
......@@ -121,14 +121,12 @@ class Channel extends EnhancedEventEmitter_1.EnhancedEventEmitter {
close() {
if (this._closed)
return;
this._logger.debug('close()');
logger.debug('close()');
this._closed = true;
// Close every pending sent.
for (const sent of this._sents.values()) {
sent.close();
}
// Destroy the logger so it does not leak debug instances.
this._logger.destroy();
// Remove event listeners but leave a fake 'error' hander to avoid
// propagation.
this._consumerSocket.removeAllListeners('end');
......@@ -156,7 +154,7 @@ class Channel extends EnhancedEventEmitter_1.EnhancedEventEmitter {
return __awaiter(this, void 0, void 0, function* () {
this._nextId < 4294967295 ? ++this._nextId : (this._nextId = 1);
const id = this._nextId;
this._logger.debug('request() [method:%s, id:%s]', method, id);
logger.debug('request() [method:%s, id:%s]', method, id);
if (this._closed)
throw new errors_1.InvalidStateError('Channel closed');
const request = { id, method, internal, data };
......@@ -202,15 +200,15 @@ class Channel extends EnhancedEventEmitter_1.EnhancedEventEmitter {
if (msg.id) {
const sent = this._sents.get(msg.id);
if (!sent) {
this._logger.error('received response does not match any sent request [id:%s]', msg.id);
logger.error('received response does not match any sent request [id:%s]', msg.id);
return;
}
if (msg.accepted) {
this._logger.debug('request succeeded [method:%s, id:%s]', sent.method, sent.id);
logger.debug('request succeeded [method:%s, id:%s]', sent.method, sent.id);
sent.resolve(msg.data);
}
else if (msg.error) {
this._logger.warn('request failed [method:%s, id:%s]: %s', sent.method, sent.id, msg.reason);
logger.warn('request failed [method:%s, id:%s]: %s', sent.method, sent.id, msg.reason);
switch (msg.error) {
case 'TypeError':
sent.reject(new TypeError(msg.reason));
......@@ -220,7 +218,7 @@ class Channel extends EnhancedEventEmitter_1.EnhancedEventEmitter {
}
}
else {
this._logger.error('received response is not accepted nor rejected [method:%s, id:%s]', sent.method, sent.id);
logger.error('received response is not accepted nor rejected [method:%s, id:%s]', sent.method, sent.id);
}
}
// If a notification emit it to the corresponding entity.
......@@ -229,7 +227,7 @@ class Channel extends EnhancedEventEmitter_1.EnhancedEventEmitter {
}
// Otherwise unexpected message.
else {
this._logger.error('received message is not a response nor a notification');
logger.error('received message is not a response nor a notification');
}
}
}
......
......@@ -26,7 +26,7 @@ class Consumer extends EnhancedEventEmitter_1.EnhancedEventEmitter {
* @emits @producerclose
*/
constructor({ internal, data, channel, appData, paused, producerPaused, score = { score: 10, producerScore: 10 }, preferredLayers }) {
super(logger);
super();
// Closed flag.
this._closed = false;
// Paused flag.
......
......@@ -21,7 +21,7 @@ class DataConsumer extends EnhancedEventEmitter_1.EnhancedEventEmitter {
* @emits @dataproducerclose
*/
constructor({ internal, data, channel, appData }) {
super(logger);
super();
// Closed flag.
this._closed = false;
// Observer instance.
......
......@@ -19,7 +19,7 @@ class DataProducer extends EnhancedEventEmitter_1.EnhancedEventEmitter {
* @emits @close
*/
constructor({ internal, data, channel, appData }) {
super(logger);
super();
// Closed flag.
this._closed = false;
// Observer instance.
......
/// <reference types="node" />
import { EventEmitter } from 'events';
import { Logger } from './Logger';
export declare class EnhancedEventEmitter extends EventEmitter {
protected readonly _logger: Logger;
constructor(logger?: Logger);
constructor();
safeEmit(event: string, ...args: any[]): boolean;
safeEmitAsPromise(event: string, ...args: any[]): Promise<any>;
}
......
{"version":3,"file":"EnhancedEventEmitter.d.ts","sourceRoot":"","sources":["../src/EnhancedEventEmitter.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAIlC,qBAAa,oBAAqB,SAAQ,YAAY;IAGrD,SAAS,CAAC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;gBAEvB,MAAM,CAAC,EAAE,MAAM;IAQ3B,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,OAAO;IAkB1C,iBAAiB,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC,GAAG,CAAC;CAMpE"}
\ No newline at end of file
{"version":3,"file":"EnhancedEventEmitter.d.ts","sourceRoot":"","sources":["../src/EnhancedEventEmitter.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AAKtC,qBAAa,oBAAqB,SAAQ,YAAY;;IAQrD,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,OAAO;IAkB1C,iBAAiB,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC,GAAG,CAAC;CAMpE"}
\ No newline at end of file
......@@ -11,12 +11,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
Object.defineProperty(exports, "__esModule", { value: true });
const events_1 = require("events");
const Logger_1 = require("./Logger");
const staticLogger = new Logger_1.Logger('EnhancedEventEmitter');
const logger = new Logger_1.Logger('EnhancedEventEmitter');
class EnhancedEventEmitter extends events_1.EventEmitter {
constructor(logger) {
constructor() {
super();
this.setMaxListeners(Infinity);
this._logger = logger || staticLogger;
}
safeEmit(event, ...args) {
const numListeners = this.listenerCount(event);
......@@ -24,7 +23,7 @@ class EnhancedEventEmitter extends events_1.EventEmitter {
return this.emit(event, ...args);
}
catch (error) {
this._logger.error('safeEmit() | event listener threw an error [event:%s]:%o', event, error);
logger.error('safeEmit() | event listener threw an error [event:%s]:%o', event, error);
return Boolean(numListeners);
}
}
......
......@@ -7,6 +7,5 @@ export declare class Logger {
get debug(): debug.Debugger;
get warn(): debug.Debugger;
get error(): debug.Debugger;
destroy(): void;
}
//# sourceMappingURL=Logger.d.ts.map
\ No newline at end of file
{"version":3,"file":"Logger.d.ts","sourceRoot":"","sources":["../src/Logger.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAI1B,qBAAa,MAAM;IAElB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAiB;IACxC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAiB;IACvC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAiB;gBAE5B,MAAM,CAAC,EAAE,MAAM;IAsB3B,IAAI,KAAK,IAAI,KAAK,CAAC,QAAQ,CAG1B;IAED,IAAI,IAAI,IAAI,KAAK,CAAC,QAAQ,CAGzB;IAED,IAAI,KAAK,IAAI,KAAK,CAAC,QAAQ,CAG1B;IAED,OAAO,IAAI,IAAI;CAMf"}
\ No newline at end of file
{"version":3,"file":"Logger.d.ts","sourceRoot":"","sources":["../src/Logger.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAI1B,qBAAa,MAAM;IAElB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAiB;IACxC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAiB;IACvC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAiB;gBAE5B,MAAM,CAAC,EAAE,MAAM;IAsB3B,IAAI,KAAK,IAAI,KAAK,CAAC,QAAQ,CAG1B;IAED,IAAI,IAAI,IAAI,KAAK,CAAC,QAAQ,CAGzB;IAED,IAAI,KAAK,IAAI,KAAK,CAAC,QAAQ,CAG1B;CACD"}
\ No newline at end of file
......@@ -32,10 +32,5 @@ class Logger {
get error() {
return this._error;
}
destroy() {
this._debug.destroy();
this._warn.destroy();
this._error.destroy();
}
}
exports.Logger = Logger;
......@@ -22,7 +22,7 @@ class Producer extends EnhancedEventEmitter_1.EnhancedEventEmitter {
* @emits @close
*/
constructor({ internal, data, channel, appData, paused }) {
super(logger);
super();
// Closed flag.
this._closed = false;
// Paused flag.
......
......@@ -35,7 +35,7 @@ class Router extends EnhancedEventEmitter_1.EnhancedEventEmitter {
* @emits @close
*/
constructor({ internal, data, channel, appData }) {
super(logger);
super();
// Closed flag.
this._closed = false;
// Transports map.
......
......@@ -20,7 +20,7 @@ class RtpObserver extends EnhancedEventEmitter_1.EnhancedEventEmitter {
* @emits @close
*/
constructor({ internal, channel, appData, getProducerById }) {
super(logger);
super();
// Closed flag.
this._closed = false;
// Paused flag.
......
......@@ -41,7 +41,7 @@ class Transport extends EnhancedEventEmitter_1.EnhancedEventEmitter {
* @emits @dataproducerclose - (dataProducer: DataProducer)
*/
constructor({ internal, data, channel, appData, getRouterRtpCapabilities, getProducerById, getDataProducerById }) {
super(logger);
super();
// Close flag.
this._closed = false;
// Producers map.
......
......@@ -111,7 +111,6 @@ export interface WorkerResourceUsage {
}
export declare class Worker extends EnhancedEventEmitter {
private _child?;
private readonly _workerLogger;
private readonly _pid;
private readonly _channel;
private _closed;
......
{"version":3,"file":"Worker.d.ts","sourceRoot":"","sources":["../src/Worker.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAG9D,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAEjD,oBAAY,cAAc,GAAG,OAAO,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,CAAC;AAEjE,MAAM,WAAW,cAAc;IAE9B;;;;OAIG;IACH,QAAQ,CAAC,EAAE,cAAc,CAAC;IAE1B;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IAEnB;;OAEG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB;;OAEG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB;;;OAGG;IACH,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAE7B;;;OAGG;IACH,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAE5B;;OAEG;IACH,OAAO,CAAC,EAAE,GAAG,CAAC;CACd;AAED,oBAAY,wBAAwB,GAAG,IAAI,CAAC,cAAc,EAAE,UAAU,GAAG,SAAS,CAAC,CAAC;AAE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AA8B;IAExD;;;;;OAKG;gBAEF,EACC,QAAQ,EACR,OAAO,EACP,UAAU,EACV,UAAU,EACV,mBAAmB,EACnB,kBAAkB,EAClB,OAAO,EACP,EAAE,cAAc;IA8KlB;;OAEG;IACH,IAAI,GAAG,IAAI,MAAM,CAGhB;IAED;;OAEG;IACH,IAAI,MAAM,IAAI,OAAO,CAGpB;IAED;;OAEG;IACH,IAAI,OAAO,IAAI,GAAG,CAGjB;IAED;;OAEG;IACH,IAAI,OAAO,CAAC,OAAO,EAAE,GAAG,EAGvB;IAED;;;;;OAKG;IACH,IAAI,QAAQ,IAAI,oBAAoB,CAGnC;IAED;;OAEG;IACH,KAAK,IAAI,IAAI;IAmCb;;OAEG;IACG,IAAI,IAAI,OAAO,CAAC,GAAG,CAAC;IAO1B;;OAEG;IACG,gBAAgB,IAAI,OAAO,CAAC,mBAAmB,CAAC;IAOtD;;OAEG;IACG,cAAc,CACnB,EACC,QAAQ,EACR,OAAO,EACP,GAAE,wBAA6B,GAC9B,OAAO,CAAC,IAAI,CAAC;IAShB;;OAEG;IACG,YAAY,CACjB,EACC,WAAW,EACX,OAAY,EACZ,GAAE,aAAkB,GAAG,OAAO,CAAC,MAAM,CAAC;CA+BxC"}
\ No newline at end of file
......@@ -37,6 +37,7 @@ const workerBin = process.env.MEDIASOUP_WORKER_BIN
? path.join(__dirname, '..', 'worker', 'out', 'Debug', 'mediasoup-worker')
: path.join(__dirname, '..', 'worker', 'out', 'Release', 'mediasoup-worker');
const logger = new Logger_1.Logger('Worker');
const workerLogger = new Logger_1.Logger('Worker');
class Worker extends EnhancedEventEmitter_1.EnhancedEventEmitter {
/**
* @private
......@@ -94,7 +95,6 @@ class Worker extends EnhancedEventEmitter_1.EnhancedEventEmitter {
// fd 4 (channel) : Consumer Channel fd.
stdio: ['ignore', 'pipe', 'pipe', 'pipe', 'pipe']
});
this._workerLogger = new Logger_1.Logger(`worker[pid:${this._child.pid}]`);
this._pid = this._child.pid;
this._channel = new Channel_1.Channel({
producerSocket: this._child.stdio[3],
......@@ -147,14 +147,14 @@ class Worker extends EnhancedEventEmitter_1.EnhancedEventEmitter {
this._child.stdout.on('data', (buffer) => {
for (const line of buffer.toString('utf8').split('\n')) {
if (line)
this._workerLogger.debug(`(stdout) ${line}`);
workerLogger.debug(`(stdout) ${line}`);
}
});
// In case of a worker bug, mediasoup will log to stderr.
this._child.stderr.on('data', (buffer) => {
for (const line of buffer.toString('utf8').split('\n')) {
if (line)
this._workerLogger.error(`(stderr) ${line}`);
workerLogger.error(`(stderr) ${line}`);
}
});
}
......
......@@ -62,7 +62,7 @@
"@typescript-eslint/parser": "^2.15.0",
"cross-env": "^6.0.3",
"eslint": "^6.8.0",
"eslint-plugin-jest": "^23.4.0",
"eslint-plugin-jest": "^23.6.0",
"gulp": "^4.0.2",
"gulp-clang-format": "^1.0.27",
"jest": "^24.9.0",
......
......@@ -5,6 +5,8 @@ import { Logger } from './Logger';
import { EnhancedEventEmitter } from './EnhancedEventEmitter';
import { InvalidStateError } from './errors';
const logger = new Logger('Channel');
interface Sent
{
id: number;
......@@ -21,9 +23,6 @@ const NS_PAYLOAD_MAX_LEN = 4194304;
export class Channel extends EnhancedEventEmitter
{
// Logger for logs from the worker process.
private readonly _workerLogger: Logger;
// Closed flag.
private _closed = false;
......@@ -57,11 +56,10 @@ export class Channel extends EnhancedEventEmitter
pid: number;
})
{
super(new Logger(`Channel[pid:${pid}]`));
super();
this._logger.debug('constructor()');
logger.debug('constructor()');
this._workerLogger = new Logger(`worker[pid:${pid}]`);
this._producerSocket = producerSocket as Duplex;
this._consumerSocket = consumerSocket as Duplex;
......@@ -81,7 +79,7 @@ export class Channel extends EnhancedEventEmitter
if (this._recvBuffer.length > NS_PAYLOAD_MAX_LEN)
{
this._logger.error('receiving buffer is full, discarding all data into it');
logger.error('receiving buffer is full, discarding all data into it');
// Reset the buffer and exit.
this._recvBuffer = null;
......@@ -99,7 +97,7 @@ export class Channel extends EnhancedEventEmitter
}
catch (error)
{
this._logger.error(
logger.error(
'invalid netstring data received from the worker process: %s', String(error));
// Reset the buffer and exit.
......@@ -124,17 +122,17 @@ export class Channel extends EnhancedEventEmitter
// 68 = 'D' (a debug log).
case 68:
this._workerLogger.debug(nsPayload.toString('utf8', 1));
logger.debug(`[pid:${pid}] ${nsPayload.toString('utf8', 1)}`);
break;
// 87 = 'W' (a warn log).
case 87:
this._workerLogger.warn(nsPayload.toString('utf8', 1));
logger.warn(`[pid:${pid}] ${nsPayload.toString('utf8', 1)}`);
break;
// 69 = 'E' (an error log).
case 69:
this._workerLogger.error(nsPayload.toString('utf8', 1));
logger.error(`[pid:${pid} ${nsPayload.toString('utf8', 1)}`);
break;
// 88 = 'X' (a dump log).
......@@ -151,7 +149,7 @@ export class Channel extends EnhancedEventEmitter
}
catch (error)
{
this._logger.error(
logger.error(
'received invalid message from the worker process: %s', String(error));
}
......@@ -168,11 +166,11 @@ export class Channel extends EnhancedEventEmitter
}
});
this._consumerSocket.on('end', () => this._logger.debug('Consumer Channel ended by the worker process'));
this._consumerSocket.on('error', (error) => this._logger.error('Consumer Channel error: %s', String(error)));
this._consumerSocket.on('end', () => logger.debug('Consumer Channel ended by the worker process'));
this._consumerSocket.on('error', (error) => logger.error('Consumer Channel error: %s', String(error)));
this._producerSocket.on('end', () => this._logger.debug('Producer Channel ended by the worker process'));
this._producerSocket.on('error', (error) => this._logger.error('Producer Channel error: %s', String(error)));
this._producerSocket.on('end', () => logger.debug('Producer Channel ended by the worker process'));
this._producerSocket.on('error', (error) => logger.error('Producer Channel error: %s', String(error)));
}
/**
......@@ -183,7 +181,7 @@ export class Channel extends EnhancedEventEmitter
if (this._closed)
return;
this._logger.debug('close()');
logger.debug('close()');
this._closed = true;
......@@ -193,9 +191,6 @@ export class Channel extends EnhancedEventEmitter
sent.close();
}
// Destroy the logger so it does not leak debug instances.
this._logger.destroy();
// Remove event listeners but leave a fake 'error' hander to avoid
// propagation.
this._consumerSocket.removeAllListeners('end');
......@@ -225,7 +220,7 @@ export class Channel extends EnhancedEventEmitter
const id = this._nextId;
this._logger.debug('request() [method:%s, id:%s]', method, id);
logger.debug('request() [method:%s, id:%s]', method, id);
if (this._closed)
throw new InvalidStateError('Channel closed');
......@@ -290,7 +285,7 @@ export class Channel extends EnhancedEventEmitter
if (!sent)
{
this._logger.error(
logger.error(
'received response does not match any sent request [id:%s]', msg.id);
return;
......@@ -298,14 +293,14 @@ export class Channel extends EnhancedEventEmitter
if (msg.accepted)
{
this._logger.debug(
logger.debug(
'request succeeded [method:%s, id:%s]', sent.method, sent.id);
sent.resolve(msg.data);
}
else if (msg.error)
{
this._logger.warn(
logger.warn(
'request failed [method:%s, id:%s]: %s',
sent.method, sent.id, msg.reason);
......@@ -321,7 +316,7 @@ export class Channel extends EnhancedEventEmitter
}
else
{
this._logger.error(
logger.error(
'received response is not accepted nor rejected [method:%s, id:%s]',
sent.method, sent.id);
}
......@@ -334,7 +329,7 @@ export class Channel extends EnhancedEventEmitter
// Otherwise unexpected message.
else
{
this._logger.error(
logger.error(
'received message is not a response nor a notification');
}
}
......
......@@ -216,7 +216,7 @@ export class Consumer extends EnhancedEventEmitter
preferredLayers?: ConsumerLayers;
})
{
super(logger);
super();
logger.debug('constructor()');
......
......@@ -77,7 +77,7 @@ export class DataConsumer extends EnhancedEventEmitter
}
)
{
super(logger);
super();
logger.debug('constructor()');
......
......@@ -89,7 +89,7 @@ export class DataProducer extends EnhancedEventEmitter
}
)
{
super(logger);
super();
logger.debug('constructor()');
......
import { EventEmitter } from 'events';
import { Logger } from './Logger';
const staticLogger = new Logger('EnhancedEventEmitter');
const logger = new Logger('EnhancedEventEmitter');
export class EnhancedEventEmitter extends EventEmitter
{
// Logger instance.
protected readonly _logger: Logger;
constructor(logger?: Logger)
constructor()
{
super();
this.setMaxListeners(Infinity);
this._logger = logger || staticLogger;
}
safeEmit(event: string, ...args: any[]): boolean
......@@ -26,7 +21,7 @@ export class EnhancedEventEmitter extends EventEmitter
}
catch (error)
{
this._logger.error(
logger.error(
'safeEmit() | event listener threw an error [event:%s]:%o',
event, error);
......
......@@ -44,11 +44,4 @@ export class Logger
{
return this._error;
}
destroy(): void
{
this._debug.destroy();
this._warn.destroy();
this._error.destroy();
}
}
......@@ -193,7 +193,7 @@ export class Producer extends EnhancedEventEmitter
}
)
{
super(logger);
super();
logger.debug('constructor()');
......
......@@ -144,7 +144,7 @@ export class Router extends EnhancedEventEmitter
}
)
{
super(logger);
super();
logger.debug('constructor()');
......
......@@ -51,7 +51,7 @@ export class RtpObserver extends EnhancedEventEmitter
}
)
{
super(logger);
super();
logger.debug('constructor()');
......
......@@ -157,7 +157,7 @@ export class Transport extends EnhancedEventEmitter
}
)
{
super(logger);
super();
logger.debug('constructor()');
......
......@@ -158,15 +158,13 @@ const workerBin = process.env.MEDIASOUP_WORKER_BIN
: path.join(__dirname, '..', 'worker', 'out', 'Release', 'mediasoup-worker');
const logger = new Logger('Worker');
const workerLogger = new Logger('Worker');
export class Worker extends EnhancedEventEmitter
{
// mediasoup-worker child process.
private _child?: ChildProcess;
// Logger for stdout and stderr logs from the worker process.
private readonly _workerLogger: Logger;
// Worker process PID.
private readonly _pid: number;
......@@ -265,8 +263,6 @@ export class Worker extends EnhancedEventEmitter
stdio : [ 'ignore', 'pipe', 'pipe', 'pipe', 'pipe' ]
});
this._workerLogger = new Logger(`worker[pid:${this._child.pid}]`);
this._pid = this._child.pid;
this._channel = new Channel(
......@@ -361,7 +357,7 @@ export class Worker extends EnhancedEventEmitter
for (const line of buffer.toString('utf8').split('\n'))
{
if (line)
this._workerLogger.debug(`(stdout) ${line}`);
workerLogger.debug(`(stdout) ${line}`);
}
});
......@@ -371,7 +367,7 @@ export class Worker extends EnhancedEventEmitter
for (const line of buffer.toString('utf8').split('\n'))
{
if (line)
this._workerLogger.error(`(stderr) ${line}`);
workerLogger.error(`(stderr) ${line}`);
}
});
}
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论