Skip to content
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
49 changes: 47 additions & 2 deletions webrtc-jni/src/main/cpp/src/JNI_Logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,38 @@

#include "rtc_base/logging.h"

#include <cstdio>
#include <memory>
#include <mutex>

#ifdef _WIN32
#include <windows.h>
#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)
Expand Down Expand Up @@ -53,8 +84,22 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_logging_Logging_logToDebug
{
int rtcSeverity = jni::JavaEnums::toNative<webrtc::LoggingSeverity>(env, jseverity);

if (rtcSeverity >= webrtc::LS_VERBOSE && rtcSeverity <= webrtc::LS_NONE) {
webrtc::LogMessage::LogToDebug(static_cast<webrtc::LoggingSeverity>(rtcSeverity));
if (rtcSeverity < webrtc::LS_VERBOSE || rtcSeverity > webrtc::LS_NONE) {
return;
}

auto severity = static_cast<webrtc::LoggingSeverity>(rtcSeverity);

std::lock_guard<std::mutex> lock(debugLogSinkMutex);

if (debugLogSinkAttached) {
webrtc::LogMessage::RemoveLogToStream(&debugLogSink);
debugLogSinkAttached = false;
}

if (severity != webrtc::LS_NONE) {
webrtc::LogMessage::AddLogToStream(&debugLogSink, severity);
debugLogSinkAttached = true;
}
}

Expand Down
12 changes: 12 additions & 0 deletions webrtc-jni/src/main/cpp/src/JNI_WebRTC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include "JavaUtils.h"
#include "WebRTCContext.h"

#include "rtc_base/logging.h"

#include <jni.h>

jni::JavaContext * javaContext = nullptr;
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading