[tor-commits] [stem/master] More succinct trace level logging

atagar at torproject.org atagar at torproject.org
Mon Aug 14 17:02:10 UTC 2017


commit 245b11bb111161c5105150d4f250ddfbce6de7c0
Author: Damian Johnson <atagar at torproject.org>
Date:   Mon Aug 14 10:00:09 2017 -0700

    More succinct trace level logging
    
    Reading nyx's trace level logs is made quite a bit more annoying due to short
    request/responses being placed on their own lines. Rather, keeping everything
    on one line if the message is short.
---
 docs/change_log.rst |  2 ++
 stem/socket.py      | 16 +++++++++-------
 2 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/docs/change_log.rst b/docs/change_log.rst
index ea353a23..59b546ad 100644
--- a/docs/change_log.rst
+++ b/docs/change_log.rst
@@ -54,8 +54,10 @@ The following are only available within Stem's `git repository
   * Unable to use cookie auth when path includes wide characters (chinese, japanese, etc)
   * Tor change caused :func:`~stem.control.Controller.list_ephemeral_hidden_services` to provide empty strings if unset (:trac:`21329`)
   * Better error message when :func:`~stem.control.Controller.set_conf` fails due to an option being immutable
+  * :func:`~stem.control.Controller.is_geoip_unavailable` now determines if database is available right away
   * Failed to parse torrcs without a port on ipv6 exit policy entries
   * Resilient to 'Tor' prefix in 'GETINFO version' result (:spec:`c5ff1b1`)
+  * More succinct trace level logging
 
  * **Descriptors**
 
diff --git a/stem/socket.py b/stem/socket.py
index 011f35ef..8c3e4205 100644
--- a/stem/socket.py
+++ b/stem/socket.py
@@ -485,7 +485,8 @@ def send_message(control_file, message, raw = False):
     control_file.flush()
 
     log_message = message.replace('\r\n', '\n').rstrip()
-    log.trace('Sent to tor:\n' + log_message)
+    msg_div = '\n' if '\n' in log_message else ' '
+    log.trace('Sent to tor:%s%s' % (msg_div, log_message))
   except socket.error as exc:
     log.info('Failed to send message: %s' % exc)
 
@@ -593,14 +594,15 @@ def recv_message(control_file):
 
       raw_content_str = b''.join(raw_content)
       log_message = stem.util.str_tools._to_unicode(raw_content_str.replace(b'\r\n', b'\n').rstrip())
+      log_message_lines = log_message.split('\n')
 
-      if TRUNCATE_LOGS:
-        log_message_lines = log_message.split('\n')
+      if TRUNCATE_LOGS and len(log_message_lines) > TRUNCATE_LOGS:
+        log_message = '\n'.join(log_message_lines[:TRUNCATE_LOGS] + ['... %i more lines...' % (len(log_message_lines) - TRUNCATE_LOGS)])
 
-        if len(log_message_lines) > TRUNCATE_LOGS:
-          log_message = '\n'.join(log_message_lines[:TRUNCATE_LOGS] + ['... %i more lines...' % (len(log_message_lines) - TRUNCATE_LOGS)])
-
-      log.trace('Received from tor:\n' + log_message)
+      if log_message_lines > 2:
+        log.trace('Received from tor:\n%s' % log_message)
+      else:
+        log.trace('Received from tor: %s' % log_message.replace('\n', '\\n'))
 
       return stem.response.ControlMessage(parsed_content, raw_content_str)
     elif divider == '+':



More information about the tor-commits mailing list