From 5b1cb82dd9adfcae32a86f5c3be5aaf1f8a67aed Mon Sep 17 00:00:00 2001 From: CI010 Date: Sun, 27 Nov 2022 23:50:28 +0800 Subject: [PATCH 1/4] Bump version and add logger callback --- CMakeLists.txt | 2 +- lib/index.d.ts | 2 +- src/rtc-wrapper.cpp | 20 +++++++++++++++++++- src/rtc-wrapper.h | 4 ++++ 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e6d9c4c..3313420 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,7 +33,7 @@ include(FetchContent) FetchContent_Declare( libdatachannel GIT_REPOSITORY https://github.com/paullouisageneau/libdatachannel.git - GIT_TAG "v0.17.10" + GIT_TAG "v0.17.12" ) option(NO_MEDIA "Disable media transport support in libdatachannel" OFF) diff --git a/lib/index.d.ts b/lib/index.d.ts index ad56a0c..cf3dadf 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -17,7 +17,7 @@ export interface SctpSettings { // Functions export function preload(): void; -export function initLogger(level: LogLevel): void; +export function initLogger(level: LogLevel, callback?: (level: number, message: string) => void): void; export function cleanup(): void; export function setSctpSettings(settings: SctpSettings): void; diff --git a/src/rtc-wrapper.cpp b/src/rtc-wrapper.cpp index 52b5029..600c3b3 100644 --- a/src/rtc-wrapper.cpp +++ b/src/rtc-wrapper.cpp @@ -61,7 +61,23 @@ void RtcWrapper::initLogger(const Napi::CallbackInfo &info) try { - rtc::InitLogger(logLevel); + if (length < 2) + { + rtc::InitLogger(logLevel); + } + else + { + logCallback = std::make_unique(info[1].As()); + rtc::InitLogger(logLevel, [&](rtc::LogLevel level, std::string message) { + if (logCallback) + logCallback->call([level, message = std::move(message)](Napi::Env env, std::vector &args) { + // This will run in main thread and needs to construct the + // arguments for the call + args = {Napi::Number::New(env, static_cast(level)), Napi::String::New(env, message)}; + }); + }); + } + } catch (std::exception &ex) { @@ -85,6 +101,8 @@ void RtcWrapper::cleanup(const Napi::CallbackInfo &info) // Clear Callbacks PeerConnectionWrapper::ResetCallbacksAll(); + + if (logCallback) logCallback.reset(); } catch (std::exception &ex) { diff --git a/src/rtc-wrapper.h b/src/rtc-wrapper.h index 50335e7..42ce8ed 100644 --- a/src/rtc-wrapper.h +++ b/src/rtc-wrapper.h @@ -8,6 +8,8 @@ #include "rtc/rtc.hpp" +#include "thread-safe-callback.h" + class RtcWrapper { public: @@ -16,6 +18,8 @@ class RtcWrapper static void initLogger(const Napi::CallbackInfo &info); static void cleanup(const Napi::CallbackInfo &info); static void setSctpSettings(const Napi::CallbackInfo &info); +private: + static inline std::unique_ptr logCallback = nullptr; }; #endif // RTC_WRAPPER_H From b73068f74d23768320a88e6478fe0ad15800eb65 Mon Sep 17 00:00:00 2001 From: CI010 Date: Mon, 28 Nov 2022 01:41:38 +0800 Subject: [PATCH 2/4] Add type guard --- src/rtc-wrapper.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/rtc-wrapper.cpp b/src/rtc-wrapper.cpp index 600c3b3..c2548af 100644 --- a/src/rtc-wrapper.cpp +++ b/src/rtc-wrapper.cpp @@ -67,6 +67,11 @@ void RtcWrapper::initLogger(const Napi::CallbackInfo &info) } else { + if (!info[1].IsFunction()) + { + Napi::TypeError::New(env, "Function expected").ThrowAsJavaScriptException(); + return; + } logCallback = std::make_unique(info[1].As()); rtc::InitLogger(logLevel, [&](rtc::LogLevel level, std::string message) { if (logCallback) From 879d2639de50ab0bf268c873fc9f297b136b30ea Mon Sep 17 00:00:00 2001 From: CI010 Date: Mon, 28 Nov 2022 13:44:26 +0800 Subject: [PATCH 3/4] Convert log level to string --- lib/index.d.ts | 2 +- src/rtc-wrapper.cpp | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/index.d.ts b/lib/index.d.ts index cf3dadf..40d8f4f 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -17,7 +17,7 @@ export interface SctpSettings { // Functions export function preload(): void; -export function initLogger(level: LogLevel, callback?: (level: number, message: string) => void): void; +export function initLogger(level: LogLevel, callback?: (level: LogLevel, message: string) => void): void; export function cleanup(): void; export function setSctpSettings(settings: SctpSettings): void; diff --git a/src/rtc-wrapper.cpp b/src/rtc-wrapper.cpp index c2548af..adecbc5 100644 --- a/src/rtc-wrapper.cpp +++ b/src/rtc-wrapper.cpp @@ -78,7 +78,21 @@ void RtcWrapper::initLogger(const Napi::CallbackInfo &info) logCallback->call([level, message = std::move(message)](Napi::Env env, std::vector &args) { // This will run in main thread and needs to construct the // arguments for the call - args = {Napi::Number::New(env, static_cast(level)), Napi::String::New(env, message)}; + + std::string logLevel; + if (level == rtc::LogLevel::Verbose) + logLevel = "Verbos"; + if (level == rtc::LogLevel::Debug) + logLevel = "Debug"; + if (level == rtc::LogLevel::Info) + logLevel = "Info"; + if (level == rtc::LogLevel::Warning) + logLevel = "Warning"; + if (level == rtc::LogLevel::Error) + logLevel = "Error"; + if (level == rtc::LogLevel::Fatal) + logLevel = "Fatal"; + args = {Napi::String::New(env, logLevel), Napi::String::New(env, message)}; }); }); } From 07a0ff349391f2334802446e621f42953cc3afb5 Mon Sep 17 00:00:00 2001 From: CI010 Date: Tue, 29 Nov 2022 21:55:10 +0800 Subject: [PATCH 4/4] Fix typo --- src/rtc-wrapper.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rtc-wrapper.cpp b/src/rtc-wrapper.cpp index adecbc5..d5d7a50 100644 --- a/src/rtc-wrapper.cpp +++ b/src/rtc-wrapper.cpp @@ -81,7 +81,7 @@ void RtcWrapper::initLogger(const Napi::CallbackInfo &info) std::string logLevel; if (level == rtc::LogLevel::Verbose) - logLevel = "Verbos"; + logLevel = "Verbose"; if (level == rtc::LogLevel::Debug) logLevel = "Debug"; if (level == rtc::LogLevel::Info)