diff --git a/pkgs/clan-vm-manager/clan_vm_manager/models/use_vms.py b/pkgs/clan-vm-manager/clan_vm_manager/models/use_vms.py index 4be7aef6..ce77770b 100644 --- a/pkgs/clan-vm-manager/clan_vm_manager/models/use_vms.py +++ b/pkgs/clan-vm-manager/clan_vm_manager/models/use_vms.py @@ -1,5 +1,6 @@ import tempfile import weakref +from collections.abc import Generator from pathlib import Path from typing import Any, ClassVar @@ -110,9 +111,10 @@ class VM(GObject.Object): threading.Thread(target=self.__start).start() + self.connect("vm_status_changed", self._start_logs_task) + # Every 50ms check if the VM is still running self._watcher_id = GLib.timeout_add(50, self._vm_watcher_task) - if self._watcher_id == 0: log.error("Failed to add watcher") raise ClanError("Failed to add watcher") @@ -130,6 +132,21 @@ class VM(GObject.Object): return GLib.SOURCE_CONTINUE + def _start_logs_task(self, obj: Any, vm: Any, _vm: Any) -> None: + print("Starting log task") + self._logs_id = GLib.timeout_add(50, self._get_logs_task) + + def _get_logs_task(self) -> bool: + if not self.process.out_file.exists(): + log.error(f"Log file {self.process.out_file} does not exist") + return GLib.SOURCE_REMOVE + if not self.is_running(): + log.info("VM is not running") + return GLib.SOURCE_REMOVE + + print(self.read_whole_log()) + return GLib.SOURCE_CONTINUE + def is_running(self) -> bool: return self.process.proc.is_alive() @@ -144,7 +161,16 @@ class VM(GObject.Object): self.process.kill_group() - def read_log(self) -> str: + def read_line_log(self) -> Generator[str, None, None]: + with open(self.process.out_file) as f: + while True: + line = f.readline() + if not line: + break + yield line + return None + + def read_whole_log(self) -> str: if not self.process.out_file.exists(): log.error(f"Log file {self.process.out_file} does not exist") return "" diff --git a/pkgs/clan-vm-manager/clan_vm_manager/views/list.py b/pkgs/clan-vm-manager/clan_vm_manager/views/list.py index a14f9953..888882b6 100644 --- a/pkgs/clan-vm-manager/clan_vm_manager/views/list.py +++ b/pkgs/clan-vm-manager/clan_vm_manager/views/list.py @@ -211,5 +211,6 @@ class ClanList(Gtk.Box): print("VM exited with code:", exitc) if not vm.is_running() and exitc != 0: print("VM exited with error. Exitcode:", exitc) - print(vm.read_log()) + print("==========VM LOGS=========") + print(vm.read_whole_log()) # self.show_error_dialog(vm.read_log())