source: qesdi/geoplot/trunk/lib/geoplot/stdout_handler.py @ 5403

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/qesdi/geoplot/trunk/lib/geoplot/stdout_handler.py@5403
Revision 5403, 1.5 KB checked in by pnorton, 12 years ago (diff)

Moved QESDI tree from DCIP repository
 http://proj.badc.rl.ac.uk/svn/dcip/qesdi@3900.

Line 
1import sys, types
2from logging import Handler
3
4class StdoutHandler(Handler):
5    """
6    A logging handler that sends all output to sys.stdout
7
8    This handler differes from StreamHandler(sys.stdout) in that it will always write
9    to sys.stdout even if it is re-assigned (while StreamHandler(sys.stdout) wont notice
10    if sys.stdout is changed after it is constructed).
11    """
12    def __init__(self):
13        """
14        Initialize the handler.
15        """
16        Handler.__init__(self)
17
18    def flush(self):
19        """
20        Flushes stdout.
21        """
22        sys.stdout.flush()
23
24    def emit(self, record):
25        """
26        Emit a record. (Function taken form StreamHandler)
27
28        If a formatter is specified, it is used to format the record.
29        The record is then written to the stream with a trailing newline
30        [N.B. this may be removed depending on feedback]. If exception
31        information is present, it is formatted using
32        traceback.print_exception and appended to the stream.
33        """
34        try:
35            msg = self.format(record)
36            fs = "%s\n"
37            if not hasattr(types, "UnicodeType"): #if no unicode support...
38                sys.stdout.write(fs % msg)
39            else:
40                try:
41                    sys.stdout.write(fs % msg)
42                except UnicodeError:
43                    sys.stdout.write(fs % msg.encode("UTF-8"))
44            self.flush()
45        except (KeyboardInterrupt, SystemExit):
46            raise
47        except:
48            self.handleError(record)
49
Note: See TracBrowser for help on using the repository browser.