Files
flipper/desktop/plugins/public/crash_reporter/index.tsx
Michel Weststrate d8f77db632 Code improvements and more logging on connection handling
Summary:
In an attempt to trace Android issues:

1. added more logging to the process (opted for info level for now since this is pretty critical for support requests, yet not super repetitive overall. We could maybe turn it into usage tracking at some point to have central stats?).
2. rewrote promise chains to async/await since they are easier to follow and harder to do accidentally wrong
3. fixed some minor potential problems, will highlights those in code.

Changelog: Improved handling of edge cases in certificate exchange, which should address cases where a Flipper connection wouldn't come up when connection to Android / IOS. Added explicit logging around connection negation.

Reviewed By: lblasa

Differential Revision: D30838947

fbshipit-source-id: a898c6d3be6edc22bd24f9d2bad76e81871360da
2021-09-10 07:04:43 -07:00

106 lines
2.5 KiB
TypeScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import type {FSWatcher} from 'fs';
import {createState, DevicePluginClient} from 'flipper-plugin';
import {showCrashNotification} from './crash-utils';
import {addFileWatcherForiOSCrashLogs} from './ios-crash-utils';
import {startAndroidCrashWatcher} from './android-crash-utils';
export type Crash = {
notificationID: string;
callstack?: string;
reason: string;
name: string;
date: number;
};
export type CrashLog = {
callstack: string;
reason: string;
name: string;
date?: number;
};
export function devicePlugin(client: DevicePluginClient) {
let notificationID = -1;
let watcher: Promise<FSWatcher | undefined> | undefined = undefined;
const crashes = createState<Crash[]>([], {persist: 'crashes'});
const selectedCrash = createState<string | undefined>();
client.onDeepLink((crashId) => {
selectedCrash.set(crashId as string);
});
function reportCrash(payload: CrashLog) {
notificationID++;
const crash = {
notificationID: notificationID.toString(),
callstack: payload.callstack,
name: payload.name,
reason: payload.reason,
date: payload.date ?? Date.now(),
};
crashes.update((draft) => {
draft.push(crash);
});
showCrashNotification(client, crash);
}
// Startup logic to establish log monitoring
if (client.device.isConnected) {
if (client.device.os.includes('iOS')) {
watcher = addFileWatcherForiOSCrashLogs(
client.device.serial,
reportCrash,
);
} else {
startAndroidCrashWatcher(client, reportCrash);
}
}
client.onDestroy(() => {
watcher
?.then((watcher) => watcher?.close())
.catch((e) =>
console.error(
'[crash_reporter] FSWatcher failed resoving on destroy:',
e,
),
);
});
return {
reportCrash,
crashes,
selectedCrash,
openInLogs(callstack: string) {
client.selectPlugin('DeviceLogs', callstack);
},
os: client.device.os,
copyCrashToClipboard(callstack: string) {
client.writeTextToClipboard(callstack);
},
createPaste(callstack: string) {
client.createPaste(callstack);
},
isFB: client.isFB,
clearCrashes() {
crashes.set([]);
selectedCrash.set(undefined);
},
};
}
export {Crashes as Component} from './Crashes';