source: sasview/src/sas/qtgui/Utilities/SasviewLogger.py @ c57ecca

Last change on this file since c57ecca was 83eb5208, checked in by Piotr Rozyczko <rozyczko@…>, 8 years ago

Putting files in more ordered fashion

  • Property mode set to 100644
File size: 1.5 KB
Line 
1import os
2import sys
3import logging
4
5from PyQt4 import QtCore
6
7
8class XStream(QtCore.QObject):
9    _stdout = None
10    _stderr = None
11    messageWritten = QtCore.pyqtSignal(str)
12
13    def flush(self):
14        pass
15
16    def fileno(self):
17        return -1
18
19    def write(self, msg):
20        if(not self.signalsBlocked()):
21            self.messageWritten.emit(unicode(msg))
22
23    @staticmethod
24    def stdout():
25        if(not XStream._stdout):
26            XStream._stdout = XStream()
27            sys.stdout = XStream._stdout
28        return XStream._stdout
29
30    @staticmethod
31    def stderr():
32        if(not XStream._stderr):
33            XStream._stderr = XStream()
34            sys.stderr = XStream._stderr
35        return XStream._stderr
36
37class QtHandler(logging.Handler):
38    """
39    Version of logging handler
40    "emitting" the message to custom stdout()
41    """
42    def __init__(self):
43        logging.Handler.__init__(self)
44
45    def emit(self, record):
46        record = self.format(record)
47        if record:
48            XStream.stdout().write('%s\n'%record)
49
50
51# Define the default logger
52logger = logging.getLogger()
53
54# Add the file handler
55logging.basicConfig(level=logging.INFO,
56                    format='%(asctime)s %(levelname)s %(message)s',
57                    filename=os.path.join(os.path.expanduser("~"),
58                                          'sasview.log'))
59# Add the qt-signal logger
60handler = QtHandler()
61handler.setFormatter(logging.Formatter("%(levelname)s: %(message)s"))
62logger.addHandler(handler)
Note: See TracBrowser for help on using the repository browser.