Changeset e4335ae in sasview for src/sas/qtgui


Ignore:
Timestamp:
Jul 19, 2018 6:12:03 AM (6 years ago)
Author:
Piotr Rozyczko <piotr.rozyczko@…>
Branches:
ESS_GUI, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc
Children:
c0de493
Parents:
02e7d3a
git-author:
Torin Cooper-Bennun <40573959+tcbennun@…> (07/19/18 06:12:03)
git-committer:
Piotr Rozyczko <piotr.rozyczko@…> (07/19/18 06:12:03)
Message:

SASVIEW-957 logging changes (#158)

  • rework XStream to continue to write to stdout/stderr alongside redirection; make logging level setting consistent; make log configuration more consistent
  • rm XStream; QT signal in QtHandler? logging handler; only logs in Log Explorer (no stdout/stderr)
  • no need to change handler level
  • use QTextBrowser.append to facilitate auto-scrolling in the Log Explorer
  • modify logger unit test to reflect changes (passes)
Location:
src/sas/qtgui
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • src/sas/qtgui/MainWindow/GuiManager.py

    rf0a8f74 re4335ae  
    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.appendLog) 
    9090 
    9191        # Log the start of the session 
     
    269269        """ 
    270270        self.statusLabel.setText(text) 
     271 
     272    def appendLog(self, msg): 
     273        """Appends a message to the list widget in the Log Explorer. Use this 
     274        instead of listWidget.insertPlainText() to facilitate auto-scrolling""" 
     275        self.listWidget.append(msg.strip()) 
    271276 
    272277    def createGuiData(self, item, p_file=None): 
  • src/sas/qtgui/MainWindow/MainWindow.py

    ra3221b6 re4335ae  
    1010from sas.qtgui.UI import main_resources_rc 
    1111from .UI.MainWindowUI import Ui_MainWindow 
    12  
    13 # Initialize logging 
    14 import sas.qtgui.Utilities.SasviewLogger 
    1512 
    1613class MainSasViewWindow(QMainWindow, Ui_MainWindow): 
  • src/sas/qtgui/Perspectives/Fitting/FittingWidget.py

    re20870bc re4335ae  
    106106        # Globals 
    107107        self.initializeGlobals() 
    108  
    109         # Set up desired logging level 
    110         logging.disable(LocalConfig.DISABLE_LOGGING) 
    111108 
    112109        # data index for the batch set 
  • src/sas/qtgui/Utilities/LocalConfig.py

    ra0ed202 re4335ae  
    136136USING_TWISTED = True 
    137137 
    138 # Logging levels to disable, if any 
    139 DISABLE_LOGGING = logging.DEBUG 
    140  
    141138# Time out for updating sasview 
    142139UPDATE_TIMEOUT = 2 
    143140 
    144141# Logging levels to disable, if any 
    145 DISABLE_LOGGING = logging.DEBUG 
     142DISABLE_LOGGING = logging.NOTSET 
    146143 
    147144def printEVT(message): 
  • src/sas/qtgui/Utilities/SasviewLogger.py

    r4992ff2 re4335ae  
    66 
    77 
    8 class XStream(QObject): 
    9     _stdout = None 
    10     _stderr = None 
     8class QtHandler(QObject, logging.Handler): 
     9    """ 
     10    Version of logging handler "emitting" the message to custom stdout() 
     11    """ 
    1112    messageWritten = pyqtSignal(str) 
    1213 
    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  
    37 class QtHandler(logging.Handler): 
    38     """ 
    39     Version of logging handler 
    40     "emitting" the message to custom stdout() 
    41     """ 
    4214    def __init__(self): 
     15        QObject.__init__(self) 
    4316        logging.Handler.__init__(self) 
    4417 
     
    4619        record = self.format(record) 
    4720        if record: 
    48             XStream.stdout().write('%s\n'%record) 
     21            self.messageWritten.emit('%s\n'%record) 
    4922 
    5023 
    51 # Define the default logger 
    52 logger = logging.getLogger() 
     24def setup_qt_logging(): 
     25    # Define the default logger 
     26    logger = logging.getLogger() 
    5327 
    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')) 
    59 # Add the qt-signal logger 
    60 handler = QtHandler() 
    61 handler.setFormatter(logging.Formatter("%(levelname)s: %(message)s")) 
    62 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    logger.addHandler(handler) 
     35 
     36    return handler 
  • src/sas/qtgui/Utilities/UnitTesting/SasviewLoggerTest.py

    r53c771e re4335ae  
    1111 
    1212# Local 
    13 from sas.qtgui.Utilities.SasviewLogger import XStream 
    1413from sas.qtgui.Utilities.SasviewLogger import QtHandler 
    1514 
     
    2322        """ 
    2423        self.logger = logging.getLogger(__name__) 
    25         handler = QtHandler() 
    26         handler.setFormatter(logging.Formatter("%(levelname)s: %(message)s")) 
    27         self.logger.addHandler(handler) 
     24        self.handler = QtHandler() 
     25        self.handler.setFormatter(logging.Formatter("%(levelname)s: %(message)s")) 
     26        self.logger.addHandler(self.handler) 
    2827        self.logger.setLevel(logging.DEBUG) 
    2928 
     
    3534        Test redirection of all levels of logging 
    3635        """ 
    37         # Attach the listeners 
    38         XStream.stderr().messageWritten.connect( self.outHandlerGui.insertPlainText ) 
    39         XStream.stdout().messageWritten.connect( self.outHandlerGui.insertPlainText ) 
     36        # Attach the listener 
     37        self.handler.messageWritten.connect(self.outHandlerGui.insertPlainText) 
    4038 
    4139        # Send the signals 
     
    4442        self.logger.warning('warning message') 
    4543        self.logger.error('error message') 
    46         sys.stdout.write('with stdout') 
    47         sys.stderr.write('with stderr') 
    4844 
    4945        out=self.outHandlerGui.toPlainText() 
     
    5450        self.assertIn('WARNING: warning message', out) 
    5551        self.assertIn('ERROR: error message', out) 
    56         self.assertIn('with stdout', out) 
    57         self.assertIn('with stderr', out) 
    5852 
    5953if __name__ == "__main__": 
Note: See TracChangeset for help on using the changeset viewer.