PROTON-882: allow python to set transport tracer

Project: http://git-wip-us.apache.org/repos/asf/qpid-proton/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton/commit/223bbc8b
Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton/tree/223bbc8b
Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton/diff/223bbc8b

Branch: refs/heads/rajith-codec
Commit: 223bbc8bd50a34282ef02952392fd4321113f770
Parents: beaea0c
Author: Rafael Schloming <[email protected]>
Authored: Fri May 8 09:21:54 2015 -0400
Committer: Rafael Schloming <[email protected]>
Committed: Fri May 8 09:21:54 2015 -0400

----------------------------------------------------------------------
 proton-c/bindings/python/cproton.i          | 36 ++++++++++++++++++++++++
 proton-c/bindings/python/proton/__init__.py | 26 +++++++++++++++++
 proton-j/src/main/resources/cengine.py      |  3 ++
 tests/python/proton_tests/transport.py      | 15 ++++++++++
 4 files changed, 80 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/223bbc8b/proton-c/bindings/python/cproton.i
----------------------------------------------------------------------
diff --git a/proton-c/bindings/python/cproton.i 
b/proton-c/bindings/python/cproton.i
index 70d6c42..357cd82 100644
--- a/proton-c/bindings/python/cproton.i
+++ b/proton-c/bindings/python/cproton.i
@@ -291,6 +291,42 @@ int pn_ssl_get_peer_hostname(pn_ssl_t *ssl, char *OUTPUT, 
size_t *OUTPUT_SIZE);
     SWIG_PYTHON_THREAD_END_BLOCK;
     return chandler;
   }
+
+  PN_HANDLE(PNI_PYTRACER)
+
+  void pn_pytracer(pn_transport_t *transport, const char *message) {
+    PyObject *pytracer = (PyObject *) 
pn_record_get(pn_transport_attachments(transport), PNI_PYTRACER);
+    SWIG_PYTHON_THREAD_BEGIN_BLOCK;
+    PyObject *pytrans = SWIG_NewPointerObj(transport, 
SWIGTYPE_p_pn_transport_t, 0);
+    PyObject *pymsg = PyString_FromString(message);
+    PyObject *result = PyObject_CallFunctionObjArgs(pytracer, pytrans, pymsg, 
NULL);
+    if (!result) {
+      PyErr_PrintEx(true);
+    }
+    Py_XDECREF(pytrans);
+    Py_XDECREF(pymsg);
+    Py_XDECREF(result);
+    SWIG_PYTHON_THREAD_END_BLOCK;
+  }
+
+  void pn_transport_set_pytracer(pn_transport_t *transport, PyObject *obj) {
+    pn_record_t *record = pn_transport_attachments(transport);
+    pn_record_def(record, PNI_PYTRACER, PN_PYREF);
+    pn_record_set(record, PNI_PYTRACER, obj);
+    pn_transport_set_tracer(transport, pn_pytracer);
+  }
+
+  PyObject *pn_transport_get_pytracer(pn_transport_t *transport) {
+    pn_record_t *record = pn_transport_attachments(transport);
+    PyObject *obj = pn_record_get(record, PNI_PYTRACER);
+    if (obj) {
+      Py_XINCREF(obj);
+      return obj;
+    } else {
+      Py_RETURN_NONE;
+    }
+  }
+
 %}
 
 %include "proton/cproton.i"

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/223bbc8b/proton-c/bindings/python/proton/__init__.py
----------------------------------------------------------------------
diff --git a/proton-c/bindings/python/proton/__init__.py 
b/proton-c/bindings/python/proton/__init__.py
index 43fd332..bc639e3 100644
--- a/proton-c/bindings/python/proton/__init__.py
+++ b/proton-c/bindings/python/proton/__init__.py
@@ -3142,6 +3142,14 @@ class Delivery(Wrapper):
 class TransportException(ProtonException):
   pass
 
+class TraceAdapter:
+
+  def __init__(self, tracer):
+    self.tracer = tracer
+
+  def __call__(self, trans_impl, message):
+    self.tracer(Transport.wrap(trans_impl), message)
+
 class Transport(Wrapper):
 
   TRACE_OFF = PN_TRACE_OFF
@@ -3179,6 +3187,24 @@ class Transport(Wrapper):
     else:
       return err
 
+  def _set_tracer(self, tracer):
+    pn_transport_set_pytracer(self._impl, TraceAdapter(tracer));
+
+  def _get_tracer(self):
+    adapter = pn_transport_get_pytracer(self._impl)
+    if adapter:
+      return adapter.tracer
+    else:
+      return None
+
+  tracer = property(_get_tracer, _set_tracer,
+                            doc="""
+A callback for trace logging. The callback is passed the transport and log 
message.
+""")
+
+  def log(self, message):
+    pn_transport_log(self._impl, message)
+
   def require_auth(self, bool):
     pn_transport_require_auth(self._impl, bool)
 

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/223bbc8b/proton-j/src/main/resources/cengine.py
----------------------------------------------------------------------
diff --git a/proton-j/src/main/resources/cengine.py 
b/proton-j/src/main/resources/cengine.py
index 6c45f33..2d82bc4 100644
--- a/proton-j/src/main/resources/cengine.py
+++ b/proton-j/src/main/resources/cengine.py
@@ -868,6 +868,9 @@ class pn_transport_wrapper:
 def pn_transport():
   return wrap(Proton.transport(), pn_transport_wrapper)
 
+def pn_transport_get_pytracer(trans):
+  raise Skipped()
+
 def pn_transport_attachments(trans):
   return trans.attachments
 

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/223bbc8b/tests/python/proton_tests/transport.py
----------------------------------------------------------------------
diff --git a/tests/python/proton_tests/transport.py 
b/tests/python/proton_tests/transport.py
index febb029..e56b122 100644
--- a/tests/python/proton_tests/transport.py
+++ b/tests/python/proton_tests/transport.py
@@ -187,3 +187,18 @@ class TransportTest(Test):
 
     # server closed
     assert srv.pending() < 0
+
+class LogTest(Test):
+
+  def testTracer(self):
+    t = Transport()
+    assert t.tracer is None
+    messages = []
+    def tracer(transport, message):
+      messages.append((transport, message))
+    t.tracer = tracer
+    assert t.tracer is tracer
+    t.log("one")
+    t.log("two")
+    t.log("three")
+    assert messages == [(t, "one"), (t, "two"), (t, "three")], messages


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to