From 7e19a0f1e341d13fe7782c7d921395cdbc3b6394 Mon Sep 17 00:00:00 2001 From: Paul Berberian Date: Fri, 1 Sep 2023 16:45:42 +0200 Subject: [PATCH] Updating the log level also impacts the worker --- src/core/api/public_api.ts | 12 ++++++++++++ src/main/send_message.ts | 10 ++++++++++ src/utils/logger.ts | 14 +++++++++++++- src/worker/index.ts | 5 +++++ 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/core/api/public_api.ts b/src/core/api/public_api.ts index cdc646c07e5..58f508c2633 100644 --- a/src/core/api/public_api.ts +++ b/src/core/api/public_api.ts @@ -401,8 +401,20 @@ class Player extends EventEmitter { type: "init", value: { wasmUrl: workerOptions.dashWasmUrl, + logLevel: log.getLevel(), }, }); + log.addEventListener("onLogLevelChange", (logLevel) => { + if (this._priv_worker === null) { + return; + } + sendMessage(this._priv_worker, { + type: "log-level-update", + value: { + logLevel, + }, + }); + }, this._destroyCanceller.signal); } else { this._priv_worker = null; } diff --git a/src/main/send_message.ts b/src/main/send_message.ts index 0332883f41b..61ac6c0db65 100644 --- a/src/main/send_message.ts +++ b/src/main/send_message.ts @@ -3,6 +3,7 @@ import { IManifestFetcherSettings, ISegmentFetcherCreatorBackoffOptions, } from "../core/fetchers"; +import { ILoggerLevel } from "../utils/logger"; import { SourceBufferType } from "../worker/media_source_interface"; export default function sendMessage( @@ -40,6 +41,14 @@ export interface IInitMessage { type : "init"; value : { wasmUrl: string; + logLevel: ILoggerLevel; + }; +} + +export interface ILogLevelUpdateMessage { + type : "log-level-update"; + value : { + logLevel: ILoggerLevel; }; } @@ -136,6 +145,7 @@ export type IReferenceUpdateMessage = export type IMainThreadMessage = IInitMessage | + ILogLevelUpdateMessage | IPrepareContentMessage | IDisposeContentMessage | /** The last prepared content can now begin. */ diff --git a/src/utils/logger.ts b/src/utils/logger.ts index 6b28493fdfa..3e9570f419f 100644 --- a/src/utils/logger.ts +++ b/src/utils/logger.ts @@ -14,6 +14,7 @@ * limitations under the License. */ +import EventEmitter from "./event_emitter"; import noop from "./noop"; export type ILoggerLevel = "NONE" | @@ -28,11 +29,19 @@ type IConsoleFn = ( const DEFAULT_LOG_LEVEL : ILoggerLevel = "NONE"; +/** + * Events sent by `Logger` where the keys are the events' name and the values + * are the corresponding payloads. + */ +interface ILoggerEvents { + onLogLevelChange: ILoggerLevel; +} + /** * Logger implementation. * @class Logger */ -export default class Logger { +export default class Logger extends EventEmitter { public error : IConsoleFn; public warn : IConsoleFn; public info : IConsoleFn; @@ -41,6 +50,7 @@ export default class Logger { private readonly _levels : Record; constructor() { + super(); this.error = noop; this.warn = noop; this.info = noop; @@ -79,6 +89,8 @@ export default class Logger { noop; /* eslint-enable no-console */ /* eslint-enable no-invalid-this */ + + this.trigger("onLogLevelChange", this._currentLevel); } /** diff --git a/src/worker/index.ts b/src/worker/index.ts index 9204c471c23..f4af53609de 100644 --- a/src/worker/index.ts +++ b/src/worker/index.ts @@ -69,6 +69,7 @@ onmessage = function (e: MessageEvent) { const msg = e.data; switch (msg.type) { case "init": + log.setLevel(msg.value.logLevel); parser.initialize({ wasmUrl: msg.value.wasmUrl }).catch((err) => { const error = err instanceof Error ? err.toString() : @@ -76,6 +77,10 @@ onmessage = function (e: MessageEvent) { log.error("Worker: Could not initialize DASH_WASM parser", error); }); break; + case "log-level-update": + log.setLevel(msg.value.logLevel); + break; + case "prepare": prepareNewContent(msg.value); break;