Skip to content

Commit edda07f

Browse files
Merge pull request mavlink#370 from qthibeault/main
Replace print statements with logging statements
2 parents 5953922 + 1e8bb6c commit edda07f

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

mavsdk/async_plugin_manager.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# -*- coding: utf-8 -*-
2+
import logging
23
import aiogrpc
34

45

@@ -29,9 +30,12 @@ async def _connect_backend(self):
2930
"{}:{}".format(self.host, self.port)
3031
)
3132

32-
print("Waiting for mavsdk_server to be ready...")
33+
logger = logging.getLogger(__name__)
34+
logger.addHandler(logging.NullHandler()) # Avoid errors when user has not configured logging
35+
36+
logger.debug("Waiting for mavsdk_server to be ready...")
3337
await aiogrpc.channel_ready_future(self._channel)
34-
print("Connected to mavsdk_server!")
38+
logger.debug("Connected to mavsdk_server!")
3539

3640
@property
3741
def channel(self):

mavsdk/system.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# -*- coding: utf-8 -*-
22

3+
import logging
4+
import threading
5+
36
from .async_plugin_manager import AsyncPluginManager
47

58
from . import action
@@ -28,6 +31,16 @@
2831
from . import bin
2932

3033

34+
class _LoggingThread(threading.Thread):
35+
def __init__(self, pipe, log_fn):
36+
super().__init__()
37+
self.pipe = pipe
38+
self.log_fn = log_fn
39+
40+
def run(self):
41+
for line in self.pipe:
42+
self.log_fn(line.decode("utf-8").replace("\n", ""))
43+
3144
class System:
3245
"""
3346
Instantiate a System object, that will serve as a proxy to
@@ -281,8 +294,12 @@ def _start_mavsdk_server(system_address=None,port=50051):
281294
bin_path_and_args.append(system_address)
282295
p = subprocess.Popen(bin_path_and_args,
283296
shell=False,
284-
stdout=subprocess.DEVNULL,
285-
stderr=subprocess.DEVNULL)
297+
stdout=subprocess.PIPE,
298+
stderr=subprocess.STDOUT)
299+
300+
logger = logging.getLogger(__name__)
301+
log_thread = _LoggingThread(p.stdout, logger.debug)
302+
log_thread.start()
286303
except FileNotFoundError:
287304
print("""
288305
This installation does not provide an embedded 'mavsdk_server' binary.

0 commit comments

Comments
 (0)