Contact: fumanchu@aminus.org

Log in as guest/misc to create tickets

root/wsgiconq.py

Revision 124 (checked in by fumanchu, 5 years ago)

New args to WSGILogger.

  • Property svn:eol-style set to native
Line 
1 import os
2 import re
3 import thread
4
5
6 class WSGILogger(object):
7     """A WSGI middleware app which wraps 'nextapp' with pyconquer."""
8    
9     def __init__(self, nextapp, path="pyconquer.log", time_calls=True,
10                  match=".*", c_calls=False):
11         self.nextapp = nextapp
12         self.path = path
13         self.time_calls = time_calls
14         self.match = match
15         self.events = ['call', 'return', 'exception']
16         if c_calls:
17             self.events.extend(['c_call', 'c_return', 'c_exception'])
18         self.count = 0
19    
20     def run(self, func, *args):
21         """run(func, *args). Run func, dumping trace data into self.path."""
22         import pyconquer
23         tr = pyconquer.Logger(events = self.events)
24         path, ext = os.path.splitext(self.path)
25         self.count += 1
26         path = "%s-%s-%s%s" % (path, self.count, thread.get_ident(), ext)
27        
28         tr.out = open(path, "wb")
29         tr.time_calls = self.time_calls
30         try:
31             tr.start()
32             return func(*args)
33         finally:
34             tr.stop()
35             tr.out.close()
36    
37     def __call__(self, environ, start_response):
38         # Wrap the request in a function so we get a "total time".
39         # This breaks the spec (which says we must yield for each chunk
40         # in the next app), but ... meh.
41         def gather():
42             result = []
43             for line in self.nextapp(environ, start_response):
44                 result.append(line)
45             return result
46        
47         if re.search(self.match, environ.get('PATH_INFO', '')):
48             return self.run(gather)
49         else:
50             return gather()
51
Note: See TracBrowser for help on using the browser.