kopia lustrzana https://github.com/inkstitch/inkstitch
set up debug logging
rodzic
f6e6d099d2
commit
8aa86f6619
|
@ -12,4 +12,4 @@ messages.po
|
||||||
/DEBUG
|
/DEBUG
|
||||||
.pydevproject
|
.pydevproject
|
||||||
.project
|
.project
|
||||||
|
/debug.log
|
||||||
|
|
12
inkstitch.py
12
inkstitch.py
|
@ -7,6 +7,7 @@ from argparse import ArgumentParser
|
||||||
|
|
||||||
from lib import extensions
|
from lib import extensions
|
||||||
from lib.utils import save_stderr, restore_stderr
|
from lib.utils import save_stderr, restore_stderr
|
||||||
|
import lib.debug as debug
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger('shapely.geos')
|
logger = logging.getLogger('shapely.geos')
|
||||||
|
@ -34,16 +35,7 @@ parser.add_argument("--extension")
|
||||||
my_args, remaining_args = parser.parse_known_args()
|
my_args, remaining_args = parser.parse_known_args()
|
||||||
|
|
||||||
if os.path.exists(os.path.join(os.path.dirname(os.path.realpath(__file__)), "DEBUG")):
|
if os.path.exists(os.path.join(os.path.dirname(os.path.realpath(__file__)), "DEBUG")):
|
||||||
# How to debug Ink/Stitch:
|
debug.enable()
|
||||||
#
|
|
||||||
# 1. Install LiClipse (liclipse.com) -- no need to install Eclipse first
|
|
||||||
# 2. Start debug server as described here: http://www.pydev.org/manual_adv_remote_debugger.html
|
|
||||||
# * follow the "Note:" to enable the debug server menu item
|
|
||||||
# 3. Create a file named "DEBUG" next to inkstitch.py in your git clone.
|
|
||||||
# 4. Run any extension and PyDev will start debugging.
|
|
||||||
|
|
||||||
import pydevd
|
|
||||||
pydevd.settrace()
|
|
||||||
|
|
||||||
extension_name = my_args.extension
|
extension_name = my_args.extension
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,84 @@
|
||||||
|
from datetime import datetime
|
||||||
|
import os
|
||||||
|
import socket
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
|
||||||
|
|
||||||
|
class Debug(object):
|
||||||
|
def __init__(self):
|
||||||
|
self.last_log_time = None
|
||||||
|
|
||||||
|
def enable(self):
|
||||||
|
self.enable_log()
|
||||||
|
self.enable_debugger()
|
||||||
|
|
||||||
|
def enable_log(self):
|
||||||
|
self.log = self._log
|
||||||
|
self.raw_log = self._raw_log
|
||||||
|
self.log_file = open(os.path.join(os.path.dirname(os.path.dirname(__file__)), "debug.log"), "w")
|
||||||
|
self.log("Debug logging enabled.")
|
||||||
|
|
||||||
|
def enable_debugger(self):
|
||||||
|
# How to debug Ink/Stitch:
|
||||||
|
#
|
||||||
|
# 1. Install LiClipse (liclipse.com) -- no need to install Eclipse first
|
||||||
|
# 2. Start debug server as described here: http://www.pydev.org/manual_adv_remote_debugger.html
|
||||||
|
# * follow the "Note:" to enable the debug server menu item
|
||||||
|
# 3. Create a file named "DEBUG" next to inkstitch.py in your git clone.
|
||||||
|
# 4. Run any extension and PyDev will start debugging.
|
||||||
|
|
||||||
|
try:
|
||||||
|
import pydevd
|
||||||
|
except ImportError:
|
||||||
|
self.log("importing pydevd failed (debugger disabled)")
|
||||||
|
|
||||||
|
# pydevd likes to shout about errors to stderr whether I want it to or not
|
||||||
|
with open(os.devnull, 'w') as devnull:
|
||||||
|
stderr = sys.stderr
|
||||||
|
sys.stderr = devnull
|
||||||
|
|
||||||
|
try:
|
||||||
|
pydevd.settrace()
|
||||||
|
except socket.error, error:
|
||||||
|
self.log("Debugging: connection to pydevd failed: %s", error)
|
||||||
|
self.log("Be sure to run 'Start debugging server' in PyDev to enable debugging.")
|
||||||
|
else:
|
||||||
|
self.log("Enabled PyDev debugger.")
|
||||||
|
|
||||||
|
sys.stderr = stderr
|
||||||
|
|
||||||
|
def _noop(self, *args, **kwargs):
|
||||||
|
pass
|
||||||
|
|
||||||
|
log = _noop
|
||||||
|
raw_log = _noop
|
||||||
|
|
||||||
|
def _log(self, message, *args):
|
||||||
|
if self.last_log_time:
|
||||||
|
message = "(+%s) %s" % (datetime.now() - self.last_log_time, message)
|
||||||
|
|
||||||
|
self.raw_log(message, *args)
|
||||||
|
|
||||||
|
def _raw_log(self, message, *args):
|
||||||
|
now = datetime.now()
|
||||||
|
timestamp = now.isoformat()
|
||||||
|
self.last_log_time = now
|
||||||
|
|
||||||
|
print >> self.log_file, timestamp, message % args
|
||||||
|
self.log_file.flush()
|
||||||
|
|
||||||
|
def time(self, func):
|
||||||
|
def decorated(*args, **kwargs):
|
||||||
|
self.raw_log("entering %s()", func.func_name)
|
||||||
|
start = time.time()
|
||||||
|
result = func(*args, **kwargs)
|
||||||
|
end = time.time()
|
||||||
|
self.raw_log("leaving %s(), duration = %s", func.func_name, round(end - start, 6))
|
||||||
|
return result
|
||||||
|
|
||||||
|
return decorated
|
||||||
|
|
||||||
|
|
||||||
|
debug = Debug()
|
||||||
|
enable = debug.enable
|
Ładowanie…
Reference in New Issue