diff --git a/coverage/cobertura-coverage.xml b/coverage/cobertura-coverage.xml index 8fae9fa..8f98be4 100644 --- a/coverage/cobertura-coverage.xml +++ b/coverage/cobertura-coverage.xml @@ -1,6 +1,6 @@ - + /Users/luongminh/workspace/bluesea/media-sdk-js @@ -582,8 +582,18 @@ - + + + + + + + + + + + @@ -635,47 +645,61 @@ - + - + - + - + - + - + - + - + - + - + - + + + + + + - - - + + - - - + + + + + + + + + + + + + @@ -719,6 +743,72 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -872,37 +962,42 @@ - + - + - + - + - + - + - + + + + + + @@ -910,40 +1005,46 @@ - - - - + + + + - - + + - + + + + - - - - - - + + + - - - - - - - - - - + + + + + + + + + + + + + + + + diff --git a/rollup.config.mjs b/rollup.config.mjs index 2829e0b..1665496 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -10,7 +10,7 @@ import resolve from '@rollup/plugin-node-resolve'; */ const banner = `/*! * ${packageJSON.name} v${packageJSON.version} - * (c) ${packageJSON.author.name} + * (c) ${packageJSON.author} * Released under the ${packageJSON.license} License. */ `; @@ -39,7 +39,6 @@ const options = { createOutputOptions({ file: './dist/index.js', format: 'commonjs', - // plugins: [resolve()], }), createOutputOptions({ file: './dist/index.cjs', @@ -54,12 +53,10 @@ const options = { createOutputOptions({ file: './dist/index.esm.js', format: 'esm', - // plugins: [resolve()], }), createOutputOptions({ file: './dist/index.umd.js', format: 'umd', - // plugins: [resolve()], }), createOutputOptions({ file: './dist/index.umd.min.js', @@ -69,7 +66,6 @@ const options = { createOutputOptions({ file: './dist/index.iife.js', format: 'iife', - // plugins: [resolve()], }), createOutputOptions({ file: './dist/index.iife.min.js', diff --git a/src/lib/core/rpc.test.ts b/src/lib/core/rpc.test.ts index daabce3..a845c11 100644 --- a/src/lib/core/rpc.test.ts +++ b/src/lib/core/rpc.test.ts @@ -1,5 +1,6 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ import type { IRealtimeSocket } from '../interfaces'; +import { setLogLevel } from '../utils'; import { MockRTSocket } from '../utils/mocks'; import { RPC, RpcRequest } from './rpc'; @@ -50,6 +51,7 @@ describe('RPC', () => { // Mock the socket object socketMock = new MockRTSocket(); rpc = new RPC(socketMock); + setLogLevel(4); }); afterEach(() => { diff --git a/src/lib/interfaces/session.ts b/src/lib/interfaces/session.ts index 792fe09..e8e3d46 100644 --- a/src/lib/interfaces/session.ts +++ b/src/lib/interfaces/session.ts @@ -1,5 +1,5 @@ import type { StreamRemote } from '../remote'; -import type { LatencyMode, Codecs, MixMinusMode, BitrateControlMode } from '../utils/types'; +import type { LatencyMode, Codecs, MixMinusMode, BitrateControlMode, LogLevel } from '../utils/types'; import type { RealtimeSocketState } from './rtsocket'; import type { SenderConfig } from './sender'; @@ -146,4 +146,9 @@ export interface ISessionConfig { * Optional bitrate control mode for the session. */ bitrateControlMode?: BitrateControlMode; + + /** + * Log level for the session. + */ + logLevel?: LogLevel; } diff --git a/src/lib/session.ts b/src/lib/session.ts index f02aaf2..98bba8c 100644 --- a/src/lib/session.ts +++ b/src/lib/session.ts @@ -3,7 +3,7 @@ import { StreamSender } from './sender'; import { StreamKinds } from './utils/types'; import { debounce } from 'ts-debounce'; import { TypedEventEmitter } from './utils/typed-event-emitter'; -import { getLogger } from './utils/logger'; +import { getLogger, setLogLevel } from './utils/logger'; import type { IRPC } from './interfaces/rpc'; import { RPC } from './core/rpc'; import type { IMediaGatewayConnector } from './interfaces/gateway'; @@ -38,6 +38,10 @@ export class Session extends TypedEventEmitter { private _connector: IMediaGatewayConnector, ) { super(); + if (this._cfg.logLevel) { + this.logger.log('set log level:', this._cfg.logLevel); + setLogLevel(this._cfg.logLevel); + } this._socket.on('peer_state', (state) => { this.emit('peer_state', state); switch (state) { diff --git a/src/lib/utils/logger.ts b/src/lib/utils/logger.ts index dc215d7..6433deb 100644 --- a/src/lib/utils/logger.ts +++ b/src/lib/utils/logger.ts @@ -1,10 +1,38 @@ +import { LogLevel } from './types'; + +let logLevel: LogLevel = LogLevel.None; + +export function setLogLevel(level: LogLevel) { + logLevel = level; +} + /* eslint-disable @typescript-eslint/no-explicit-any */ export function getLogger(prefix: string) { return { - log: (...args: any[]) => console.log(prefix, ...args), - debug: (...args: any[]) => console.debug(prefix, ...args), - info: (...args: any[]) => console.info(prefix, ...args), - warn: (...args: any[]) => console.warn(prefix, ...args), - error: (...args: any[]) => console.error(prefix, ...args), + log: (...args: any[]) => { + if (logLevel >= LogLevel.Debug) { + console.log(`[${prefix}]`, ...args); + } + }, + debug: (...args: any[]) => { + if (logLevel >= LogLevel.Debug) { + console.debug(`[${prefix}]`, ...args); + } + }, + info: (...args: any[]) => { + if (logLevel >= LogLevel.Info) { + console.info(`[${prefix}]`, ...args); + } + }, + warn: (...args: any[]) => { + if (logLevel >= LogLevel.Warn) { + console.warn(`[${prefix}]`, ...args); + } + }, + error: (...args: any[]) => { + if (logLevel >= LogLevel.Error) { + console.error(`[${prefix}]`, ...args); + } + }, }; } diff --git a/src/lib/utils/types.ts b/src/lib/utils/types.ts index d31fe65..ce2e46e 100644 --- a/src/lib/utils/types.ts +++ b/src/lib/utils/types.ts @@ -1,5 +1,13 @@ import type { ISessionCallbacks } from '../interfaces'; +export enum LogLevel { + None = 0, + Error = 1, + Warn = 2, + Info = 3, + Debug = 4, +} + export enum StreamKinds { AUDIO = 'audio', VIDEO = 'video',