mirror of
https://github.com/oxen-io/session-android.git
synced 2024-11-28 04:25:18 +00:00
Increase reliability of locally logging crashes.
Exception logging tends to be race-y, so now we block and wait for all logs to be written before continuing with the crash.
This commit is contained in:
parent
01a9931d92
commit
11a2ed0743
@ -185,7 +185,7 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc
|
|||||||
|
|
||||||
private void initializeCrashHandling() {
|
private void initializeCrashHandling() {
|
||||||
final Thread.UncaughtExceptionHandler originalHandler = Thread.getDefaultUncaughtExceptionHandler();
|
final Thread.UncaughtExceptionHandler originalHandler = Thread.getDefaultUncaughtExceptionHandler();
|
||||||
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionLogger(originalHandler, persistentLogger));
|
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionLogger(originalHandler));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initializeJobManager() {
|
private void initializeJobManager() {
|
||||||
|
@ -31,4 +31,8 @@ public class AndroidLogger extends Log.Logger {
|
|||||||
public void wtf(String tag, String message, Throwable t) {
|
public void wtf(String tag, String message, Throwable t) {
|
||||||
android.util.Log.wtf(tag, message, t);
|
android.util.Log.wtf(tag, message, t);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void blockUntilAllWritesFinished() {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -127,6 +127,14 @@ public class Log {
|
|||||||
return simpleName;
|
return simpleName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void blockUntilAllWritesFinished() {
|
||||||
|
if (loggers != null) {
|
||||||
|
for (Logger logger : loggers) {
|
||||||
|
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);
|
||||||
@ -134,5 +142,6 @@ public class Log {
|
|||||||
public abstract void w(String tag, String message, Throwable t);
|
public abstract void w(String tag, String message, Throwable t);
|
||||||
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,7 @@ import java.util.Arrays;
|
|||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
@ -47,7 +48,7 @@ public class PersistentLogger extends Log.Logger {
|
|||||||
this.context = context.getApplicationContext();
|
this.context = context.getApplicationContext();
|
||||||
this.secret = LogSecretProvider.getOrCreateAttachmentSecret(context);
|
this.secret = LogSecretProvider.getOrCreateAttachmentSecret(context);
|
||||||
this.executor = Executors.newSingleThreadExecutor(r -> {
|
this.executor = Executors.newSingleThreadExecutor(r -> {
|
||||||
Thread thread = new Thread(r, "logger");
|
Thread thread = new Thread(r, "PersistentLogger");
|
||||||
thread.setPriority(Thread.MIN_PRIORITY);
|
thread.setPriority(Thread.MIN_PRIORITY);
|
||||||
return thread;
|
return thread;
|
||||||
});
|
});
|
||||||
@ -85,6 +86,19 @@ public class PersistentLogger extends Log.Logger {
|
|||||||
write(LOG_WTF, tag, message, t);
|
write(LOG_WTF, tag, message, t);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void blockUntilAllWritesFinished() {
|
||||||
|
CountDownLatch latch = new CountDownLatch(1);
|
||||||
|
|
||||||
|
executor.execute(latch::countDown);
|
||||||
|
|
||||||
|
try {
|
||||||
|
latch.await();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
android.util.Log.w(TAG, "Failed to wait for all writes.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@WorkerThread
|
@WorkerThread
|
||||||
public ListenableFuture<String> getLogs() {
|
public ListenableFuture<String> getLogs() {
|
||||||
final SettableFuture<String> future = new SettableFuture<>();
|
final SettableFuture<String> future = new SettableFuture<>();
|
||||||
|
@ -7,16 +7,15 @@ public class UncaughtExceptionLogger implements Thread.UncaughtExceptionHandler
|
|||||||
private static final String TAG = UncaughtExceptionLogger.class.getSimpleName();
|
private static final String TAG = UncaughtExceptionLogger.class.getSimpleName();
|
||||||
|
|
||||||
private final Thread.UncaughtExceptionHandler originalHandler;
|
private final Thread.UncaughtExceptionHandler originalHandler;
|
||||||
private final PersistentLogger persistentLogger;
|
|
||||||
|
|
||||||
public UncaughtExceptionLogger(@NonNull Thread.UncaughtExceptionHandler originalHandler, @NonNull PersistentLogger persistentLogger) {
|
public UncaughtExceptionLogger(@NonNull Thread.UncaughtExceptionHandler originalHandler) {
|
||||||
this.originalHandler = originalHandler;
|
this.originalHandler = originalHandler;
|
||||||
this.persistentLogger = persistentLogger;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void uncaughtException(Thread t, Throwable e) {
|
public void uncaughtException(Thread t, Throwable e) {
|
||||||
Log.e(TAG, "", e);
|
Log.e(TAG, "", e);
|
||||||
|
Log.blockUntilAllWritesFinished();
|
||||||
originalHandler.uncaughtException(t, e);
|
originalHandler.uncaughtException(t, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user