"Add logging to backend plugin process"

This commit introduces logging to the backend plugin process in the Cura application. The output from the plugin process is now redirected to a log file, which is created in the application's data storage directory. This can be useful for debugging and troubleshooting purposes by providing more transparency about what's happening in the plugin process. A new dependency 'os' has been added to the 'BackendPlugin.py'. Also, the 'Resources' module has been imported from 'UM'. The changes mainly affect the way the 'subproccess.Popen' function is used - its 'stdout' and 'stderr' parameters were adjusted accordingly.

Contributes to CURA-11064
This commit is contained in:
j.spijker@ultimaker.com 2023-09-20 14:37:35 +02:00 committed by Jelle Spijker
parent 7633ebf704
commit a9f2d879f7

View file

@ -1,6 +1,7 @@
# Copyright (c) 2023 Ultimaker B.V. # Copyright (c) 2023 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher. # Cura is released under the terms of the LGPLv3 or higher.
import socket import socket
import os
import subprocess import subprocess
from typing import Optional, List from typing import Optional, List
@ -10,6 +11,7 @@ from UM.Settings.AdditionalSettingDefinitionAppender import AdditionalSettingDef
from UM.PluginObject import PluginObject from UM.PluginObject import PluginObject
from UM.i18n import i18nCatalog from UM.i18n import i18nCatalog
from UM.Platform import Platform from UM.Platform import Platform
from UM.Resources import Resources
class BackendPlugin(AdditionalSettingDefinitionsAppender, PluginObject): class BackendPlugin(AdditionalSettingDefinitionsAppender, PluginObject):
@ -75,10 +77,17 @@ class BackendPlugin(AdditionalSettingDefinitionsAppender, PluginObject):
# STDIN needs to be None because we provide no input, but communicate via a local socket instead. # STDIN needs to be None because we provide no input, but communicate via a local socket instead.
# The NUL device sometimes doesn't exist on some computers. # The NUL device sometimes doesn't exist on some computers.
Logger.info(f"Starting backend_plugin [{self._plugin_id}] with command: {self._validatePluginCommand()}") Logger.info(f"Starting backend_plugin [{self._plugin_id}] with command: {self._validatePluginCommand()}")
popen_kwargs = {"stdin": None } plugin_log_path = os.path.join(Resources.getDataStoragePath(), f"{self.getPluginId()}.log")
if Platform.isWindows(): Logger.info(f"Logging plugin output to: {plugin_log_path}")
popen_kwargs["creationflags"] = subprocess.CREATE_NO_WINDOW with open(plugin_log_path, 'a') as f:
self._process = subprocess.Popen(self._validatePluginCommand(), **popen_kwargs) popen_kwargs = {
"stdin": None,
"stdout": f, # Redirect output to file
"stderr": subprocess.STDOUT, # Combine stderr and stdout
}
if Platform.isWindows():
popen_kwargs["creationflags"] = subprocess.CREATE_NO_WINDOW
self._process = subprocess.Popen(self._validatePluginCommand(), **popen_kwargs)
self._is_running = True self._is_running = True
return True return True
except PermissionError: except PermissionError: