Browse Source

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.
master
clowwindy 10 years ago
parent
commit
cb7062e1c1
  1. 6
      shadowsocks/asyncdns.py
  2. 10
      shadowsocks/daemon.py
  3. 6
      shadowsocks/eventloop.py
  4. 5
      shadowsocks/local.py
  5. 5
      shadowsocks/server.py
  6. 12
      shadowsocks/tcprelay.py
  7. 4
      shadowsocks/udprelay.py
  8. 13
      shadowsocks/utils.py

6
shadowsocks/asyncdns.py

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

10
shadowsocks/daemon.py

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

6
shadowsocks/eventloop.py

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

5
shadowsocks/local.py

@ -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__':

5
shadowsocks/server.py

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

12
shadowsocks/tcprelay.py

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

4
shadowsocks/udprelay.py

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

13
shadowsocks/utils.py

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

Loading…
Cancel
Save