diff --git a/app/classes/minecraft/bedrock_ping.py b/app/classes/minecraft/bedrock_ping.py index 556a221a..d2be6449 100644 --- a/app/classes/minecraft/bedrock_ping.py +++ b/app/classes/minecraft/bedrock_ping.py @@ -1,7 +1,12 @@ +from contextlib import redirect_stderr import os import socket import time -import psutil + +from app.classes.shared.null_writer import NullWriter + +with redirect_stderr(NullWriter()): + import psutil class BedrockPing: diff --git a/app/classes/minecraft/stats.py b/app/classes/minecraft/stats.py index 5a36da8c..0f4208a8 100644 --- a/app/classes/minecraft/stats.py +++ b/app/classes/minecraft/stats.py @@ -1,16 +1,20 @@ from __future__ import annotations +from contextlib import redirect_stderr import json import logging import datetime import base64 import typing as t -import psutil +from app.classes.shared.null_writer import NullWriter from app.classes.minecraft.mc_ping import ping from app.classes.models.management import HostStats from app.classes.models.servers import HelperServers from app.classes.shared.helpers import Helpers +with redirect_stderr(NullWriter()): + import psutil + if t.TYPE_CHECKING: from app.classes.shared.main_controller import Controller diff --git a/app/classes/shared/helpers.py b/app/classes/shared/helpers.py index 81606ba3..4ac928b5 100644 --- a/app/classes/shared/helpers.py +++ b/app/classes/shared/helpers.py @@ -16,15 +16,18 @@ import pathlib import ctypes from datetime import datetime from socket import gethostname -from contextlib import suppress -import psutil +from contextlib import redirect_stderr, suppress +from app.classes.shared.null_writer import NullWriter from app.classes.shared.console import Console from app.classes.shared.installer import installer from app.classes.shared.file_helpers import FileHelpers from app.classes.shared.translation import Translation from app.classes.web.websocket_helper import WebSocketHelper +with redirect_stderr(NullWriter()): + import psutil + logger = logging.getLogger(__name__) try: diff --git a/app/classes/shared/null_writer.py b/app/classes/shared/null_writer.py new file mode 100644 index 00000000..55a09460 --- /dev/null +++ b/app/classes/shared/null_writer.py @@ -0,0 +1,10 @@ +import logging +import os + +logger = logging.getLogger(__name__) + + +class NullWriter: + def write(self, data): + if os.environ["CRAFTY_LOG_NULLWRITER"] == "true": + logger.debug(data) diff --git a/app/classes/shared/server.py b/app/classes/shared/server.py index 2700e805..4e2c104f 100644 --- a/app/classes/shared/server.py +++ b/app/classes/shared/server.py @@ -1,3 +1,4 @@ +from contextlib import redirect_stderr import os import re import time @@ -8,8 +9,6 @@ import logging.config import subprocess import html import tempfile -import psutil -from psutil import NoSuchProcess # TZLocal is set as a hidden import on win pipeline from tzlocal import get_localzone @@ -25,6 +24,11 @@ from app.classes.models.server_permissions import PermissionsServers from app.classes.shared.console import Console from app.classes.shared.helpers import Helpers from app.classes.shared.file_helpers import FileHelpers +from app.classes.shared.null_writer import NullWriter + +with redirect_stderr(NullWriter()): + import psutil + from psutil import NoSuchProcess logger = logging.getLogger(__name__)