1 """Logging handlers for Pyjamas logging based on CPython's logging handlers."""
2 __author__ = 'Peter Bittner <peter.bittner@gmx.net>'
3
4 from cgi import escape
5 from logging import Handler
6 from pyjamas import DOM, Window
7 from __pyjamas__ import doc, JS
8
10 """A log output handler displaying any log message using an alert popup."""
13
14 - def emit(self, record):
17
19 """A log output handler showing text in a <div> appended to the end of the
20 HTML document. Use the 'div' property to find out the element's ID if you
21 want to position or style the output in your Pyjamas application."""
22 div = None
23 div_id = ''
24 output = ''
25
29
31 """Strip out all characters that could be invalid for an element ID"""
32 from string import ascii_letters, digits
33 return ''.join(c for c in name if c in (ascii_letters + digits + '_'))
34
36 """Add a container in the DOM where logging output will be written to.
37 This cannot be done in the constructor as it must happen late enough
38 to ensure a document body (to add an element to) does already exist."""
39 if self.div == None:
40 self.div = DOM.createDiv()
41 self.div.setAttribute('id', self.div_id)
42 DOM.appendChild(doc().body, self.div)
43
44 - def emit(self, record):
51
53 """A log output handler making use of Firebug's console.log() function."""
54
55 __consoleFuncForLevel = None
56
66
67 - def emit(self, record):
68 """Print a message using the console.debug/info/warn/error/log()
69 functions. Use a simple print() as a fallback in browsers that don't
70 support console.log -- including Pyjamas Desktop."""
71 msg = self.format(record)
72 func = self.__consoleFuncForLevel.get(record.levelname, self.__log)
73 try:
74
75 func(msg)
76 except:
77 print(msg)
78
80 JS(" console['debug'](@{{msg}}); ")
81
83 JS(" console['info'](@{{msg}}); ")
84
86 JS(" console['warn'](@{{msg}}); ")
87
89 JS(" console['error'](@{{msg}}); ")
90
92 JS(" console['log'](@{{msg}}); ")
93
95 """A log output handler that does nothing. Use to disable logging."""
98
99 - def emit(self, record):
101