41 from datetime
import datetime
42 from dateutil.tz
import tzlocal
43 from diagnostic_msgs.msg
import DiagnosticArray, DiagnosticStatus
48 Base exception class of related errors 55 Errors related to network I/O failures 62 Make sure that master is available 63 :raises: :exc:`ROSTopicException` If unable to successfully communicate with master 66 rosgraph.Master(
'/rostopic').getPid()
68 raise ROSTopicIOException(
"Unable to communicate with master!")
73 DIAG_MSG_LVL_STR_NOCOLOR = {
74 DiagnosticStatus.OK:
' OK ',
75 DiagnosticStatus.WARN:
'WARN ',
76 DiagnosticStatus.ERROR:
'ERROR',
77 DiagnosticStatus.STALE:
'STALE',
80 DIAG_MSG_LVL_STR_COLOR = {
81 DiagnosticStatus.OK:
'\033[92m OK \033[0m',
82 DiagnosticStatus.WARN:
'\033[93mWARN \033[0m',
83 DiagnosticStatus.ERROR:
'\033[91mERROR\033[0m',
84 DiagnosticStatus.STALE:
'\033[94mSTALE\033[0m',
90 rospy.Subscriber(options.topic, DiagnosticArray, self.
_diag_callback, queue_size=1)
94 return '/'.join(name.split(
'/')[:-1])
97 return [self.
_get_ns(s.name)
for s
in statuses]
101 return [s
for s
in statuses
if s.name
not in parent_namespaces]
104 if not self._options.follow:
105 dt = datetime.fromtimestamp(msg.header.stamp.to_sec(), tzlocal())
106 print(
'=====================================================================')
107 print(
'Diagnostics generated on: {}'.format(dt))
108 print(
'---------------------------------------------------------------------')
111 if not self._options.follow:
112 print(
'=====================================================================')
113 rospy.signal_shutdown(
'Run complete')
116 prog = re.compile(self._options.filter)
118 for diag
in sorted(self.
_get_leaf_statuses(msg.status), key=
lambda d: d.level, reverse=
True):
119 if prog.search(diag.name):
124 if self._options.min_level <= diag.level:
125 print(
'[ {} ] {} - {}'.format(self.
_lvl_strings_map[diag.level], diag.name, diag.message))
126 if self._options.detail:
127 dt = datetime.fromtimestamp(ts.to_sec(), tzlocal())
128 print(
' timestamp: {}'.format(dt))
129 print(
' hardware_id: {}'.format(diag.hardware_id))
130 for kv
in diag.values:
131 print(
' - {}: {}'.format(kv.key, kv.value))
135 parser = argparse.ArgumentParser(
136 description=
'ROS Diagnostic Viewer is a command-line tool for printing information about ROS Diagnostics.' 138 parser.add_argument(
'-f',
'--follow', action=
'store_true', dest=
'follow',
139 help=
'follows the diagnostic messages continuously')
141 parser.add_argument(
'-l',
'--level', action=
'store', metavar=
'LEVEL', type=int, default=DiagnosticStatus.WARN,
143 help=
'the minimum diagnostic level to display (OK=0, WARN=1, ERROR=2, STALE=3) [default=1]')
145 parser.add_argument(
'--topic', action=
'store', metavar=
'TOPIC', type=str, default=
'/diagnostics_agg', dest=
'topic',
146 help=
'topic to read the diagnostics from')
148 parser.add_argument(
'--filter', action=
'store', metavar=
'FILTER', type=str, default=
'.*', dest=
'filter',
149 help=
'regular expression to be applied as a filter to the diagnostic name')
151 parser.add_argument(
'--nocolor', action=
'store_true',
152 help=
'output should not make use of any color')
154 parser.add_argument(
'-d',
'--detail', action=
'store_true',
155 help=
'printing the full diagnostic details ')
157 args = parser.parse_args(argv)
160 rospy.init_node(
'rosdiagnostic', anonymous=
True)
170 argv = rospy.myargv(argv)
175 sys.stderr.write(
"Network communication failed. Most likely failed to communicate with master.\n")
177 except rosgraph.MasterException
as e:
179 sys.stderr.write(
"ERROR: %s\n" % str(e))
181 except ROSTopicException
as e:
182 sys.stderr.write(
"ERROR: %s\n" % str(e))
184 except KeyboardInterrupt:
186 except rospy.ROSInterruptException:
def _rosdiagnostic_cmd_echo(argv)
dictionary DIAG_MSG_LVL_STR_COLOR
def _diag_callback(self, msg)
def __init__(self, options)
dictionary DIAG_MSG_LVL_STR_NOCOLOR
def _get_leaf_statuses(self, statuses)
def _get_non_leaf_statuses(self, statuses)
def _print_end_banner(self, msg)
def _print_status(self, diag, ts)
def rosdiagnosticmain(argv=None)
def _print_begin_banner(self, msg)