| // 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) { |
| let targetDeviceName = device?.nodeName; |
| let 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(); |
| } |
| |
| let self = this; |
| |
| /** |
| * handle stdout and stderr output from the log process |
| * The data is appended to the output channel. |
| * @param buffer the data received from the log process |
| */ |
| function handleData(buffer: Buffer) { |
| const messages = new TextDecoder().decode(buffer); |
| self.output?.append(messages); |
| } |
| |
| let args = ['log', '--no-color']; |
| if (this.deviceName) { |
| args = ['--target', this.deviceName, ...args]; |
| } |
| this.logProcess = this.ffx.runFfxAsync(args, handleData, handleData); |
| } |
| |
| createOutputChannel() { |
| this.output = vscode.window.createOutputChannel('Fuchsia Logs', 'fuchsia-log'); |
| } |
| } |