Robert Brewer avatar Robert Brewer committed 50decf0

Fix for #745 (Make a builtin tool which logs all hooks for a given request).

Comments (0)

Files changed (4)

cherrypy/_cprequest.py

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

cherrypy/_cptools.py

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

cherrypy/lib/cptools.py

     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:

cherrypy/test/test_misc_tools.py

 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
 
             'tools.response_headers.on': True,
             'tools.response_headers.headers': [("Content-Language", "fr"),
                                                ('Content-Type', 'text/plain')],
+            'tools.log_hooks.on': True,
             }
     
     
     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
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.