diff options
author | Robert Brewer <fumanchu@aminus.org> | 2007-10-30 00:27:35 +0000 |
---|---|---|
committer | Robert Brewer <fumanchu@aminus.org> | 2007-10-30 00:27:35 +0000 |
commit | 8930a99ab31bdd06abb6f3889e11d5ed1db9a0e3 (patch) | |
tree | f9e23188f6cb69fa0fcce62aa7e3113882e7a08f /cherrypy | |
parent | b2a94dc6e65737807c779799fc6b9d92b16a6c11 (diff) | |
download | cherrypy-git-8930a99ab31bdd06abb6f3889e11d5ed1db9a0e3.tar.gz |
Fix for #745 (Make a builtin tool which logs all hooks for a given request).
Diffstat (limited to 'cherrypy')
-rw-r--r-- | cherrypy/_cprequest.py | 8 | ||||
-rw-r--r-- | cherrypy/_cptools.py | 1 | ||||
-rw-r--r-- | cherrypy/lib/cptools.py | 18 | ||||
-rw-r--r-- | cherrypy/test/test_misc_tools.py | 8 |
4 files changed, 34 insertions, 1 deletions
diff --git a/cherrypy/_cprequest.py b/cherrypy/_cprequest.py index 32e6c45b..d31ba94f 100644 --- a/cherrypy/_cprequest.py +++ b/cherrypy/_cprequest.py @@ -56,6 +56,14 @@ class Hook(object): def __call__(self): """Run self.callback(**self.kwargs).""" return self.callback(**self.kwargs) + + def __repr__(self): + cls = self.__class__ + return ("%s.%s(callback=%r, failsafe=%r, priority=%r, %s)" + % (cls.__module__, cls.__name__, self.callback, + self.failsafe, self.priority, + ", ".join(['%s=%r' % (k, v) + for k, v in self.kwargs.iteritems()]))) class HookMap(dict): diff --git a/cherrypy/_cptools.py b/cherrypy/_cptools.py index 92e9274e..5ce7e4de 100644 --- a/cherrypy/_cptools.py +++ b/cherrypy/_cptools.py @@ -427,6 +427,7 @@ _d.proxy = Tool('before_request_body', cptools.proxy, priority=30) _d.response_headers = Tool('on_start_resource', cptools.response_headers) _d.log_tracebacks = Tool('before_error_response', cptools.log_traceback) _d.log_headers = Tool('before_error_response', cptools.log_request_headers) +_d.log_hooks = Tool('on_end_request', cptools.log_hooks, priority=100) _d.err_redirect = ErrorTool(cptools.redirect) _d.etags = Tool('before_finalize', cptools.validate_etags) _d.decode = Tool('before_handler', encoding.decode) diff --git a/cherrypy/lib/cptools.py b/cherrypy/lib/cptools.py index 8d2e261e..bf610709 100644 --- a/cherrypy/lib/cptools.py +++ b/cherrypy/lib/cptools.py @@ -296,6 +296,24 @@ def log_request_headers(): h = [" %s: %s" % (k, v) for k, v in cherrypy.request.header_list] cherrypy.log('\nRequest Headers:\n' + '\n'.join(h), "HTTP") +def log_hooks(): + """Write request.hooks to the cherrypy error log.""" + msg = [] + # Sort by the standard points if possible. + from cherrypy import _cprequest + points = _cprequest.hookpoints + for k in cherrypy.request.hooks.keys(): + if k not in points: + points.append(k) + + for k in points: + msg.append(" %s:" % k) + v = cherrypy.request.hooks.get(k, []) + v.sort() + for h in v: + msg.append(" %r" % h) + cherrypy.log('\nRequest Hooks:\n' + '\n'.join(msg), "HTTP") + def redirect(url='', internal=True): """Raise InternalRedirect or HTTPRedirect to the given url.""" if internal: diff --git a/cherrypy/test/test_misc_tools.py b/cherrypy/test/test_misc_tools.py index d13ae90a..8752bfcb 100644 --- a/cherrypy/test/test_misc_tools.py +++ b/cherrypy/test/test_misc_tools.py @@ -1,6 +1,10 @@ from cherrypy.test import test test.prefer_parent_path() +import os +localDir = os.path.dirname(__file__) +logfile = os.path.join(localDir, "test_misc_tools.log") + import cherrypy from cherrypy import tools @@ -20,6 +24,7 @@ def setup_server(): 'tools.response_headers.on': True, 'tools.response_headers.headers': [("Content-Language", "fr"), ('Content-Type', 'text/plain')], + 'tools.log_hooks.on': True, } @@ -67,7 +72,8 @@ def setup_server(): root.referer = Referer() root.accept = Accept() cherrypy.tree.mount(root, config=conf) - cherrypy.config.update({'environment': 'test_suite'}) + cherrypy.config.update({'environment': 'test_suite', + 'log.error_file': logfile}) from cherrypy.test import helper |