Move crash reporting listener to the server

Summary: Changelog: Move crash watcher to the server. Add 'device-crash' event. Add 'device-start-crash-watcher', 'device-stop-crash-watcher' commands. Add 'onDeviceCrash' method to Plugin Client.

Reviewed By: mweststrate

Differential Revision: D33089810

fbshipit-source-id: ed62ee7c1129e5e25af18b444744b0796f567b72
This commit is contained in:
Andrey Goncharov
2021-12-20 11:37:25 -08:00
committed by Facebook GitHub Bot
parent 9fd45b96d2
commit 731749b41f
21 changed files with 519 additions and 426 deletions

View File

@@ -15,6 +15,7 @@ import {
Idler,
createState,
getFlipperLib,
CrashLogListener,
} from 'flipper-plugin';
import {
DeviceLogEntry,
@@ -22,6 +23,7 @@ import {
DeviceType,
DeviceDescription,
FlipperServer,
CrashLog,
} from 'flipper-common';
import {DeviceSpec, PluginDetails} from 'flipper-common';
import {getPluginKey} from '../utils/pluginKey';
@@ -85,6 +87,8 @@ export default class BaseDevice implements Device {
logListeners: Map<Symbol, DeviceLogListener> = new Map();
crashListeners: Map<Symbol, CrashLogListener> = new Map();
readonly connected = createState(true);
// if imported, stores the original source location
@@ -181,6 +185,52 @@ export default class BaseDevice implements Device {
}
}
private crashLogEventHandler = (payload: {
serial: string;
crash: CrashLog;
}) => {
if (payload.serial === this.serial && this.crashListeners.size > 0) {
this.addCrashEntry(payload.crash);
}
};
addCrashEntry(entry: CrashLog) {
this.crashListeners.forEach((listener) => {
// prevent breaking other listeners, if one listener doesn't work.
try {
listener(entry);
} catch (e) {
console.error(`Crash listener exception:`, e);
}
});
}
async startCrashWatcher() {
await this.flipperServer.exec('device-start-crash-watcher', this.serial);
this.flipperServer.on('device-crash', this.crashLogEventHandler);
}
stopCrashWatcher() {
this.flipperServer.off('device-crash', this.crashLogEventHandler);
return this.flipperServer.exec('device-stop-crash-watcher', this.serial);
}
addCrashListener(callback: CrashLogListener): Symbol {
if (this.crashListeners.size === 0) {
this.startCrashWatcher();
}
const id = Symbol();
this.crashListeners.set(id, callback);
return id;
}
removeCrashListener(id: Symbol) {
this.crashListeners.delete(id);
if (this.crashListeners.size === 0) {
this.stopCrashWatcher();
}
}
async navigateToLocation(location: string) {
return this.flipperServer.exec('device-navigate', this.serial, location);
}
@@ -328,6 +378,8 @@ export default class BaseDevice implements Device {
disconnect() {
this.logListeners.clear();
this.stopLogging();
this.crashListeners.clear();
this.stopCrashWatcher();
this.connected.set(false);
}