blob: bcf056ddc69b70807b3b2794db05360d55b7c44d [file]
// Copyright 2024 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import * as vscode from 'vscode';
import { Ffx, FuchsiaDevice } from './ffx';
import { DataStreamProcess } from './process';
export class LogView {
private deviceName?: string;
private deviceConnected?: boolean;
private logProcess?: DataStreamProcess;
public output?: vscode.OutputChannel;
constructor(readonly ffx: Ffx) {
ffx.onSetTarget(target => { this.watch(target); });
}
watch(device: FuchsiaDevice | null) {
const targetDeviceName = device?.nodeName;
const targetDeviceConnected = device?.connected ?? false;
if (targetDeviceName === this.deviceName && targetDeviceConnected === this.deviceConnected) {
// Already watching this device.
return;
}
this.deviceName = targetDeviceName;
this.deviceConnected = targetDeviceConnected;
if (this.logProcess) {
this.logProcess.stop();
this.logProcess = undefined;
}
if (!targetDeviceConnected) {
if (this.output) {
this.output.appendLine('=== Device disconnected ===');
} else {
this.createOutputChannel();
this.output!.appendLine('=== No device connected ===');
}
return;
}
if (this.output) {
this.output.clear();
} else {
this.createOutputChannel();
}
const handleData = (buffer: Buffer) => {
const messages = new TextDecoder().decode(buffer);
this.output?.append(messages);
};
const args = ['log', '--no-color'];
this.logProcess = this.ffx.runFfxAsync(args, handleData, handleData, this.deviceName);
}
createOutputChannel() {
this.output = vscode.window.createOutputChannel('Fuchsia Logs', 'fuchsia-log');
}
}