Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node: New setLogEventListeners() utility to get log events #1448

Merged
merged 2 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- `Worker`: Fix `io_uring` support detection ([PR #1445](https://github.com/versatica/mediasoup/pull/1445)).
- Mitigate libsrtp wraparound with loss decryption failure ([PR #1438](https://github.com/versatica/mediasoup/pull/1438)).
- Node: New `setLogEventListeners()` utility to get log events ([PR #1448](https://github.com/versatica/mediasoup/pull/1448)).

### 3.14.11

Expand Down
2 changes: 1 addition & 1 deletion node/src/ActiveSpeakerObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export class ActiveSpeakerObserver<
}

default: {
logger.error('ignoring unknown event "%s"', event);
logger.error(`ignoring unknown event "${event}"`);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion node/src/AudioLevelObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export class AudioLevelObserver<
}

default: {
logger.error('ignoring unknown event "%s"', event);
logger.error(`ignoring unknown event "${event}"`);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion node/src/Consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@ export class Consumer<
}

default: {
logger.error('ignoring unknown event "%s"', event);
logger.error(`ignoring unknown event "${event}"`);
}
}
}
Expand Down
7 changes: 2 additions & 5 deletions node/src/DataConsumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ export class DataConsumer<
* Set buffered amount low threshold.
*/
async setBufferedAmountLowThreshold(threshold: number): Promise<void> {
logger.debug('setBufferedAmountLowThreshold() [threshold:%s]', threshold);
logger.debug(`setBufferedAmountLowThreshold() [threshold:${threshold}]`);

/* Build Request. */
const requestOffset =
Expand Down Expand Up @@ -714,10 +714,7 @@ export class DataConsumer<
}

default: {
logger.error(
'ignoring unknown event "%s" in channel listener',
event
);
logger.error(`ignoring unknown event "${event}"`);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion node/src/DirectTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ export class DirectTransport<
}

default: {
logger.error('ignoring unknown event "%s"', event);
logger.error(`ignoring unknown event "${event}"`);
}
}
}
Expand Down
46 changes: 40 additions & 6 deletions node/src/Logger.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,35 @@
import debug from 'debug';
import { EnhancedEventEmitter } from './enhancedEvents';

const APP_NAME = 'mediasoup';

export type LoggerEmitterEvents = {
debuglog: [string, string];
warnlog: [string, string];
errorlog: [string, string, Error?];
};

export type LoggerEmitter = EnhancedEventEmitter<LoggerEmitterEvents>;

export class Logger {
private static debugLogEmitter?: LoggerEmitter;
private static warnLogEmitter?: LoggerEmitter;
private static errorLogEmitter?: LoggerEmitter;

readonly #debug: debug.Debugger;
readonly #warn: debug.Debugger;
readonly #error: debug.Debugger;

static setEmitters(
debugLogEmitter?: LoggerEmitter,
warnLogEmitter?: LoggerEmitter,
errorLogEmitter?: LoggerEmitter
): void {
Logger.debugLogEmitter = debugLogEmitter;
Logger.warnLogEmitter = warnLogEmitter;
Logger.errorLogEmitter = errorLogEmitter;
}

constructor(prefix?: string) {
if (prefix) {
this.#debug = debug(`${APP_NAME}:${prefix}`);
Expand All @@ -25,15 +48,26 @@ export class Logger {
/* eslint-enable no-console */
}

get debug(): debug.Debugger {
return this.#debug;
debug(log: string): void {
this.#debug(log);

Logger.debugLogEmitter?.safeEmit('debuglog', this.#debug.namespace, log);
}

get warn(): debug.Debugger {
return this.#warn;
warn(log: string): void {
this.#warn(log);

Logger.warnLogEmitter?.safeEmit('warnlog', this.#warn.namespace, log);
}

get error(): debug.Debugger {
return this.#error;
error(log: string, error?: Error): void {
this.#error(log, error);

Logger.errorLogEmitter?.safeEmit(
'errorlog',
this.#error.namespace,
log,
error
);
}
}
2 changes: 1 addition & 1 deletion node/src/PipeTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ export class PipeTransport<
}

default: {
logger.error('ignoring unknown event "%s"', event);
logger.error(`ignoring unknown event "${event}"`);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion node/src/PlainTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ export class PlainTransport<
}

default: {
logger.error('ignoring unknown event "%s"', event);
logger.error(`ignoring unknown event "${event}"`);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion node/src/Producer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ export class Producer<
}

default: {
logger.error('ignoring unknown event "%s"', event);
logger.error(`ignoring unknown event "${event}"`);
}
}
}
Expand Down
17 changes: 7 additions & 10 deletions node/src/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1212,7 +1212,7 @@ export class Router<
})
.catch(error => {
logger.error(
'pipeToRouter() | error creating PipeTransport pair:%o',
'pipeToRouter() | error creating PipeTransport pair:',
error
);

Expand Down Expand Up @@ -1281,8 +1281,8 @@ export class Router<
return { pipeConsumer, pipeProducer };
} catch (error) {
logger.error(
'pipeToRouter() | error creating pipe Consumer/Producer pair:%o',
error
'pipeToRouter() | error creating pipe Consumer/Producer pair:',
error as Error
);

if (pipeConsumer) {
Expand Down Expand Up @@ -1326,8 +1326,8 @@ export class Router<
return { pipeDataConsumer, pipeDataProducer };
} catch (error) {
logger.error(
'pipeToRouter() | error creating pipe DataConsumer/DataProducer pair:%o',
error
'pipeToRouter() | error creating pipe DataConsumer/DataProducer pair:',
error as Error
);

pipeDataConsumer?.close();
Expand Down Expand Up @@ -1526,10 +1526,7 @@ export class Router<
const producer = this.#producers.get(producerId);

if (!producer) {
logger.error(
'canConsume() | Producer with id "%s" not found',
producerId
);
logger.error(`canConsume() | Producer with id "${producerId}" not found`);

return false;
}
Expand All @@ -1543,7 +1540,7 @@ export class Router<
clonedRtpCapabilities
);
} catch (error) {
logger.error('canConsume() | unexpected error: %s', String(error));
logger.error(`canConsume() | unexpected error: ${error}`);

return false;
}
Expand Down
6 changes: 3 additions & 3 deletions node/src/Transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ export abstract class Transport<
* Set maximum incoming bitrate for receiving media.
*/
async setMaxIncomingBitrate(bitrate: number): Promise<void> {
logger.debug('setMaxIncomingBitrate() [bitrate:%s]', bitrate);
logger.debug(`setMaxIncomingBitrate() [bitrate:${bitrate}]`);

/* Build Request. */
const requestOffset =
Expand All @@ -688,7 +688,7 @@ export abstract class Transport<
* Set maximum outgoing bitrate for sending media.
*/
async setMaxOutgoingBitrate(bitrate: number): Promise<void> {
logger.debug('setMaxOutgoingBitrate() [bitrate:%s]', bitrate);
logger.debug(`setMaxOutgoingBitrate() [bitrate:${bitrate}]`);

/* Build Request. */
const requestOffset = new FbsTransport.SetMaxOutgoingBitrateRequestT(
Expand All @@ -707,7 +707,7 @@ export abstract class Transport<
* Set minimum outgoing bitrate for sending media.
*/
async setMinOutgoingBitrate(bitrate: number): Promise<void> {
logger.debug('setMinOutgoingBitrate() [bitrate:%s]', bitrate);
logger.debug(`setMinOutgoingBitrate() [bitrate:${bitrate}]`);

/* Build Request. */
const requestOffset = new FbsTransport.SetMinOutgoingBitrateRequestT(
Expand Down
2 changes: 1 addition & 1 deletion node/src/WebRtcTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ export class WebRtcTransport<
}

default: {
logger.error('ignoring unknown event "%s"', event);
logger.error(`ignoring unknown event "${event}"`);
}
}
}
Expand Down
34 changes: 8 additions & 26 deletions node/src/Worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,11 +348,7 @@ export class Worker<
spawnArgs.push(`--disableLiburing=true`);
}

logger.debug(
'spawning worker process: %s %s',
spawnBin,
spawnArgs.join(' ')
);
logger.debug(`spawning worker process: ${spawnBin} ${spawnArgs.join(' ')}`);

this.#child = spawn(
// command
Expand Down Expand Up @@ -398,7 +394,7 @@ export class Worker<
if (!spawnDone && event === Event.WORKER_RUNNING) {
spawnDone = true;

logger.debug('worker process running [pid:%s]', this.#pid);
logger.debug(`worker process running [pid:${this.#pid}]`);

this.emit('@success');
}
Expand All @@ -415,18 +411,14 @@ export class Worker<

if (code === 42) {
logger.error(
'worker process failed due to wrong settings [pid:%s]',
this.#pid
`worker process failed due to wrong settings [pid:${this.#pid}]`
);

this.close();
this.emit('@failure', new TypeError('wrong settings'));
} else {
logger.error(
'worker process failed unexpectedly [pid:%s, code:%s, signal:%s]',
this.#pid,
code,
signal
`worker process failed unexpectedly [pid:${this.#pid}, code:${code}, signal:${signal}]`
);

this.close();
Expand All @@ -437,10 +429,7 @@ export class Worker<
}
} else {
logger.error(
'worker process died unexpectedly [pid:%s, code:%s, signal:%s]',
this.#pid,
code,
signal
`worker process died unexpectedly [pid:${this.#pid}, code:${code}, signal:${signal}]`
);

this.workerDied(
Expand All @@ -459,18 +448,14 @@ export class Worker<
spawnDone = true;

logger.error(
'worker process failed [pid:%s]: %s',
this.#pid,
error.message
`worker process failed [pid:${this.#pid}]: ${error.message}`
);

this.close();
this.emit('@failure', error);
} else {
logger.error(
'worker process error [pid:%s]: %s',
this.#pid,
error.message
`worker process error [pid:${this.#pid}]: ${error.message}`
);

this.workerDied(error);
Expand All @@ -479,10 +464,7 @@ export class Worker<

this.#child.on('close', (code, signal) => {
logger.debug(
'worker subprocess closed [pid:%s, code:%s, signal:%s]',
this.#pid,
code,
signal
`worker subprocess closed [pid:${this.#pid}, code:${code}, signal:${signal}]`
);

this.#subprocessClosed = true;
Expand Down
5 changes: 2 additions & 3 deletions node/src/enhancedEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ export class EnhancedEventEmitter<
return super.emit(eventName, ...args);
} catch (error) {
enhancedEventEmitterLogger.error(
'safeEmit() | event listener threw an error [eventName:%s]:%o',
eventName,
error
`safeEmit() | event listener threw an error [eventName:${eventName}]:`,
error as Error
);

try {
Expand Down
Loading
Loading