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:
parent
48ddc1714b
commit
cb7062e1c1
8 changed files with 32 additions and 29 deletions
|
@ -25,7 +25,7 @@ import struct
|
||||||
import re
|
import re
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from shadowsocks import common, lru_cache, eventloop
|
from shadowsocks import common, lru_cache, eventloop, utils
|
||||||
|
|
||||||
|
|
||||||
CACHE_SWEEP_INTERVAL = 30
|
CACHE_SWEEP_INTERVAL = 30
|
||||||
|
@ -221,9 +221,7 @@ def parse_response(data):
|
||||||
response.answers.append((an[1], an[2], an[3]))
|
response.answers.append((an[1], an[2], an[3]))
|
||||||
return response
|
return response
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
import traceback
|
utils.print_exception(e)
|
||||||
traceback.print_exc()
|
|
||||||
logging.error(e)
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,7 @@ import sys
|
||||||
import logging
|
import logging
|
||||||
import signal
|
import signal
|
||||||
import time
|
import time
|
||||||
from shadowsocks import common
|
from shadowsocks import common, utils
|
||||||
|
|
||||||
# this module is ported from ShadowVPN daemon.c
|
# 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,
|
fd = os.open(pid_file, os.O_RDWR | os.O_CREAT,
|
||||||
stat.S_IRUSR | stat.S_IWUSR)
|
stat.S_IRUSR | stat.S_IWUSR)
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
logging.error(e)
|
utils.print_exception(e)
|
||||||
return -1
|
return -1
|
||||||
flags = fcntl.fcntl(fd, fcntl.F_GETFD)
|
flags = fcntl.fcntl(fd, fcntl.F_GETFD)
|
||||||
assert flags != -1
|
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.stdout)
|
||||||
freopen(log_file, 'a', sys.stderr)
|
freopen(log_file, 'a', sys.stderr)
|
||||||
except IOError as e:
|
except IOError as e:
|
||||||
logging.error(e)
|
utils.print_exception(e)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
@ -140,7 +140,7 @@ def daemon_stop(pid_file):
|
||||||
if not buf:
|
if not buf:
|
||||||
logging.error('not running')
|
logging.error('not running')
|
||||||
except IOError as e:
|
except IOError as e:
|
||||||
logging.error(e)
|
utils.print_exception(e)
|
||||||
if e.errno == errno.ENOENT:
|
if e.errno == errno.ENOENT:
|
||||||
# always exit 0 if we are sure daemon is not running
|
# always exit 0 if we are sure daemon is not running
|
||||||
logging.error('not running')
|
logging.error('not running')
|
||||||
|
@ -155,7 +155,7 @@ def daemon_stop(pid_file):
|
||||||
logging.error('not running')
|
logging.error('not running')
|
||||||
# always exit 0 if we are sure daemon is not running
|
# always exit 0 if we are sure daemon is not running
|
||||||
return
|
return
|
||||||
logging.error(e)
|
utils.print_exception(e)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
else:
|
else:
|
||||||
logging.error('pid is not positive: %d', pid)
|
logging.error('pid is not positive: %d', pid)
|
||||||
|
|
|
@ -28,6 +28,8 @@ import errno
|
||||||
import logging
|
import logging
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
||||||
|
from shadowsocks import utils
|
||||||
|
|
||||||
|
|
||||||
__all__ = ['EventLoop', 'POLL_NULL', 'POLL_IN', 'POLL_OUT', 'POLL_ERR',
|
__all__ = ['EventLoop', 'POLL_NULL', 'POLL_IN', 'POLL_OUT', 'POLL_ERR',
|
||||||
'POLL_HUP', 'POLL_NVAL', 'EVENT_NAMES']
|
'POLL_HUP', 'POLL_NVAL', 'EVENT_NAMES']
|
||||||
|
@ -223,9 +225,7 @@ class EventLoop(object):
|
||||||
try:
|
try:
|
||||||
handler(events)
|
handler(events)
|
||||||
except (OSError, IOError) as e:
|
except (OSError, IOError) as e:
|
||||||
logging.error(e)
|
utils.print_exception(e)
|
||||||
import traceback
|
|
||||||
traceback.print_exc()
|
|
||||||
if self._handlers_to_remove:
|
if self._handlers_to_remove:
|
||||||
for handler in self._handlers_to_remove:
|
for handler in self._handlers_to_remove:
|
||||||
self._handlers.remove(handler)
|
self._handlers.remove(handler)
|
||||||
|
|
|
@ -65,10 +65,7 @@ def main():
|
||||||
daemon.set_user(config.get('user', None))
|
daemon.set_user(config.get('user', None))
|
||||||
loop.run()
|
loop.run()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(e)
|
utils.print_exception(e)
|
||||||
if config['verbose']:
|
|
||||||
import traceback
|
|
||||||
traceback.print_exc()
|
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
|
@ -80,10 +80,7 @@ def main():
|
||||||
daemon.set_user(config.get('user', None))
|
daemon.set_user(config.get('user', None))
|
||||||
loop.run()
|
loop.run()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(e)
|
utils.print_exception(e)
|
||||||
if config['verbose']:
|
|
||||||
import traceback
|
|
||||||
traceback.print_exc()
|
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
if int(config['workers']) > 1:
|
if int(config['workers']) > 1:
|
||||||
|
|
|
@ -203,9 +203,7 @@ class TCPRelayHandler(object):
|
||||||
errno.EWOULDBLOCK):
|
errno.EWOULDBLOCK):
|
||||||
uncomplete = True
|
uncomplete = True
|
||||||
else:
|
else:
|
||||||
logging.error(e)
|
utils.print_exception(e)
|
||||||
if self._config['verbose']:
|
|
||||||
traceback.print_exc()
|
|
||||||
self.destroy()
|
self.destroy()
|
||||||
return False
|
return False
|
||||||
if uncomplete:
|
if uncomplete:
|
||||||
|
@ -259,7 +257,7 @@ class TCPRelayHandler(object):
|
||||||
self._config['fast_open'] = False
|
self._config['fast_open'] = False
|
||||||
self.destroy()
|
self.destroy()
|
||||||
else:
|
else:
|
||||||
logging.error(e)
|
utils.print_exception(e)
|
||||||
if self._config['verbose']:
|
if self._config['verbose']:
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
self.destroy()
|
self.destroy()
|
||||||
|
@ -383,7 +381,7 @@ class TCPRelayHandler(object):
|
||||||
self._update_stream(STREAM_DOWN, WAIT_STATUS_READING)
|
self._update_stream(STREAM_DOWN, WAIT_STATUS_READING)
|
||||||
return
|
return
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(e)
|
utils.print_exception(e)
|
||||||
if self._config['verbose']:
|
if self._config['verbose']:
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
self.destroy()
|
self.destroy()
|
||||||
|
@ -445,7 +443,7 @@ class TCPRelayHandler(object):
|
||||||
try:
|
try:
|
||||||
self._write_to_sock(data, self._local_sock)
|
self._write_to_sock(data, self._local_sock)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(e)
|
utils.print_exception(e)
|
||||||
if self._config['verbose']:
|
if self._config['verbose']:
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
# TODO use logging when debug completed
|
# TODO use logging when debug completed
|
||||||
|
@ -683,7 +681,7 @@ class TCPRelay(object):
|
||||||
errno.EWOULDBLOCK):
|
errno.EWOULDBLOCK):
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
logging.error(e)
|
utils.print_exception(e)
|
||||||
if self._config['verbose']:
|
if self._config['verbose']:
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -69,7 +69,7 @@ import struct
|
||||||
import errno
|
import errno
|
||||||
import random
|
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
|
from shadowsocks.common import parse_header, pack_addr
|
||||||
|
|
||||||
|
|
||||||
|
@ -208,7 +208,7 @@ class UDPRelay(object):
|
||||||
if err in (errno.EINPROGRESS, errno.EAGAIN):
|
if err in (errno.EINPROGRESS, errno.EAGAIN):
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
logging.error(e)
|
utils.print_exception(e)
|
||||||
|
|
||||||
def _handle_client(self, sock):
|
def _handle_client(self, sock):
|
||||||
data, r_addr = sock.recvfrom(BUF_SIZE)
|
data, r_addr = sock.recvfrom(BUF_SIZE)
|
||||||
|
|
|
@ -29,6 +29,8 @@ from shadowsocks import encrypt
|
||||||
|
|
||||||
VERBOSE_LEVEL = 5
|
VERBOSE_LEVEL = 5
|
||||||
|
|
||||||
|
verbose = 0
|
||||||
|
|
||||||
|
|
||||||
def check_python():
|
def check_python():
|
||||||
info = sys.version_info
|
info = sys.version_info
|
||||||
|
@ -43,6 +45,14 @@ def check_python():
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
def print_exception(e):
|
||||||
|
global verbose
|
||||||
|
logging.error(e)
|
||||||
|
if verbose > 0:
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
|
|
||||||
|
|
||||||
def print_shadowsocks():
|
def print_shadowsocks():
|
||||||
version = ''
|
version = ''
|
||||||
try:
|
try:
|
||||||
|
@ -115,6 +125,8 @@ def check_config(config, is_local):
|
||||||
|
|
||||||
|
|
||||||
def get_config(is_local):
|
def get_config(is_local):
|
||||||
|
global verbose
|
||||||
|
|
||||||
logging.basicConfig(level=logging.INFO,
|
logging.basicConfig(level=logging.INFO,
|
||||||
format='%(levelname)-s: %(message)s')
|
format='%(levelname)-s: %(message)s')
|
||||||
if is_local:
|
if is_local:
|
||||||
|
@ -243,6 +255,7 @@ def get_config(is_local):
|
||||||
level = logging.ERROR
|
level = logging.ERROR
|
||||||
else:
|
else:
|
||||||
level = logging.INFO
|
level = logging.INFO
|
||||||
|
verbose = config['verbose']
|
||||||
logging.basicConfig(level=level,
|
logging.basicConfig(level=level,
|
||||||
format='%(asctime)s %(levelname)-8s %(message)s',
|
format='%(asctime)s %(levelname)-8s %(message)s',
|
||||||
datefmt='%Y-%m-%d %H:%M:%S')
|
datefmt='%Y-%m-%d %H:%M:%S')
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue