Reland "Reland "Injectable logging""
This is a reland of 21219a0e43446701810236fb9fdd59be072c12df The default implementation of OnLogMessage(msg, sev, tag) discarded the tag, resulting in FileRotatingLogSink not receiving tags. Since the revert the default implementation of OnLogMessage(msg, sev, tag) has been updated to add the tag to the log message. A more efficient implementation of it has also been added for FileRotatingLogSink. Unit tests are added for the default implementation and for Loggable injection. Original change's description: > Reland "Injectable logging" > > Any injected loggable or NativeLogger would be deleted if PCFactory > was reinitialized without calling setInjectableLogger. Now native > logging is not implemented as a Loggable, so it will remain active > unless a Loggable is injected. > > This is a reland of 59216ec4a4151b1ba5478c8f2b5c9f01f4683d7f > > Original change's description: > > Injectable logging > > > > Allows passing a Loggable to PCFactory.initializationOptions, which > > is then injected to Logging.java and logging.h. Future log messages > > in both Java and native will then be passed to this Loggable. > > > > Bug: webrtc:9225 > > Change-Id: I2ff693380639448301a78a93dc11d3a0106f0967 > > Reviewed-on: https://webrtc-review.googlesource.com/73243 > > Commit-Queue: Paulina Hensman <phensman@webrtc.org> > > Reviewed-by: Karl Wiberg <kwiberg@webrtc.org> > > Reviewed-by: Sami Kalliomäki <sakal@webrtc.org> > > Cr-Commit-Position: refs/heads/master@{#23241} > > Bug: webrtc:9225 > Change-Id: I2fe3fbc8c323814284bb62e43fe1870bdab581ee > TBR: kwiberg > Reviewed-on: https://webrtc-review.googlesource.com/77140 > Commit-Queue: Paulina Hensman <phensman@webrtc.org> > Reviewed-by: Sami Kalliomäki <sakal@webrtc.org> > Cr-Commit-Position: refs/heads/master@{#23310} Bug: webrtc:9225 Change-Id: I67a5728fe772f0bedc9509713ed8b8ffdc31af81 TBR: kwiberg Reviewed-on: https://webrtc-review.googlesource.com/80860 Commit-Queue: Paulina Hensman <phensman@webrtc.org> Reviewed-by: Sami Kalliomäki <sakal@webrtc.org> Cr-Commit-Position: refs/heads/master@{#23711}
This commit is contained in:
committed by
Commit Bot
parent
6f440ed5b5
commit
1ec04f19c6
@ -0,0 +1,164 @@
|
||||
/*
|
||||
* Copyright 2018 The WebRTC Project Authors. All rights reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
package org.webrtc;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
import android.support.test.InstrumentationRegistry;
|
||||
import android.support.test.filters.SmallTest;
|
||||
import android.support.test.runner.AndroidJUnit4;
|
||||
import java.util.ArrayList;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.webrtc.PeerConnectionFactory;
|
||||
import org.webrtc.Logging.Severity;
|
||||
import org.webrtc.Loggable;
|
||||
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class LoggableTest {
|
||||
private static String TAG = "LoggableTest";
|
||||
private static String NATIVE_FILENAME_TAG = "loggable_test.cc";
|
||||
|
||||
private static class MockLoggable implements Loggable {
|
||||
private ArrayList<String> messages = new ArrayList<>();
|
||||
private ArrayList<Severity> sevs = new ArrayList<>();
|
||||
private ArrayList<String> tags = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public void onLogMessage(String message, Severity sev, String tag) {
|
||||
messages.add(message);
|
||||
sevs.add(sev);
|
||||
tags.add(tag);
|
||||
}
|
||||
|
||||
public boolean isMessageReceived(String message) {
|
||||
for (int i = 0; i < messages.size(); i++) {
|
||||
if (messages.get(i).contains(message)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isMessageReceived(String message, Severity sev, String tag) {
|
||||
for (int i = 0; i < messages.size(); i++) {
|
||||
if (messages.get(i).contains(message) && sevs.get(i) == sev && tags.get(i).equals(tag)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private final MockLoggable mockLoggable = new MockLoggable();
|
||||
|
||||
@Test
|
||||
@SmallTest
|
||||
public void testLoggableSetWithoutError() throws InterruptedException {
|
||||
PeerConnectionFactory.initialize(PeerConnectionFactory.InitializationOptions
|
||||
.builder(InstrumentationRegistry.getTargetContext())
|
||||
.setInjectableLogger(mockLoggable, Severity.LS_INFO)
|
||||
.setNativeLibraryName(TestConstants.NATIVE_LIBRARY)
|
||||
.createInitializationOptions());
|
||||
}
|
||||
|
||||
@Test
|
||||
@SmallTest
|
||||
public void testMessageIsLoggedCorrectly() throws InterruptedException {
|
||||
PeerConnectionFactory.initialize(PeerConnectionFactory.InitializationOptions
|
||||
.builder(InstrumentationRegistry.getTargetContext())
|
||||
.setInjectableLogger(mockLoggable, Severity.LS_INFO)
|
||||
.setNativeLibraryName(TestConstants.NATIVE_LIBRARY)
|
||||
.createInitializationOptions());
|
||||
String msg = "Message that should be logged";
|
||||
Logging.d(TAG, msg);
|
||||
assertTrue(mockLoggable.isMessageReceived(msg, Severity.LS_INFO, TAG));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SmallTest
|
||||
public void testLowSeverityIsFiltered() throws InterruptedException {
|
||||
// Set severity to LS_WARNING to filter out LS_INFO and below.
|
||||
PeerConnectionFactory.initialize(PeerConnectionFactory.InitializationOptions
|
||||
.builder(InstrumentationRegistry.getTargetContext())
|
||||
.setInjectableLogger(mockLoggable, Severity.LS_WARNING)
|
||||
.setNativeLibraryName(TestConstants.NATIVE_LIBRARY)
|
||||
.createInitializationOptions());
|
||||
String msg = "Message that should NOT be logged";
|
||||
Logging.d(TAG, msg);
|
||||
assertFalse(mockLoggable.isMessageReceived(msg));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SmallTest
|
||||
public void testLoggableDoesNotReceiveMessagesAfterUnsetting() {
|
||||
PeerConnectionFactory.initialize(PeerConnectionFactory.InitializationOptions
|
||||
.builder(InstrumentationRegistry.getTargetContext())
|
||||
.setInjectableLogger(mockLoggable, Severity.LS_INFO)
|
||||
.setNativeLibraryName(TestConstants.NATIVE_LIBRARY)
|
||||
.createInitializationOptions());
|
||||
// Reinitialize without Loggable
|
||||
PeerConnectionFactory.initialize(PeerConnectionFactory.InitializationOptions
|
||||
.builder(InstrumentationRegistry.getTargetContext())
|
||||
.setNativeLibraryName(TestConstants.NATIVE_LIBRARY)
|
||||
.createInitializationOptions());
|
||||
String msg = "Message that should NOT be logged";
|
||||
Logging.d(TAG, msg);
|
||||
assertFalse(mockLoggable.isMessageReceived(msg));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SmallTest
|
||||
public void testNativeMessageIsLoggedCorrectly() throws InterruptedException {
|
||||
PeerConnectionFactory.initialize(PeerConnectionFactory.InitializationOptions
|
||||
.builder(InstrumentationRegistry.getTargetContext())
|
||||
.setInjectableLogger(mockLoggable, Severity.LS_INFO)
|
||||
.setNativeLibraryName(TestConstants.NATIVE_LIBRARY)
|
||||
.createInitializationOptions());
|
||||
String msg = "Message that should be logged";
|
||||
nativeLogInfoTestMessage(msg);
|
||||
assertTrue(mockLoggable.isMessageReceived(msg, Severity.LS_INFO, NATIVE_FILENAME_TAG));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SmallTest
|
||||
public void testNativeLowSeverityIsFiltered() throws InterruptedException {
|
||||
PeerConnectionFactory.initialize(PeerConnectionFactory.InitializationOptions
|
||||
.builder(InstrumentationRegistry.getTargetContext())
|
||||
.setInjectableLogger(mockLoggable, Severity.LS_WARNING)
|
||||
.setNativeLibraryName(TestConstants.NATIVE_LIBRARY)
|
||||
.createInitializationOptions());
|
||||
String msg = "Message that should NOT be logged";
|
||||
nativeLogInfoTestMessage(msg);
|
||||
assertFalse(mockLoggable.isMessageReceived(msg));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SmallTest
|
||||
public void testNativeLoggableDoesNotReceiveMessagesAfterUnsetting() {
|
||||
PeerConnectionFactory.initialize(PeerConnectionFactory.InitializationOptions
|
||||
.builder(InstrumentationRegistry.getTargetContext())
|
||||
.setInjectableLogger(mockLoggable, Severity.LS_INFO)
|
||||
.setNativeLibraryName(TestConstants.NATIVE_LIBRARY)
|
||||
.createInitializationOptions());
|
||||
// Reinitialize without Loggable
|
||||
PeerConnectionFactory.initialize(PeerConnectionFactory.InitializationOptions
|
||||
.builder(InstrumentationRegistry.getTargetContext())
|
||||
.setNativeLibraryName(TestConstants.NATIVE_LIBRARY)
|
||||
.createInitializationOptions());
|
||||
String msg = "Message that should NOT be logged";
|
||||
nativeLogInfoTestMessage(msg);
|
||||
assertFalse(mockLoggable.isMessageReceived(msg));
|
||||
}
|
||||
|
||||
private static native void nativeLogInfoTestMessage(String message);
|
||||
}
|
||||
Reference in New Issue
Block a user