Add Log.internal()

fork-5.53.8
Greyson Parrelli 2021-04-14 14:38:29 -04:00
rodzic c393cd655d
commit e461625da4
6 zmienionych plików z 162 dodań i 61 usunięć

Wyświetl plik

@ -229,7 +229,7 @@ public class ApplicationContext extends MultiDexApplication implements AppForegr
private void initializeLogging() { private void initializeLogging() {
persistentLogger = new PersistentLogger(this, LogSecretProvider.getOrCreateAttachmentSecret(this), BuildConfig.VERSION_NAME); persistentLogger = new PersistentLogger(this, LogSecretProvider.getOrCreateAttachmentSecret(this), BuildConfig.VERSION_NAME);
org.signal.core.util.logging.Log.initialize(new AndroidLogger(), persistentLogger); org.signal.core.util.logging.Log.initialize(FeatureFlags::internalUser, new AndroidLogger(), persistentLogger);
SignalProtocolLoggerProvider.setProvider(new CustomSignalProtocolLogger()); SignalProtocolLoggerProvider.setProvider(new CustomSignalProtocolLogger());
} }

Wyświetl plik

@ -23,7 +23,6 @@ import org.thoughtcrime.securesms.notifications.NotificationIds
import org.thoughtcrime.securesms.recipients.Recipient import org.thoughtcrime.securesms.recipients.Recipient
import org.thoughtcrime.securesms.service.KeyCachingService import org.thoughtcrime.securesms.service.KeyCachingService
import org.thoughtcrime.securesms.util.BubbleUtil.BubbleState import org.thoughtcrime.securesms.util.BubbleUtil.BubbleState
import org.thoughtcrime.securesms.util.FeatureFlags
import org.thoughtcrime.securesms.util.ServiceUtil import org.thoughtcrime.securesms.util.ServiceUtil
import org.thoughtcrime.securesms.util.TextSecurePreferences import org.thoughtcrime.securesms.util.TextSecurePreferences
import org.thoughtcrime.securesms.webrtc.CallNotificationBuilder import org.thoughtcrime.securesms.webrtc.CallNotificationBuilder
@ -111,9 +110,7 @@ class MessageNotifierV2 : MessageNotifier {
val state: NotificationStateV2 = NotificationStateProvider.constructNotificationState(context) val state: NotificationStateV2 = NotificationStateProvider.constructNotificationState(context)
if (FeatureFlags.internalUser()) { Log.internal().i(TAG, state.toString())
Log.i(TAG, state.toString())
}
if (state.isEmpty) { if (state.isEmpty) {
Log.i(TAG, "State is empty, cancelling all notifications") Log.i(TAG, "State is empty, cancelling all notifications")

Wyświetl plik

@ -27,7 +27,6 @@ import org.thoughtcrime.securesms.notifications.NotificationIds
import org.thoughtcrime.securesms.recipients.Recipient import org.thoughtcrime.securesms.recipients.Recipient
import org.thoughtcrime.securesms.util.BubbleUtil import org.thoughtcrime.securesms.util.BubbleUtil
import org.thoughtcrime.securesms.util.ConversationUtil import org.thoughtcrime.securesms.util.ConversationUtil
import org.thoughtcrime.securesms.util.FeatureFlags
import org.thoughtcrime.securesms.util.ServiceUtil import org.thoughtcrime.securesms.util.ServiceUtil
import org.thoughtcrime.securesms.util.TextSecurePreferences import org.thoughtcrime.securesms.util.TextSecurePreferences
@ -56,9 +55,7 @@ object NotificationFactory {
if (Build.VERSION.SDK_INT >= 23 || state.conversations.size == 1) { if (Build.VERSION.SDK_INT >= 23 || state.conversations.size == 1) {
state.conversations.forEach { conversation -> state.conversations.forEach { conversation ->
if (conversation.threadId == visibleThreadId && conversation.hasNewNotifications()) { if (conversation.threadId == visibleThreadId && conversation.hasNewNotifications()) {
if (FeatureFlags.internalUser()) { Log.internal().i(TAG, "Thread is visible, notifying in thread. notificationId: ${conversation.notificationId}")
Log.i(TAG, "Thread is visible, notifying in thread. notificationId: ${conversation.notificationId}")
}
notifyInThread(context, conversation.recipient, lastAudibleNotification) notifyInThread(context, conversation.recipient, lastAudibleNotification)
} else if (conversation.hasNewNotifications() || alertOverrides.contains(conversation.threadId)) { } else if (conversation.hasNewNotifications() || alertOverrides.contains(conversation.threadId)) {
@ -233,9 +230,7 @@ object NotificationFactory {
private fun NotificationManagerCompat.safelyNotify(context: Context, threadRecipient: Recipient?, notificationId: Int, notification: Notification) { private fun NotificationManagerCompat.safelyNotify(context: Context, threadRecipient: Recipient?, notificationId: Int, notification: Notification) {
try { try {
notify(notificationId, notification) notify(notificationId, notification)
if (FeatureFlags.internalUser()) { Log.internal().i(TAG, "Posted notification: $notification")
Log.i(TAG, "Posted notification: $notification")
}
} catch (e: SecurityException) { } catch (e: SecurityException) {
Log.i(TAG, "Security exception when posting notification, clearing ringtone") Log.i(TAG, "Security exception when posting notification, clearing ringtone")
if (threadRecipient != null) { if (threadRecipient != null) {

Wyświetl plik

@ -0,0 +1,65 @@
package org.signal.core.util.logging;
import androidx.annotation.NonNull;
/**
* A way to treat N loggers as one. Wraps a bunch of other loggers and forwards the method calls to
* all of them.
*/
class CompoundLogger extends Log.Logger {
private final Log.Logger[] loggers;
CompoundLogger(@NonNull Log.Logger... loggers) {
this.loggers = loggers;
}
@Override
public void v(String tag, String message, Throwable t) {
for (Log.Logger logger : loggers) {
logger.v(tag, message, t);
}
}
@Override
public void d(String tag, String message, Throwable t) {
for (Log.Logger logger : loggers) {
logger.d(tag, message, t);
}
}
@Override
public void i(String tag, String message, Throwable t) {
for (Log.Logger logger : loggers) {
logger.i(tag, message, t);
}
}
@Override
public void w(String tag, String message, Throwable t) {
for (Log.Logger logger : loggers) {
logger.w(tag, message, t);
}
}
@Override
public void e(String tag, String message, Throwable t) {
for (Log.Logger logger : loggers) {
logger.e(tag, message, t);
}
}
@Override
public void wtf(String tag, String message, Throwable t) {
for (Log.Logger logger : loggers) {
logger.wtf(tag, message, t);
}
}
@Override
public void blockUntilAllWritesFinished() {
for (Log.Logger logger : loggers) {
logger.blockUntilAllWritesFinished();
}
}
}

Wyświetl plik

@ -3,15 +3,30 @@ package org.signal.core.util.logging;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import androidx.annotation.MainThread; import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
import java.util.logging.Logger;
@SuppressLint("LogNotSignal") @SuppressLint("LogNotSignal")
public final class Log { public final class Log {
private static Logger[] loggers; private static final Logger NOOP_LOGGER = new NoopLogger();
private static InternalCheck internalCheck;
private static Logger logger = new AndroidLogger();
/**
* @param internalCheck A checker that will indicate if this is an internal user
* @param loggers A list of loggers that will be given every log statement.
*/
@MainThread @MainThread
public static void initialize(@NonNull InternalCheck internalCheck, Logger... loggers) {
Log.internalCheck = internalCheck;
Log.logger = new CompoundLogger(loggers);
}
public static void initialize(Logger... loggers) { public static void initialize(Logger... loggers) {
Log.loggers = loggers; initialize(() -> false, loggers);
} }
public static void v(String tag, String message) { public static void v(String tag, String message) {
@ -63,63 +78,27 @@ public final class Log {
} }
public static void v(String tag, String message, Throwable t) { public static void v(String tag, String message, Throwable t) {
if (loggers != null) { logger.v(tag, message, t);
for (Logger logger : loggers) {
logger.v(tag, message, t);
}
} else {
android.util.Log.v(tag, message, t);
}
} }
public static void d(String tag, String message, Throwable t) { public static void d(String tag, String message, Throwable t) {
if (loggers != null) { logger.d(tag, message, t);
for (Logger logger : loggers) {
logger.d(tag, message, t);
}
} else {
android.util.Log.d(tag, message, t);
}
} }
public static void i(String tag, String message, Throwable t) { public static void i(String tag, String message, Throwable t) {
if (loggers != null) { logger.i(tag, message, t);
for (Logger logger : loggers) {
logger.i(tag, message, t);
}
} else {
android.util.Log.i(tag, message, t);
}
} }
public static void w(String tag, String message, Throwable t) { public static void w(String tag, String message, Throwable t) {
if (loggers != null) { logger.w(tag, message, t);
for (Logger logger : loggers) {
logger.w(tag, message, t);
}
} else {
android.util.Log.w(tag, message, t);
}
} }
public static void e(String tag, String message, Throwable t) { public static void e(String tag, String message, Throwable t) {
if (loggers != null) { logger.e(tag, message, t);
for (Logger logger : loggers) {
logger.e(tag, message, t);
}
} else {
android.util.Log.e(tag, message, t);
}
} }
public static void wtf(String tag, String message, Throwable t) { public static void wtf(String tag, String message, Throwable t) {
if (loggers != null) { logger.wtf(tag, message, t);
for (Logger logger : loggers) {
logger.wtf(tag, message, t);
}
} else {
android.util.Log.wtf(tag, message, t);
}
} }
public static String tag(Class<?> clazz) { public static String tag(Class<?> clazz) {
@ -130,14 +109,24 @@ public final class Log {
return simpleName; return simpleName;
} }
public static void blockUntilAllWritesFinished() { /**
if (loggers != null) { * Important: This is not something that can be used to log PII. Instead, it's intended use is for
for (Logger logger : loggers) { * logs that might be too verbose or otherwise unnecessary for public users.
logger.blockUntilAllWritesFinished(); *
} * @return The normal logger if this is an internal user, or a no-op logger if it isn't.
*/
public static Logger internal() {
if (internalCheck.isInternal()) {
return logger;
} else {
return NOOP_LOGGER;
} }
} }
public static void blockUntilAllWritesFinished() {
logger.blockUntilAllWritesFinished();
}
public static abstract class Logger { public static abstract class Logger {
public abstract void v(String tag, String message, Throwable t); public abstract void v(String tag, String message, Throwable t);
public abstract void d(String tag, String message, Throwable t); public abstract void d(String tag, String message, Throwable t);
@ -146,5 +135,33 @@ public final class Log {
public abstract void e(String tag, String message, Throwable t); public abstract void e(String tag, String message, Throwable t);
public abstract void wtf(String tag, String message, Throwable t); public abstract void wtf(String tag, String message, Throwable t);
public abstract void blockUntilAllWritesFinished(); public abstract void blockUntilAllWritesFinished();
public void v(String tag, String message) {
v(tag, message, null);
}
public void d(String tag, String message) {
d(tag, message, null);
}
public void i(String tag, String message) {
i(tag, message, null);
}
public void w(String tag, String message) {
w(tag, message, null);
}
public void e(String tag, String message) {
e(tag, message, null);
}
public void wtf(String tag, String message) {
wtf(tag, message, null);
}
}
public interface InternalCheck {
boolean isInternal();
} }
} }

Wyświetl plik

@ -0,0 +1,27 @@
package org.signal.core.util.logging;
/**
* A logger that does nothing.
*/
class NoopLogger extends Log.Logger {
@Override
public void v(String tag, String message, Throwable t) { }
@Override
public void d(String tag, String message, Throwable t) { }
@Override
public void i(String tag, String message, Throwable t) { }
@Override
public void w(String tag, String message, Throwable t) { }
@Override
public void e(String tag, String message, Throwable t) { }
@Override
public void wtf(String tag, String message, Throwable t) { }
@Override
public void blockUntilAllWritesFinished() { }
}