Patchwork [python-hglib] Add the abilty to trace the protocol between the client and server

login
register
mail settings
Submitter Barry A. Scott
Date Oct. 18, 2016, 4:51 p.m.
Message ID <9855e0c3aac7600f82cf.1476809507@varric.chelsea.private>
Download mbox | patch
Permalink /patch/17172/
State Accepted
Headers show

Comments

Barry A. Scott - Oct. 18, 2016, 4:51 p.m.
# HG changeset patch
# User Barry A. Scott <barry@barrys-emacs.org>
# Date 1476809117 -3600
#      Tue Oct 18 17:45:17 2016 +0100
# Node ID 9855e0c3aac7600f82cf1596082a0e7f955360f9
# Parent  6f15cb7cc9cb4427f35c60080f85dbf4ca5abd10
Add the abilty to trace the protocol between the client and server.

This is useful when debugging issues with driving hg via hglib
where output and error messages can be lost.

Call setprotocoltrace with the name of a trace function or None.
If the trace function is None no tracing is done.

The trace function is called with the direction, the channel-identified
and its data.
Yuya Nishihara - Oct. 22, 2016, 10:04 a.m.
On Tue, 18 Oct 2016 17:51:47 +0100, Barry A. Scott wrote:
> # HG changeset patch
> # User Barry A. Scott <barry@barrys-emacs.org>
> # Date 1476809117 -3600
> #      Tue Oct 18 17:45:17 2016 +0100
> # Node ID 9855e0c3aac7600f82cf1596082a0e7f955360f9
> # Parent  6f15cb7cc9cb4427f35c60080f85dbf4ca5abd10
> Add the abilty to trace the protocol between the client and server.

"client: add the abilty to trace the protocol between the client and server"

The code looks good to me. Marked as pre-reviewed, thanks.

Patch

diff -r 6f15cb7cc9cb -r 9855e0c3aac7 hglib/client.py
--- a/hglib/client.py	Mon Jul 18 23:40:45 2016 -0500
+++ b/hglib/client.py	Tue Oct 18 17:45:17 2016 +0100
@@ -62,6 +62,18 @@ 
         if connect:
             self.open()
 
+        self._protocoltracefn = None
+
+    def setprotocoltrace(self, tracefn=None):
+        """
+        if tracefn is None no trace calls will be made.
+        Otherwise tracefn is call as tracefn( direction, channel, data )
+        direction is 'r' for read from server and 'w' for write to server
+        channel is always None when direction is 'w'
+        and the channel-identified when the direction is 'r'
+        """
+        self._protocoltracefn = tracefn
+
     def __enter__(self):
         if self.server is None:
             self.open()
@@ -119,6 +131,8 @@ 
 
     def runcommand(self, args, inchannels, outchannels):
         def writeblock(data):
+            if self._protocoltracefn is not None:
+                self._protocoltracefn('w', None, data)
             self.server.stdin.write(struct.pack(self.inputfmt, len(data)))
             self.server.stdin.write(data)
             self.server.stdin.flush()
@@ -131,6 +145,8 @@ 
 
         while True:
             channel, data = self._readchannel()
+            if self._protocoltracefn is not None:
+                self._protocoltracefn('r', channel, data)
 
             # input channels
             if channel in inchannels: