rosconsole_overlay_text.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import re
4 
5 from jsk_rviz_plugins.msg import OverlayText
6 from rosgraph_msgs.msg import Log
7 import rospy
8 
9 
10 def colored_message(msg):
11  cmsg = msg.msg
12  cmsg = re.sub(r'\x1b\[31m', '<span style="color: red">', cmsg)
13  cmsg = re.sub(r'\x1b\[32m', '<span style="color: green">', cmsg)
14  cmsg = re.sub(r'\x1b\[33m', '<span style="color: yellow">', cmsg)
15  cmsg = re.sub(r'\x1b\[34m', '<span style="color: blue">', cmsg)
16  cmsg = re.sub(r'\x1b\[35m', '<span style="color: purple">', cmsg)
17  cmsg = re.sub(r'\x1b\[36m', '<span style="color: cyan">', cmsg)
18  cmsg = re.sub(r'\x1b\[0m', '</span>', cmsg)
19  if msg.level == Log.DEBUG:
20  return '<span style="color: rgb(120,120,120);">%s</span>' % cmsg
21  elif msg.level == Log.INFO:
22  return '<span style="color: white;">%s</span>' % cmsg
23  elif msg.level == Log.WARN:
24  return '<span style="color: yellow;">%s</span>' % cmsg
25  elif msg.level == Log.ERROR:
26  return '<span style="color: red;">%s</span>' % cmsg
27  elif msg.level == Log.FATAL:
28  return '<span style="color: red;">%s</span>' % cmsg
29 
30 
31 def callback(msg):
32  for exclude_regex in exclude_regexes:
33  if re.match(exclude_regex, msg.msg):
34  return
35 
36  global lines
37  if msg.name not in ignore_nodes:
38  if msg.name in nodes or len(nodes) == 0:
39  if len(nodes_regexp) == 0 or nodes_regexp_compiled.match(msg.name):
40  if reverse_lines:
41  lines = [colored_message(msg)] + lines
42  if len(lines) > line_buffer_length:
43  lines = lines[0:line_buffer_length]
44  else:
45  lines = lines + [colored_message(msg)]
46  if len(lines) > line_buffer_length:
47  lines = lines[-line_buffer_length:]
48  text = OverlayText()
49  text.left = 20
50  text.top = 20
51  text.width = 1200
52  text.height = 1200
53  text.fg_color.a = 1.0
54  text.fg_color.r = 0.3
55  text.text_size = 12
56  text.text = "\n".join(lines)
57  pub.publish(text)
58 
59 
60 if __name__ == "__main__":
61  rospy.init_node("rosconsole_overlay_text")
62  nodes = rospy.get_param("~nodes", [])
63  nodes_regexp = rospy.get_param("~nodes_regexp", "")
64  if nodes_regexp:
65  nodes_regexp_compiled = re.compile(nodes_regexp)
66  ignore_nodes = rospy.get_param("~ignore_nodes", [])
67  exclude_regexes = rospy.get_param("~exclude_regexes", [])
68  line_buffer_length = rospy.get_param("~line_buffer_length", 100)
69  reverse_lines = rospy.get_param("~reverse_lines", True)
70  lines = []
71  sub = rospy.Subscriber("/rosout", Log, callback)
72  pub = rospy.Publisher("~output", OverlayText, queue_size=1)
73  rospy.spin()


jsk_rviz_plugins
Author(s): Kei Okada , Yohei Kakiuchi , Shohei Fujii , Ryohei Ueda
autogenerated on Thu Jun 1 2023 02:45:58