Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/qtgui/Utilities/SasviewLogger.py

    r4992ff2 r5dba493  
    77 
    88class XStream(QObject): 
     9    """ 
     10    Imitates, loosely, an `io.TextIOWrapper` class in order to intercept 
     11    messages going to stdout and stderr. 
     12 
     13    To intercept messages to stdout, use: 
     14 
     15    .. code-block:: python 
     16       XStream.stdout().messageWritten.mySignalHandler() 
     17 
     18    All messages to stdout will now also be emitted to your handler. Use 
     19    `XStream.stderr()` in the same way to intercept stderr messages. 
     20 
     21    An additional function, `XStream.write_log()`, emits a message only to the 
     22    messageWritten signal, and not the stdout or stderr streams. It is intended 
     23    for separation of log handling between console and Qt, as reflected in the 
     24    name. 
     25    """ 
    926    _stdout = None 
    1027    _stderr = None 
    1128    messageWritten = pyqtSignal(str) 
     29 
     30    _real_stream = None 
    1231 
    1332    def flush(self): 
     
    1837 
    1938    def write(self, msg): 
     39        """ 
     40        'msg' is emitted in the messageWritten signal, and is also written to 
     41        the original stream. This means stdout/stderr messages have been 
     42        redirected to a custom location (e.g. in-widget), but also reach 
     43        console output. 
     44 
     45        :param msg: message to write 
     46        :return: None 
     47        """ 
     48        if(not self.signalsBlocked()): 
     49            self.messageWritten.emit(str(msg)) 
     50        self._real_stream.write(msg) 
     51 
     52    def write_log(self, msg): 
     53        """ 
     54        'msg' is only emitted in the messageWritten signal, not in the original 
     55        stdout/stderr stream, for the purposes of logging. Use in, e.g. a 
     56        custom logging handler's emit() function. 
     57 
     58        :param msg: message to write 
     59        :return: None 
     60        """ 
    2061        if(not self.signalsBlocked()): 
    2162            self.messageWritten.emit(str(msg)) 
     
    2364    @staticmethod 
    2465    def stdout(): 
     66        """ 
     67        Creates imitation of sys.stdout. 
     68        :return: An XStream instance that interfaces exactly like sys.stdout, 
     69                 but, has redirection capabilities through the 
     70                 messageWritten(str) signal. 
     71        """ 
    2572        if(not XStream._stdout): 
    2673            XStream._stdout = XStream() 
     74            XStream._stdout._real_stream = sys.stdout 
    2775            sys.stdout = XStream._stdout 
    2876        return XStream._stdout 
     
    3078    @staticmethod 
    3179    def stderr(): 
     80        """ 
     81        Creates imitation of sys.stderr. 
     82        :return: An XStream instance that interfaces exactly like sys.stderr, 
     83                 but, has redirection capabilities through the 
     84                 messageWritten(str) signal. 
     85        """ 
    3286        if(not XStream._stderr): 
    3387            XStream._stderr = XStream() 
     88            XStream._stderr._real_stream = sys.stderr 
    3489            sys.stderr = XStream._stderr 
    3590        return XStream._stderr 
    3691 
     92 
    3793class QtHandler(logging.Handler): 
    3894    """ 
    39     Version of logging handler 
    40     "emitting" the message to custom stdout() 
     95    Version of logging handler "emitting" the message to custom stdout() 
    4196    """ 
    4297    def __init__(self): 
     
    46101        record = self.format(record) 
    47102        if record: 
    48             XStream.stdout().write('%s\n'%record) 
     103            XStream.stdout().write_log('%s\n'%record) 
    49104 
    50105 
     
    52107logger = logging.getLogger() 
    53108 
    54 # Add the file handler 
    55 logging.basicConfig(level=logging.INFO, 
    56                     format='%(asctime)s %(levelname)s %(message)s', 
    57                     filename=os.path.join(os.path.expanduser("~"), 
    58                                           'sasview.log')) 
    59109# Add the qt-signal logger 
    60110handler = QtHandler() 
    61 handler.setFormatter(logging.Formatter("%(levelname)s: %(message)s")) 
     111handler.setFormatter(logging.Formatter( 
     112    fmt="%(asctime)s - %(levelname)s: %(message)s", 
     113    datefmt="%H:%M:%S" 
     114)) 
    62115logger.addHandler(handler) 
Note: See TracChangeset for help on using the changeset viewer.