Files
flipper/src/devices/OculusDevice.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

141 lines
3.4 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 {DeviceType, DeviceLogEntry} from './BaseDevice.js';
import fs from 'fs-extra';
import os from 'os';
import path from 'path';
import BaseDevice from './BaseDevice.js';
function getLogsPath(fileName: ?string): string {
const dir = '/AppData/Local/Oculus/';
if (fileName) {
return path.join(os.homedir(), dir, fileName);
}
return path.join(os.homedir(), dir);
}
export default class OculusDevice extends BaseDevice {
supportedPlugins = ['DeviceLogs'];
icon = 'icons/oculus.png';
os = 'Oculus';
watcher: any;
processedFileMap: {};
watchedFile: ?string;
timer: TimeoutID;
constructor(serial: string, deviceType: DeviceType, title: string) {
super(serial, deviceType, title);
this.watcher = null;
this.processedFileMap = {};
this.setupListener();
}
teardown() {
clearTimeout(this.timer);
const file = this.watchedFile;
if (file) {
fs.unwatchFile(path.join(getLogsPath(), file));
}
}
supportedColumns(): Array<string> {
return ['date', 'tag', 'message', 'type', 'time'];
}
mapLogLevel(type: string): $PropertyType<DeviceLogEntry, 'type'> {
switch (type) {
case 'WARNING':
return 'warn';
case '!ERROR!':
return 'error';
case 'DEBUG':
return 'debug';
case 'INFO':
return 'info';
default:
return 'verbose';
}
}
processText(text: Buffer) {
text
.toString()
.split('\r\n')
.forEach(line => {
const regex = /(.*){(\S+)}\s*\[([\w :.\\]+)\](.*)/;
const match = regex.exec(line);
let entry;
if (match && match.length === 5) {
entry = {
tid: 0,
pid: 0,
date: new Date(Date.parse(match[1])),
type: this.mapLogLevel(match[2]),
tag: match[3],
message: match[4],
};
} else if (line.trim() === '') {
// skip
} else {
entry = {
tid: 0,
pid: 0,
date: new Date(),
type: 'verbose',
tag: 'failed-parse',
message: line,
};
}
if (entry) {
this.notifyLogListeners(entry);
}
});
}
async setupListener() {
const files = await fs.readdir(getLogsPath());
this.watchedFile = files
.filter(file => file.startsWith('Service_'))
.sort()
.pop();
this.watch();
this.timer = setTimeout(() => this.checkForNewLog(), 5000);
}
watch() {
const filePath = getLogsPath(this.watchedFile);
fs.watchFile(filePath, async (current, previous) => {
const readLen = current.size - previous.size;
const buffer = new Buffer(readLen);
const fd = await fs.open(filePath, 'r');
await fs.read(fd, buffer, 0, readLen, previous.size);
this.processText(buffer);
});
}
async checkForNewLog() {
const files = await fs.readdir(getLogsPath());
const latestLog = files
.filter(file => file.startsWith('Service_'))
.sort()
.pop();
if (this.watchedFile !== latestLog) {
const oldFilePath = getLogsPath(this.watchedFile);
fs.unwatchFile(oldFilePath);
this.watchedFile = latestLog;
this.watch();
}
this.timer = setTimeout(() => this.checkForNewLog(), 5000);
}
}