diff --git a/webrtc-jni/src/main/cpp/src/JNI_Logging.cpp b/webrtc-jni/src/main/cpp/src/JNI_Logging.cpp index 6e680aa0..ff2252e6 100644 --- a/webrtc-jni/src/main/cpp/src/JNI_Logging.cpp +++ b/webrtc-jni/src/main/cpp/src/JNI_Logging.cpp @@ -23,7 +23,38 @@ #include "rtc_base/logging.h" +#include #include +#include + +#ifdef _WIN32 +#include +#endif + +namespace +{ + // libwebrtc no longer lets LogMessage::LogToDebug change the severity of its + // own debug output after the logging configuration is initialized. A sink + // registered with AddLogToStream can be added and removed at any time, so + // logToDebug is implemented with one that writes where libwebrtc used to: + // the debugger output on Windows and stderr everywhere. + class DebugLogSink : public webrtc::LogSink + { + public: + void OnLogMessage(const std::string & message) override + { +#ifdef _WIN32 + OutputDebugStringA(message.c_str()); +#endif + fputs(message.c_str(), stderr); + fflush(stderr); + } + }; + + DebugLogSink debugLogSink; + bool debugLogSinkAttached = false; + std::mutex debugLogSinkMutex; +} JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_logging_Logging_addLogSink (JNIEnv * env, jclass caller, jobject jseverity, jobject jsink) @@ -53,8 +84,22 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_logging_Logging_logToDebug { int rtcSeverity = jni::JavaEnums::toNative(env, jseverity); - if (rtcSeverity >= webrtc::LS_VERBOSE && rtcSeverity <= webrtc::LS_NONE) { - webrtc::LogMessage::LogToDebug(static_cast(rtcSeverity)); + if (rtcSeverity < webrtc::LS_VERBOSE || rtcSeverity > webrtc::LS_NONE) { + return; + } + + auto severity = static_cast(rtcSeverity); + + std::lock_guard lock(debugLogSinkMutex); + + if (debugLogSinkAttached) { + webrtc::LogMessage::RemoveLogToStream(&debugLogSink); + debugLogSinkAttached = false; + } + + if (severity != webrtc::LS_NONE) { + webrtc::LogMessage::AddLogToStream(&debugLogSink, severity); + debugLogSinkAttached = true; } } diff --git a/webrtc-jni/src/main/cpp/src/JNI_WebRTC.cpp b/webrtc-jni/src/main/cpp/src/JNI_WebRTC.cpp index 4ac8813a..b932822d 100644 --- a/webrtc-jni/src/main/cpp/src/JNI_WebRTC.cpp +++ b/webrtc-jni/src/main/cpp/src/JNI_WebRTC.cpp @@ -19,6 +19,8 @@ #include "JavaUtils.h" #include "WebRTCContext.h" +#include "rtc_base/logging.h" + #include jni::JavaContext * javaContext = nullptr; @@ -31,6 +33,16 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM * vm, void * reserved) return -1; } + // libwebrtc initializes its logging configuration on the first log line, + // with everything from LS_INFO upwards written to stderr, and the + // configuration can be set only once. Initialize it here, before anything + // can log, so nothing is written unless the application asks for it through + // Logging.logToDebug or a log sink. + webrtc::LoggingConfig loggingConfig; + loggingConfig.set_min_severity(webrtc::LS_NONE); + loggingConfig.set_debug_severity(webrtc::LS_NONE); + webrtc::InitializeLogging(std::move(loggingConfig)); + javaContext = new jni::WebRTCContext(vm); try { diff --git a/webrtc/src/test/java/dev/onvoid/webrtc/logging/LoggingTests.java b/webrtc/src/test/java/dev/onvoid/webrtc/logging/LoggingTests.java index efeb9f17..15920629 100644 --- a/webrtc/src/test/java/dev/onvoid/webrtc/logging/LoggingTests.java +++ b/webrtc/src/test/java/dev/onvoid/webrtc/logging/LoggingTests.java @@ -29,6 +29,15 @@ class LoggingTests { + @Test + void logToDebug() { + Logging.logToDebug(Severity.ERROR); + Logging.error("logToDebug at ERROR"); + Logging.logToDebug(Severity.INFO); + Logging.logToDebug(Severity.NONE); + Logging.info("logToDebug at NONE must not print this"); + } + @Test void logInfo() throws Exception { CountDownLatch latch = new CountDownLatch(3);