diff --git a/src/ansys/dpf/core/__init__.py b/src/ansys/dpf/core/__init__.py index 08a256525f6..98fdbfb43b3 100644 --- a/src/ansys/dpf/core/__init__.py +++ b/src/ansys/dpf/core/__init__.py @@ -128,6 +128,7 @@ SERVER = None SERVER_CONFIGURATION = None +DEFAULT_SERVER_DEBUG = None _server_instances = [] diff --git a/src/ansys/dpf/core/server_types.py b/src/ansys/dpf/core/server_types.py index 05964171502..6d114d8c3e5 100644 --- a/src/ansys/dpf/core/server_types.py +++ b/src/ansys/dpf/core/server_types.py @@ -34,7 +34,7 @@ import ctypes import io import os -from pathlib import Path +from pathlib import Path, PurePosixPath, PureWindowsPath import socket import subprocess import sys @@ -51,7 +51,7 @@ from ansys.dpf.core._version import min_server_version, server_to_ansys_version from ansys.dpf.core.check_version import get_server_version, meets_version, version_requires from ansys.dpf.core.server_context import AvailableServerContexts, ServerContext -from ansys.dpf.gate import data_processing_grpcapi, load_api +from ansys.dpf.gate import data_processing_capi, data_processing_grpcapi, load_api if TYPE_CHECKING: # pragma: no cover from ansys.dpf.core.server_factory import DockerConfig @@ -445,6 +445,8 @@ def __init__(self): self._info_instance = None self._docker_config = server_factory.RunningDockerConfig() self._server_meet_version = {} + if core.DEFAULT_SERVER_DEBUG is not None: + self.start_debug(folder_path=core.DEFAULT_SERVER_DEBUG) def set_as_global(self, as_global=True): """Set the current server as global if necessary. @@ -709,6 +711,32 @@ def __del__(self): except: warnings.warn(traceback.format_exc()) + def start_debug(self, folder_path: str | Path): + """Start writing server-side debug information within the given folder. + + Parameters + ---------- + folder_path: + Path to a folder server-side where to write debug info. + + """ + folder_path = ( + PurePosixPath(folder_path) if self.os == "posix" else PureWindowsPath(folder_path) + ) + api = self.get_api_for_type( + capi=data_processing_capi.DataProcessingCAPI, + grpcapi=data_processing_grpcapi.DataProcessingGRPCAPI, + ) + api.data_processing_set_debug_trace(text=str(folder_path)) + + def stop_debug(self): + """Stop writing server-side debug information.""" + api = self.get_api_for_type( + capi=data_processing_capi.DataProcessingCAPI, + grpcapi=data_processing_grpcapi.DataProcessingGRPCAPI, + ) + api.data_processing_set_debug_trace(text="") + class CServer(BaseServer, ABC): """Abstract class for servers going through the DPFClientAPI.""" diff --git a/tests/test_server.py b/tests/test_server.py index b7ff9a44591..3b4543409fe 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -179,6 +179,20 @@ def test_server_plugins(self, server_config): assert isinstance(server_plugins, dict) assert "native" in server_plugins.keys() + def test_server_debug(self, server_config, tmp_path): + from pathlib import Path + + server_instance = start_local_server(config=server_config) + server_instance.start_debug(tmp_path / Path("DEBUG_TEST_")) + f = dpf.core.field_from_array([1.0], server=server_instance) + fwd = dpf.core.operators.utility.forward(any=f, server=server_instance) + fwd.run() + server_instance.stop_debug() + debug_folder = sorted(tmp_path.glob("DEBUG_TEST_*")) + assert len(debug_folder) == 1 + init_path = debug_folder[0] / Path("init.log") + assert init_path.exists() + @pytest.mark.skipif( os.name == "posix" or running_docker,