implement utils.print_exception()

Previously we used logging.error(e) and traceback.print_exc()
to output error stack trace. The problem is, we want to
output the stack trace only when verbose > 0. The if statement
scattered around the code. So we replaced them with the new
utils.print_exception() call.
This commit is contained in:
clowwindy 2015-02-10 17:16:24 +08:00
parent 48ddc1714b
commit cb7062e1c1
8 changed files with 32 additions and 29 deletions

View file

@ -25,7 +25,7 @@ import struct
import re
import logging
from shadowsocks import common, lru_cache, eventloop
from shadowsocks import common, lru_cache, eventloop, utils
CACHE_SWEEP_INTERVAL = 30
@ -221,9 +221,7 @@ def parse_response(data):
response.answers.append((an[1], an[2], an[3]))
return response
except Exception as e:
import traceback
traceback.print_exc()
logging.error(e)
utils.print_exception(e)
return None

View file

@ -23,7 +23,7 @@ import sys
import logging
import signal
import time
from shadowsocks import common
from shadowsocks import common, utils
# this module is ported from ShadowVPN daemon.c
@ -58,7 +58,7 @@ def write_pid_file(pid_file, pid):
fd = os.open(pid_file, os.O_RDWR | os.O_CREAT,
stat.S_IRUSR | stat.S_IWUSR)
except OSError as e:
logging.error(e)
utils.print_exception(e)
return -1
flags = fcntl.fcntl(fd, fcntl.F_GETFD)
assert flags != -1
@ -127,7 +127,7 @@ def daemon_start(pid_file, log_file):
freopen(log_file, 'a', sys.stdout)
freopen(log_file, 'a', sys.stderr)
except IOError as e:
logging.error(e)
utils.print_exception(e)
sys.exit(1)
@ -140,7 +140,7 @@ def daemon_stop(pid_file):
if not buf:
logging.error('not running')
except IOError as e:
logging.error(e)
utils.print_exception(e)
if e.errno == errno.ENOENT:
# always exit 0 if we are sure daemon is not running
logging.error('not running')
@ -155,7 +155,7 @@ def daemon_stop(pid_file):
logging.error('not running')
# always exit 0 if we are sure daemon is not running
return
logging.error(e)
utils.print_exception(e)
sys.exit(1)
else:
logging.error('pid is not positive: %d', pid)

View file

@ -28,6 +28,8 @@ import errno
import logging
from collections import defaultdict
from shadowsocks import utils
__all__ = ['EventLoop', 'POLL_NULL', 'POLL_IN', 'POLL_OUT', 'POLL_ERR',
'POLL_HUP', 'POLL_NVAL', 'EVENT_NAMES']
@ -223,9 +225,7 @@ class EventLoop(object):
try:
handler(events)
except (OSError, IOError) as e:
logging.error(e)
import traceback
traceback.print_exc()
utils.print_exception(e)
if self._handlers_to_remove:
for handler in self._handlers_to_remove:
self._handlers.remove(handler)

View file

@ -65,10 +65,7 @@ def main():
daemon.set_user(config.get('user', None))
loop.run()
except Exception as e:
logging.error(e)
if config['verbose']:
import traceback
traceback.print_exc()
utils.print_exception(e)
sys.exit(1)
if __name__ == '__main__':

View file

@ -80,10 +80,7 @@ def main():
daemon.set_user(config.get('user', None))
loop.run()
except Exception as e:
logging.error(e)
if config['verbose']:
import traceback
traceback.print_exc()
utils.print_exception(e)
sys.exit(1)
if int(config['workers']) > 1:

View file

@ -203,9 +203,7 @@ class TCPRelayHandler(object):
errno.EWOULDBLOCK):
uncomplete = True
else:
logging.error(e)
if self._config['verbose']:
traceback.print_exc()
utils.print_exception(e)
self.destroy()
return False
if uncomplete:
@ -259,7 +257,7 @@ class TCPRelayHandler(object):
self._config['fast_open'] = False
self.destroy()
else:
logging.error(e)
utils.print_exception(e)
if self._config['verbose']:
traceback.print_exc()
self.destroy()
@ -383,7 +381,7 @@ class TCPRelayHandler(object):
self._update_stream(STREAM_DOWN, WAIT_STATUS_READING)
return
except Exception as e:
logging.error(e)
utils.print_exception(e)
if self._config['verbose']:
traceback.print_exc()
self.destroy()
@ -445,7 +443,7 @@ class TCPRelayHandler(object):
try:
self._write_to_sock(data, self._local_sock)
except Exception as e:
logging.error(e)
utils.print_exception(e)
if self._config['verbose']:
traceback.print_exc()
# TODO use logging when debug completed
@ -683,7 +681,7 @@ class TCPRelay(object):
errno.EWOULDBLOCK):
continue
else:
logging.error(e)
utils.print_exception(e)
if self._config['verbose']:
traceback.print_exc()
else:

View file

@ -69,7 +69,7 @@ import struct
import errno
import random
from shadowsocks import encrypt, eventloop, lru_cache, common
from shadowsocks import encrypt, eventloop, lru_cache, common, utils
from shadowsocks.common import parse_header, pack_addr
@ -208,7 +208,7 @@ class UDPRelay(object):
if err in (errno.EINPROGRESS, errno.EAGAIN):
pass
else:
logging.error(e)
utils.print_exception(e)
def _handle_client(self, sock):
data, r_addr = sock.recvfrom(BUF_SIZE)

View file

@ -29,6 +29,8 @@ from shadowsocks import encrypt
VERBOSE_LEVEL = 5
verbose = 0
def check_python():
info = sys.version_info
@ -43,6 +45,14 @@ def check_python():
sys.exit(1)
def print_exception(e):
global verbose
logging.error(e)
if verbose > 0:
import traceback
traceback.print_exc()
def print_shadowsocks():
version = ''
try:
@ -115,6 +125,8 @@ def check_config(config, is_local):
def get_config(is_local):
global verbose
logging.basicConfig(level=logging.INFO,
format='%(levelname)-s: %(message)s')
if is_local:
@ -243,6 +255,7 @@ def get_config(is_local):
level = logging.ERROR
else:
level = logging.INFO
verbose = config['verbose']
logging.basicConfig(level=level,
format='%(asctime)s %(levelname)-8s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')