Changeset 41d6187 in sasview


Ignore:
Timestamp:
Jul 13, 2018 3:26:57 AM (6 years ago)
Author:
Torin Cooper-Bennun <torin.cooper-bennun@…>
Children:
6138f73
Parents:
158a0c7
Message:

rm XStream; QT signal in QtHandler? logging handler; only logs in Log Explorer (no stdout/stderr)

Location:
src/sas
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/sas/logger_config.py

    r5dba493 r41d6187  
    77 
    88import pkg_resources 
    9  
    10 import sas.qtgui.Utilities.LocalConfig as LocalConfig 
    119 
    1210''' 
     
    3129            logger = logging.getLogger(self.name) 
    3230 
    33         # use this as a final override if the need arises 
    34         logging.disable(LocalConfig.DISABLE_LOGGING) 
    35  
    3631        return logger 
    3732 
     
    4338        self._update_all_logs_to_debug(logger) 
    4439        logging.captureWarnings(True) 
    45  
    46         # use this as a final override if the need arises 
    47         logging.disable(LocalConfig.DISABLE_LOGGING) 
    4840 
    4941        return logger 
  • src/sas/qtgui/MainWindow/GuiManager.py

    rf0a8f74 r41d6187  
    1313# General SAS imports 
    1414from sas.qtgui.Utilities.ConnectionProxy import ConnectionProxy 
    15 from sas.qtgui.Utilities.SasviewLogger import XStream 
     15from sas.qtgui.Utilities.SasviewLogger import setup_qt_logging 
    1616 
    1717import sas.qtgui.Utilities.LocalConfig as LocalConfig 
     
    8686 
    8787        # Fork off logging messages to the Log Window 
    88         XStream.stdout().messageWritten.connect(self.listWidget.insertPlainText) 
    89         XStream.stderr().messageWritten.connect(self.listWidget.insertPlainText) 
     88        handler = setup_qt_logging() 
     89        handler.messageWritten.connect(self.listWidget.insertPlainText) 
    9090 
    9191        # Log the start of the session 
  • src/sas/qtgui/Utilities/SasviewLogger.py

    r5dba493 r41d6187  
    66 
    77 
    8 class 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     """ 
    26     _stdout = None 
    27     _stderr = None 
    28     messageWritten = pyqtSignal(str) 
    29  
    30     _real_stream = None 
    31  
    32     def flush(self): 
    33         pass 
    34  
    35     def fileno(self): 
    36         return -1 
    37  
    38     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         """ 
    61         if(not self.signalsBlocked()): 
    62             self.messageWritten.emit(str(msg)) 
    63  
    64     @staticmethod 
    65     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         """ 
    72         if(not XStream._stdout): 
    73             XStream._stdout = XStream() 
    74             XStream._stdout._real_stream = sys.stdout 
    75             sys.stdout = XStream._stdout 
    76         return XStream._stdout 
    77  
    78     @staticmethod 
    79     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         """ 
    86         if(not XStream._stderr): 
    87             XStream._stderr = XStream() 
    88             XStream._stderr._real_stream = sys.stderr 
    89             sys.stderr = XStream._stderr 
    90         return XStream._stderr 
    91  
    92  
    93 class QtHandler(logging.Handler): 
     8class QtHandler(QObject, logging.Handler): 
    949    """ 
    9510    Version of logging handler "emitting" the message to custom stdout() 
    9611    """ 
     12    messageWritten = pyqtSignal(str) 
     13 
    9714    def __init__(self): 
     15        QObject.__init__(self) 
    9816        logging.Handler.__init__(self) 
    9917 
     
    10119        record = self.format(record) 
    10220        if record: 
    103             XStream.stdout().write_log('%s\n'%record) 
     21            self.messageWritten.emit('%s\n'%record) 
    10422 
    10523 
    106 # Define the default logger 
    107 logger = logging.getLogger() 
     24def setup_qt_logging(): 
     25    # Define the default logger 
     26    logger = logging.getLogger() 
    10827 
    109 # Add the qt-signal logger 
    110 handler = QtHandler() 
    111 handler.setFormatter(logging.Formatter( 
    112     fmt="%(asctime)s - %(levelname)s: %(message)s", 
    113     datefmt="%H:%M:%S" 
    114 )) 
    115 logger.addHandler(handler) 
     28    # Add the qt-signal logger 
     29    handler = QtHandler() 
     30    handler.setFormatter(logging.Formatter( 
     31        fmt="%(asctime)s - %(levelname)s: %(message)s", 
     32        datefmt="%H:%M:%S" 
     33    )) 
     34    handler.setLevel(logging.DEBUG) 
     35    logger.addHandler(handler) 
     36 
     37    return handler 
Note: See TracChangeset for help on using the changeset viewer.