CherryPy / lib / filter / logdebuginfofilter.py

The branch 'cherrypy' does not exist.
import time

try:
    import cPickle as pickle
except ImportError:
    import pickle

from basefilter import BaseFilter


class LogDebugInfoFilter(BaseFilter):
    """Filter that adds debug information to the page"""
    
    def onStartResource(self):
        # We have to dynamically import cherrypy because Python can't handle
        #   circular module imports :-(
        global cherrypy
        import cherrypy
        cherrypy.request.startBuilTime = time.time()
    
    def beforeFinalize(self):
        if not cherrypy.config.get('logDebugInfoFilter.on', False):
            return
        
        mimelist = cherrypy.config.get('logDebugInfoFilter.mimeTypeList', ['text/html'])
        ct = cherrypy.response.headerMap.get('Content-Type').split(';')[0]
        if ct in mimelist:
            body = ''.join([chunk for chunk in cherrypy.response.body])
            debuginfo = '\n'
            
            logAsComment = cherrypy.config.get('logDebugInfoFilter.logAsComment', False)
            if logAsComment:
                debuginfo += '<!-- '
            else:
                debuginfo += "<br/><br/>"
            logList = []
            
            if cherrypy.config.get('logDebugInfoFilter.logBuildTime', True):
                logList.append("Build time: %.03fs" % (
                    time.time() - cherrypy.request.startBuilTime))
            
            if cherrypy.config.get('logDebugInfoFilter.logPageSize', True):
                logList.append("Page size: %.02fKB" % (
                    len(body)/float(1024)))
            ''' 
            # this is not compatible with the session filter
            if (cherrypy.config.get('logDebugInfoFilter.logSessionSize', True)
                and cherrypy.config.get('session.storageType')):
                # Pickle session data to get its size
                try:
                    dumpStr = pickle.dumps(cherrypy.request.sessionMap, 1)
                    logList.append("Session data size: %.02fKB" %
                                   (len(dumpStr) / float(1024)))
                except:
                    logList.append("Session data size: Unable to pickle session")
            '''
            
            debuginfo += ', '.join(logList)
            if logAsComment:
                debuginfo += '-->'
            
            cherrypy.response.body = [body, debuginfo]
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.