support -vv verbose logging

This commit is contained in:
clowwindy 2014-06-20 21:06:15 +08:00
parent 6c6afde2a5
commit 41010d810e
2 changed files with 18 additions and 7 deletions

View file

@ -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

View file

@ -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+')