From 41010d810efc40b9a6a89742842ed688c3e70fb0 Mon Sep 17 00:00:00 2001 From: clowwindy Date: Fri, 20 Jun 2014 21:06:15 +0800 Subject: [PATCH] support -vv verbose logging --- shadowsocks/tcprelay.py | 9 +++++---- shadowsocks/utils.py | 16 +++++++++++++--- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/shadowsocks/tcprelay.py b/shadowsocks/tcprelay.py index 77ff8cd..cfab5f2 100644 --- a/shadowsocks/tcprelay.py +++ b/shadowsocks/tcprelay.py @@ -30,6 +30,7 @@ import traceback import random import encrypt import eventloop +import utils from common import parse_header @@ -559,7 +560,7 @@ class TCPRelay(object): # we just need a sorted last_activity queue and it's faster than heapq # in fact we can do O(1) insertion/remove so we invent our own if self._timeouts: - logging.debug('sweeping timeouts') + logging.log(utils.VERBOSE_LEVEL, 'sweeping timeouts') now = time.time() length = len(self._timeouts) pos = self._timeout_offset @@ -590,9 +591,9 @@ class TCPRelay(object): def _handle_events(self, events): for sock, fd, event in events: - # if sock: - # logging.debug('fd %d %s', fd, - # eventloop.EVENT_NAMES.get(event, event)) + if sock: + logging.log(utils.VERBOSE_LEVEL, 'fd %d %s', fd, + eventloop.EVENT_NAMES.get(event, event)) if sock == self._server_socket: if event & eventloop.POLL_ERR: # TODO diff --git a/shadowsocks/utils.py b/shadowsocks/utils.py index ed95443..c1d29e6 100644 --- a/shadowsocks/utils.py +++ b/shadowsocks/utils.py @@ -28,6 +28,9 @@ import getopt import logging +VERBOSE_LEVEL = 5 + + def check_python(): info = sys.version_info if not (info[0] == 2 and info[1] >= 6): @@ -106,6 +109,7 @@ def get_config(is_local): config = {} optlist, args = getopt.getopt(sys.argv[1:], shortopts, longopts) + v_count = 0 for key, value in optlist: if key == '-p': config['server_port'] = int(value) @@ -120,7 +124,10 @@ def get_config(is_local): elif key == '-b': config['local_address'] = value elif key == '-v': - config['verbose'] = True + v_count += 1 + print v_count + # '-vv' turns on more verbose mode + config['verbose'] = v_count elif key == '-t': config['timeout'] = int(value) elif key == '--fast-open': @@ -148,11 +155,14 @@ def get_config(is_local): config['verbose'] = config.get('verbose', False) config['local_address'] = config.get('local_address', '127.0.0.1') - if config['verbose']: + logging.getLogger('').handlers = [] + logging.addLevelName(VERBOSE_LEVEL, 'VERBOSE') + if config['verbose'] == 2: + level = VERBOSE_LEVEL + elif config['verbose']: level = logging.DEBUG else: level = logging.INFO - logging.getLogger('').handlers = [] logging.basicConfig(level=level, format='%(asctime)s %(levelname)-8s %(message)s', datefmt='%Y-%m-%d %H:%M:%S', filemode='a+')