changeset 2417:192ae573901a

jp (debug): new monitor command to show pretty formatted XML stream
author Goffi <goffi@goffi.org>
date Sat, 04 Nov 2017 22:11:45 +0100
parents e2cbd449c002
children 69f979adb1d7
files frontends/src/jp/cmd_debug.py
diffstat 1 files changed, 44 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/jp/cmd_debug.py	Sat Nov 04 22:11:01 2017 +0100
+++ b/frontends/src/jp/cmd_debug.py	Sat Nov 04 22:11:45 2017 +0100
@@ -21,6 +21,7 @@
 import base
 from sat.core.i18n import _
 from sat_frontends.jp.constants import Const as C
+from sat.tools.common.ansi import ANSI as A
 import json
 
 __commands__ = ["Debug"]
@@ -101,8 +102,50 @@
         super(Bridge, self).__init__(host, 'bridge', use_profile=False, help=_('bridge s(t)imulation'))
 
 
+class Monitor(base.CommandBase):
+
+    def __init__(self, host):
+        super(Monitor, self).__init__(host,
+                                      'monitor',
+                                      use_verbose=True,
+                                      use_profile=False,
+                                      use_output=C.OUTPUT_XML,
+                                      help=_('monitor XML stream'))
+        self.need_loop = True
+
+    def add_parser_options(self):
+        self.parser.add_argument("-d", "--direction", choices=('in', 'out', 'both'), default='both', help=_(u"stream direction filter"))
+
+    def printXML(self, direction, xml_data, profile):
+        if self.args.direction == 'in' and direction != 'IN':
+            return
+        if self.args.direction == 'out' and direction != 'OUT':
+            return
+        verbosity = self.host.verbosity
+        if not xml_data.strip():
+            if verbosity <= 2:
+                return
+            whiteping = True
+        else:
+            whiteping = False
+
+        if verbosity:
+            profile_disp = u' ({})'.format(profile) if verbosity>1 else u''
+            if direction == 'IN':
+                self.disp(A.color(A.BOLD, A.FG_YELLOW, '<<<===== IN ====', A.FG_WHITE, profile_disp))
+            else:
+                self.disp(A.color(A.BOLD, A.FG_CYAN, '==== OUT ====>>>', A.FG_WHITE, profile_disp))
+        if whiteping:
+            self.disp('[WHITESPACE PING]')
+        else:
+            self.output(xml_data)
+
+    def start(self):
+        self.host.bridge.register_signal('xmlLog', self.printXML, 'plugin')
+
+
 class Debug(base.CommandBase):
-    subcommands = (Bridge,)
+    subcommands = (Bridge, Monitor)
 
     def __init__(self, host):
         super(Debug, self).__init__(host, 'debug', use_profile=False, help=_('debugging tools'))