Files
flipper/src/devices/BaseDevice.js
Daniel Büchele afdc846a8b log listener
Summary:
The logs plugin opened a new log connection every time it was activated and never closed the connection.

This is now changed. Once a device is connected, a log connection is opened. The logs plugin subscribes and unsubscribes to this connection. This allows the logs plugin it even access the logs from when it was not activated and ensures to only open on connection to read the logs. Logs are persisted when switching away from the plugin.

Also removes the spinner from the logs plugin, as it loads much faster now.

Reviewed By: jknoxville

Differential Revision: D9613054

fbshipit-source-id: e37ea56c563450e7fc4e3c85a015292be1f2dbfc
2018-08-31 10:13:06 -07:00

108 lines
2.3 KiB
JavaScript

/**
* Copyright 2018-present Facebook.
* 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 stream from 'stream';
import {SonarDevicePlugin} from 'sonar';
export type LogLevel =
| 'unknown'
| 'verbose'
| 'debug'
| 'info'
| 'warn'
| 'error'
| 'fatal';
export type DeviceLogEntry = {|
date: Date,
pid: number,
tid: number,
app?: string,
type: LogLevel,
tag: string,
message: string,
|};
export type DeviceShell = {
stdout: stream.Readable,
stderr: stream.Readable,
stdin: stream.Writable,
};
export type DeviceLogListener = (entry: DeviceLogEntry) => void;
export type DeviceType = 'emulator' | 'physical';
export default class BaseDevice {
constructor(serial: string, deviceType: DeviceType, title: string) {
this.serial = serial;
this.title = title;
this.deviceType = deviceType;
}
// operating system of this device
os: string;
// human readable name for this device
title: string;
// type of this device
deviceType: DeviceType;
// serial number for this device
serial: string;
// supported device plugins for this platform
supportedPlugins: Array<string> = [];
// possible src of icon to display next to the device title
icon: ?string;
logListeners: Map<Symbol, DeviceLogListener> = new Map();
logEntries: Array<DeviceLogEntry> = [];
supportsOS(os: string) {
return os.toLowerCase() === this.os.toLowerCase();
}
supportsPlugin = (DevicePlugin: Class<SonarDevicePlugin<>>): boolean => {
return this.supportedPlugins.includes(DevicePlugin.id);
};
toJSON() {
return `<${this.constructor.name}#${this.title}>`;
}
teardown() {}
supportedColumns(): Array<string> {
throw new Error('unimplemented');
}
addLogListener(callback: DeviceLogListener): Symbol {
const id = Symbol();
this.logListeners.set(id, callback);
this.logEntries.map(callback);
return id;
}
notifyLogListeners(entry: DeviceLogEntry) {
this.logEntries.push(entry);
if (this.logListeners.size > 0) {
this.logListeners.forEach(listener => listener(entry));
}
}
removeLogListener(id: Symbol) {
this.logListeners.delete(id);
}
spawnShell(): DeviceShell {
throw new Error('unimplemented');
}
}