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

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since 9c0ce68 was 4992ff2, checked in by Piotr Rozyczko <rozyczko@…>, 7 years ago

Initial, in-progress version. Not really working atm. SASVIEW-787

  • Property mode set to 100644
File size: 1.5 KB
Line 
1import os
2import sys
3import logging
4
5from PyQt5.QtCore import *
6
7
8class XStream(QObject):
9    _stdout = None
10    _stderr = None
11    messageWritten = 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(str(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.