Improve logging for workerpool tasks by providing __repr__
authorMichael Hanselmann <hansmi@google.com>
Wed, 13 Jan 2010 11:08:03 +0000 (12:08 +0100)
committerMichael Hanselmann <hansmi@google.com>
Wed, 13 Jan 2010 13:03:20 +0000 (14:03 +0100)
Before it would log something like “starting task
(<ganeti.http.client._HttpClientPendingRequest object at 0x2aaaad176790>,)”,
which isn't really useful for debugging. Now it'll log “[…]
<ganeti.http.client._HttpClientPendingRequest
req=<ganeti.http.client.HttpClientRequest 172.24.x.y:1811 PUT /node_info at
0x2aaaaab7ed10> at 0x2aaaaab823d0>”.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>

lib/http/client.py
lib/jqueue.py

index d32eb5b..5ca76a1 100644 (file)
@@ -95,6 +95,14 @@ class HttpClientRequest(object):
     self.resp_headers = None
     self.resp_body = None
 
+  def __repr__(self):
+    status = ["%s.%s" % (self.__class__.__module__, self.__class__.__name__),
+              "%s:%s" % (self.host, self.port),
+              self.method,
+              self.path]
+
+    return "<%s at %#x>" % (" ".join(status), id(self))
+
 
 class _HttpClientToServerMessageWriter(http.HttpMessageWriter):
   pass
@@ -328,6 +336,12 @@ class _HttpClientPendingRequest(object):
     # Thread synchronization
     self.done = threading.Event()
 
+  def __repr__(self):
+    status = ["%s.%s" % (self.__class__.__module__, self.__class__.__name__),
+              "req=%r" % self.request]
+
+    return "<%s at %#x>" % (" ".join(status), id(self))
+
 
 class HttpClientWorker(workerpool.BaseWorker):
   """HTTP client worker class.
index f7e0557..f989ef8 100644 (file)
@@ -190,6 +190,13 @@ class _QueuedJob(object):
     # Condition to wait for changes
     self.change = threading.Condition(self.queue._lock)
 
+  def __repr__(self):
+    status = ["%s.%s" % (self.__class__.__module__, self.__class__.__name__),
+              "id=%s" % self.id,
+              "ops=%s" % ",".join([op.input.Summary() for op in self.ops])]
+
+    return "<%s at %#x>" % (" ".join(status), id(self))
+
   @classmethod
   def Restore(cls, queue, state):
     """Restore a _QueuedJob from serialized state: