Merge branch '93-fail-gracefully-when-lttng-is-not-installed' into 'master'

Fail gracefully when trying to trace if LTTng is not installed

Closes #93

See merge request micro-ROS/ros_tracing/ros2_tracing!174
This commit is contained in:
Christophe Bedard 2020-05-14 16:53:18 +00:00
commit 4204ba38c5
3 changed files with 40 additions and 1 deletions

View file

@ -50,7 +50,9 @@ no_lttng:
image: $BASE_IMAGE_ID:$DISTRO-base
script:
- colcon build --symlink-install --event-handlers console_cohesion+ --packages-up-to $PACKAGES_LIST
- . install/setup.sh
- (! ./build/tracetools/status)
- (! ros2 trace)
- colcon test --event-handlers console_cohesion+ --packages-select $PACKAGES_LIST
- colcon test-result --all
<<: *global_artifacts

View file

@ -14,8 +14,9 @@
"""Interface for tracing with LTTng."""
import platform
import subprocess
import sys
from typing import List
from typing import Optional
@ -83,3 +84,35 @@ def lttng_fini(
"""
_lttng.stop(session_name)
_lttng.destroy(session_name)
def is_lttng_installed() -> bool:
"""
Check if LTTng is installed.
It first checks if the OS can support LTTng.
If so, it then simply checks if LTTng is installed using the 'lttng' command.
:return: True if it is installed, False otherwise
"""
message_doc = (
'Cannot trace. See documentation at: '
'https://gitlab.com/micro-ROS/ros_tracing/ros2_tracing'
)
system = platform.system()
if 'Linux' != system:
print(f"System '{system}' does not support LTTng.\n{message_doc}", file=sys.stderr)
return False
try:
process = subprocess.Popen(
['lttng', '--version'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
_, stderr = process.communicate()
if 0 != process.returncode:
raise RuntimeError(stderr.decode())
return True
except Exception as e:
print(f'LTTng not found: {e}\n{message_doc}', file=sys.stderr)
return False

View file

@ -15,6 +15,7 @@
"""Entrypoint/script to setup and start an LTTng tracing session."""
import sys
from typing import List
from tracetools_trace.tools import args
@ -41,6 +42,9 @@ def init(
:param context_names: list of context names to enable
:param display_list: whether to display list(s) of enabled events and context names
"""
if not lttng.is_lttng_installed():
sys.exit(2)
ust_enabled = len(ros_events) > 0
kernel_enabled = len(kernel_events) > 0
if ust_enabled: