[tor-commits] [arm/master] Moving logging functions to a log util

atagar at torproject.org atagar at torproject.org
Mon Jun 23 15:00:33 UTC 2014


commit 51cae9c5b64d134cc1c54f65ec6098f6ea2c0125
Author: Damian Johnson <atagar at torproject.org>
Date:   Sat Jun 21 14:08:25 2014 -0700

    Moving logging functions to a log util
    
    Moving our short aliases like debug() and notice() from arm.util to
    arm.util.log to better namespace them.
---
 arm/starter.py       |   20 ++++++++++----------
 arm/util/__init__.py |   42 ++++--------------------------------------
 arm/util/log.py      |   44 ++++++++++++++++++++++++++++++++++++++++++++
 arm/util/tracker.py  |   22 +++++++++++-----------
 arm/util/ui_tools.py |    6 +++---
 5 files changed, 72 insertions(+), 62 deletions(-)

diff --git a/arm/starter.py b/arm/starter.py
index 2131b8c..ea8e17b 100644
--- a/arm/starter.py
+++ b/arm/starter.py
@@ -25,7 +25,7 @@ import stem
 import stem.util.log
 import stem.util.system
 
-from arm.util import BASE_DIR, init_controller, msg, trace, info, notice, warn, uses_settings
+from arm.util import log, BASE_DIR, init_controller, msg, uses_settings
 
 
 @uses_settings
@@ -132,7 +132,7 @@ def _setup_debug_logging(args):
     except IOError as exc:
       armrc_content = "[unable to read file: %s]" % exc.strerror
 
-  trace(
+  log.trace(
     'debug.header',
     arm_version = arm.__version__,
     stem_version = stem.__version__,
@@ -160,14 +160,14 @@ def _load_user_armrc(path, config):
       chroot = config.get('tor.chroot', '').strip().rstrip(os.path.sep)
 
       if chroot and not os.path.exists(chroot):
-        notice('setup.chroot_doesnt_exist', path = chroot)
+        log.notice('setup.chroot_doesnt_exist', path = chroot)
         config.set('tor.chroot', '')
       else:
         config.set('tor.chroot', chroot)  # use the normalized path
     except IOError as exc:
-      warn('config.unable_to_read_file', error = exc.strerror)
+      log.warn('config.unable_to_read_file', error = exc.strerror)
   else:
-    notice('config.nothing_loaded', path = path)
+    log.notice('config.nothing_loaded', path = path)
 
 
 def _warn_if_root(controller):
@@ -178,10 +178,10 @@ def _warn_if_root(controller):
   tor_user = controller.get_user(None)
 
   if tor_user == 'root':
-    notice('setup.tor_is_running_as_root')
+    log.notice('setup.tor_is_running_as_root')
   elif os.getuid() == 0:
     tor_user = tor_user if tor_user else '<tor user>'
-    notice('setup.arm_is_running_as_root', tor_user = tor_user)
+    log.notice('setup.arm_is_running_as_root', tor_user = tor_user)
 
 
 def _warn_if_unable_to_get_pid(controller):
@@ -193,7 +193,7 @@ def _warn_if_unable_to_get_pid(controller):
   try:
     controller.get_pid()
   except ValueError:
-    warn('setup.unable_to_determine_pid')
+    log.warn('setup.unable_to_determine_pid')
 
 
 @uses_settings
@@ -206,7 +206,7 @@ def _setup_freebsd_chroot(controller, config):
     jail_chroot = stem.util.system.get_bsd_jail_path(controller.get_pid(0))
 
     if jail_chroot and os.path.exists(jail_chroot):
-      info('setup.set_freebsd_chroot', path = jail_chroot)
+      log.info('setup.set_freebsd_chroot', path = jail_chroot)
       config.set('tor.chroot', jail_chroot)
 
 
@@ -218,7 +218,7 @@ def _notify_of_unknown_events():
   missing_events = arm.arguments.missing_event_types()
 
   if missing_events:
-    info('setup.unknown_event_types', event_types = ', '.join(missing_events))
+    log.info('setup.unknown_event_types', event_types = ', '.join(missing_events))
 
 
 @uses_settings
diff --git a/arm/util/__init__.py b/arm/util/__init__.py
index 3853b3d..de40170 100644
--- a/arm/util/__init__.py
+++ b/arm/util/__init__.py
@@ -4,6 +4,7 @@ safely working with curses (hiding some of the gory details).
 """
 
 __all__ = [
+  'log',
   'panel',
   'text_input',
   'tor_config',
@@ -16,7 +17,8 @@ import sys
 
 import stem.connection
 import stem.util.conf
-import stem.util.log
+
+from arm.util import log
 
 TOR_CONTROLLER = None
 BASE_DIR = os.path.sep.join(__file__.split(os.path.sep)[:-2])
@@ -65,41 +67,5 @@ def msg(message, config, **attr):
   try:
     return config.get('msg.%s' % message).format(**attr)
   except:
-    notice('BUG: We attempted to use an undefined string resource (%s)' % message)
+    log.notice('BUG: We attempted to use an undefined string resource (%s)' % message)
     return ''
-
-
-def trace(msg, **attr):
-  _log(stem.util.log.TRACE, msg, **attr)
-
-
-def debug(msg, **attr):
-  _log(stem.util.log.DEBUG, msg, **attr)
-
-
-def info(msg, **attr):
-  _log(stem.util.log.INFO, msg, **attr)
-
-
-def notice(msg, **attr):
-  _log(stem.util.log.NOTICE, msg, **attr)
-
-
-def warn(msg, **attr):
-  _log(stem.util.log.WARN, msg, **attr)
-
-
-def error(msg, **attr):
-  _log(stem.util.log.ERROR, msg, **attr)
-
-
-def _log(runlevel, message, **attr):
-  """
-  Logs the given message, formatted with optional attributes.
-
-  :param stem.util.log.Runlevel runlevel: runlevel at which to log the message
-  :param str message: message handle to log
-  :param dict attr: attributes to format the message with
-  """
-
-  stem.util.log.log(runlevel, msg(message, **attr))
diff --git a/arm/util/log.py b/arm/util/log.py
new file mode 100644
index 0000000..72c1d9d
--- /dev/null
+++ b/arm/util/log.py
@@ -0,0 +1,44 @@
+"""
+Logging utilities, primiarily short aliases for logging a message at various
+runlevels.
+"""
+
+import stem.util.log
+
+import arm.util
+
+
+def trace(msg, **attr):
+  _log(stem.util.log.TRACE, msg, **attr)
+
+
+def debug(msg, **attr):
+  _log(stem.util.log.DEBUG, msg, **attr)
+
+
+def info(msg, **attr):
+  _log(stem.util.log.INFO, msg, **attr)
+
+
+def notice(msg, **attr):
+  _log(stem.util.log.NOTICE, msg, **attr)
+
+
+def warn(msg, **attr):
+  _log(stem.util.log.WARN, msg, **attr)
+
+
+def error(msg, **attr):
+  _log(stem.util.log.ERROR, msg, **attr)
+
+
+def _log(runlevel, message, **attr):
+  """
+  Logs the given message, formatted with optional attributes.
+
+  :param stem.util.log.Runlevel runlevel: runlevel at which to log the message
+  :param str message: message handle to log
+  :param dict attr: attributes to format the message with
+  """
+
+  stem.util.log.log(runlevel, arm.util.msg(message, **attr))
diff --git a/arm/util/tracker.py b/arm/util/tracker.py
index a10d291..e5d819d 100644
--- a/arm/util/tracker.py
+++ b/arm/util/tracker.py
@@ -42,7 +42,7 @@ import threading
 from stem.control import State
 from stem.util import conf, connection, proc, str_tools, system
 
-from arm.util import tor_controller, debug, info, notice
+from arm.util import log, tor_controller
 
 CONFIG = conf.config_dict('arm', {
   'queries.connections.rate': 5,
@@ -459,13 +459,13 @@ class ConnectionTracker(Daemon):
           min_rate += 1  # little extra padding so we don't frequently update this
           self.set_rate(min_rate)
           self._rate_too_low_count = 0
-          debug('tracker.lookup_rate_increased', seconds = "%0.1f" % min_rate)
+          log.debug('tracker.lookup_rate_increased', seconds = "%0.1f" % min_rate)
       else:
         self._rate_too_low_count = 0
 
       return True
     except IOError as exc:
-      info('wrap', text = exc)
+      log.info('wrap', text = exc)
 
       # Fail over to another resolver if we've repeatedly been unable to use
       # this one.
@@ -478,13 +478,13 @@ class ConnectionTracker(Daemon):
           self._failure_count = 0
 
           if self._resolvers:
-            notice(
+            log.notice(
               'tracker.unable_to_use_resolver',
               old_resolver = resolver,
               new_resolver = self._resolvers[0],
             )
           else:
-            notice('tracker.unable_to_use_all_resolvers')
+            log.notice('tracker.unable_to_use_all_resolvers')
 
       return False
 
@@ -576,19 +576,19 @@ class ResourceTracker(Daemon):
           self._use_proc = False
           self._failure_count = 0
 
-          info(
+          log.info(
             'tracker.abort_getting_resources',
             resolver = 'proc',
             response = 'falling back to ps',
             exc = exc,
           )
         else:
-          debug('tracker.unable_to_get_resources', resolver = 'proc', exc = exc)
+          log.debug('tracker.unable_to_get_resources', resolver = 'proc', exc = exc)
       else:
         if self._failure_count >= 3:
           # Give up on further attempts.
 
-          info(
+          log.info(
             'tracker.abort_getting_resources',
             resolver = 'ps',
             response = 'giving up on getting resource usage information',
@@ -597,7 +597,7 @@ class ResourceTracker(Daemon):
 
           self.stop()
         else:
-          debug('tracker.unable_to_get_resources', resolver = 'ps', exc = exc)
+          log.debug('tracker.unable_to_get_resources', resolver = 'ps', exc = exc)
 
       return False
 
@@ -654,9 +654,9 @@ class PortUsageTracker(Daemon):
       self._failure_count += 1
 
       if self._failure_count >= 3:
-        info('tracker.abort_getting_port_usage', exc = exc)
+        log.info('tracker.abort_getting_port_usage', exc = exc)
         self.stop()
       else:
-        debug('tracker.unable_to_get_port_usages', exc = exc)
+        log.debug('tracker.unable_to_get_port_usages', exc = exc)
 
       return False
diff --git a/arm/util/ui_tools.py b/arm/util/ui_tools.py
index 2b24b83..01daffa 100644
--- a/arm/util/ui_tools.py
+++ b/arm/util/ui_tools.py
@@ -7,7 +7,7 @@ import curses
 
 from curses.ascii import isprint
 
-from arm.util import info, msg
+from arm.util import log, msg
 
 from stem.util import conf, enum, system
 
@@ -137,10 +137,10 @@ def _color_attr():
         curses.init_pair(color_pair + 1, foreground_color, background_color)
         color_attr[color_name] = curses.color_pair(color_pair + 1)
 
-      info('setup.color_support_available')
+      log.info('setup.color_support_available')
       COLOR_ATTR = color_attr
     else:
-      info('setup.color_support_unavailable')
+      log.info('setup.color_support_unavailable')
       COLOR_ATTR = DEFAULT_COLOR_ATTR
 
   return COLOR_ATTR





More information about the tor-commits mailing list