Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify logging of mavsdk_server output #586

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 5 additions & 20 deletions mavsdk/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,6 @@

from . import bin


class _LoggingThread(threading.Thread):
def __init__(self, pipe, log_fn):
super().__init__()
self.pipe = pipe
self.log_fn = log_fn

def run(self):
for line in self.pipe:
self.log_fn(line.decode("utf-8").replace("\n", ""))

class System:
"""
Instantiate a System object, that will serve as a proxy to
Expand Down Expand Up @@ -85,7 +74,7 @@ def __init__(self, mavsdk_server_address=None, port=50051, sysid=245, compid=190
def __del__(self):
self._stop_mavsdk_server()

async def connect(self, system_address=None):
async def connect(self, system_address=None, verbose=True):
"""
Connect the System object to a remote system.

Expand All @@ -111,7 +100,7 @@ async def connect(self, system_address=None):

if self._mavsdk_server_address is None:
self._mavsdk_server_address = 'localhost'
self._server_process = self._start_mavsdk_server(system_address,self._port, self._sysid, self._compid)
self._server_process = self._start_mavsdk_server(system_address,self._port, self._sysid, self._compid, verbose)

await self._init_plugins(self._mavsdk_server_address, self._port)

Expand Down Expand Up @@ -352,7 +341,7 @@ def tune(self) -> tune.Tune:
return self._plugins["tune"]

@staticmethod
def _start_mavsdk_server(system_address, port, sysid, compid):
def _start_mavsdk_server(system_address, port, sysid, compid, verbose=True):
"""
Starts the gRPC server in a subprocess, listening on localhost:port
port parameter can be specified now to allow multiple mavsdk servers to be spawned via code
Expand Down Expand Up @@ -382,12 +371,8 @@ def _start_mavsdk_server(system_address, port, sysid, compid):
bin_path_and_args.append(system_address)
p = subprocess.Popen(bin_path_and_args,
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)

logger = logging.getLogger(__name__)
log_thread = _LoggingThread(p.stdout, logger.debug)
log_thread.start()
stdout=subprocess.DEVNULL if not verbose else None,
stderr=subprocess.DEVNULL if not verbose else None)
except FileNotFoundError:
print("""
This installation does not provide an embedded 'mavsdk_server' binary.
Expand Down