| 1 |
import time |
|---|
| 2 |
|
|---|
| 3 |
try: |
|---|
| 4 |
import cPickle as pickle |
|---|
| 5 |
except ImportError: |
|---|
| 6 |
import pickle |
|---|
| 7 |
|
|---|
| 8 |
import cherrypy |
|---|
| 9 |
from basefilter import BaseFilter |
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
class LogDebugInfoFilter(BaseFilter): |
|---|
| 13 |
"""Filter that adds debug information to the page""" |
|---|
| 14 |
|
|---|
| 15 |
def on_start_resource(self): |
|---|
| 16 |
cherrypy.request.startBuilTime = time.time() |
|---|
| 17 |
|
|---|
| 18 |
def before_finalize(self): |
|---|
| 19 |
if not cherrypy.config.get('log_debug_info_filter.on', False): |
|---|
| 20 |
return |
|---|
| 21 |
|
|---|
| 22 |
mimelist = cherrypy.config.get('log_debug_info_filter.mime_types', ['text/html']) |
|---|
| 23 |
ct = cherrypy.response.headers.get('Content-Type', '').split(';')[0] |
|---|
| 24 |
if ct in mimelist: |
|---|
| 25 |
body = cherrypy.response.collapse_body() |
|---|
| 26 |
debuginfo = '\n' |
|---|
| 27 |
|
|---|
| 28 |
logAsComment = cherrypy.config.get('log_debug_info_filter.log_as_comment', False) |
|---|
| 29 |
if logAsComment: |
|---|
| 30 |
debuginfo += '<!-- ' |
|---|
| 31 |
else: |
|---|
| 32 |
debuginfo += "<br/><br/>" |
|---|
| 33 |
logList = [] |
|---|
| 34 |
|
|---|
| 35 |
if cherrypy.config.get('log_debug_info_filter.log_build_time', True): |
|---|
| 36 |
logList.append("Build time: %.03fs" % ( |
|---|
| 37 |
time.time() - cherrypy.request.startBuilTime)) |
|---|
| 38 |
|
|---|
| 39 |
if cherrypy.config.get('log_debug_info_filter.log_page_size', True): |
|---|
| 40 |
logList.append("Page size: %.02fKB" % ( |
|---|
| 41 |
len(body)/float(1024))) |
|---|
| 42 |
''' |
|---|
| 43 |
# this is not compatible with the session filter |
|---|
| 44 |
if (cherrypy.config.get('log_debug_info_filter.log_session_size', True) |
|---|
| 45 |
and cherrypy.config.get('session.storageType')): |
|---|
| 46 |
# Pickle session data to get its size |
|---|
| 47 |
try: |
|---|
| 48 |
dumpStr = pickle.dumps(cherrypy.request.sessionMap, 1) |
|---|
| 49 |
logList.append("Session data size: %.02fKB" % |
|---|
| 50 |
(len(dumpStr) / float(1024))) |
|---|
| 51 |
except: |
|---|
| 52 |
logList.append("Session data size: Unable to pickle session") |
|---|
| 53 |
''' |
|---|
| 54 |
|
|---|
| 55 |
debuginfo += ', '.join(logList) |
|---|
| 56 |
if logAsComment: |
|---|
| 57 |
debuginfo += '-->' |
|---|
| 58 |
|
|---|
| 59 |
cherrypy.response.body = [body, debuginfo] |
|---|
| 60 |
|
|---|
| 61 |
cherrypy.response.headers.pop("Content-Length", None) |
|---|