[0cd8612] | 1 | import sys |
---|
| 2 | import unittest |
---|
| 3 | import logging |
---|
| 4 | |
---|
[53c771e] | 5 | from PyQt5.QtGui import * |
---|
| 6 | from PyQt5.QtCore import * |
---|
| 7 | from PyQt5.QtWidgets import * |
---|
[0cd8612] | 8 | |
---|
[31c5b58] | 9 | # set up import paths |
---|
[83eb5208] | 10 | import sas.qtgui.path_prepare |
---|
[31c5b58] | 11 | |
---|
[0cd8612] | 12 | # Local |
---|
[83eb5208] | 13 | from sas.qtgui.Utilities.SasviewLogger import XStream |
---|
| 14 | from sas.qtgui.Utilities.SasviewLogger import QtHandler |
---|
[0cd8612] | 15 | |
---|
[464cd07] | 16 | if not QApplication.instance(): |
---|
| 17 | app = QApplication(sys.argv) |
---|
[0cd8612] | 18 | |
---|
[a95260d] | 19 | class SasviewLoggerTest(unittest.TestCase): |
---|
| 20 | def setUp(self): |
---|
| 21 | """ |
---|
| 22 | Prepare the logger |
---|
| 23 | """ |
---|
| 24 | self.logger = logging.getLogger(__name__) |
---|
| 25 | handler = QtHandler() |
---|
| 26 | handler.setFormatter(logging.Formatter("%(levelname)s: %(message)s")) |
---|
| 27 | self.logger.addHandler(handler) |
---|
| 28 | self.logger.setLevel(logging.DEBUG) |
---|
| 29 | |
---|
| 30 | self.outHandlerGui=QTextBrowser() |
---|
| 31 | |
---|
| 32 | |
---|
| 33 | def testQtHandler(self): |
---|
| 34 | """ |
---|
| 35 | Test redirection of all levels of logging |
---|
| 36 | """ |
---|
| 37 | # Attach the listeners |
---|
| 38 | XStream.stderr().messageWritten.connect( self.outHandlerGui.insertPlainText ) |
---|
| 39 | XStream.stdout().messageWritten.connect( self.outHandlerGui.insertPlainText ) |
---|
| 40 | |
---|
| 41 | # Send the signals |
---|
| 42 | self.logger.debug('debug message') |
---|
| 43 | self.logger.info('info message') |
---|
| 44 | self.logger.warning('warning message') |
---|
| 45 | self.logger.error('error message') |
---|
| 46 | sys.stdout.write('with stdout') |
---|
| 47 | sys.stderr.write('with stderr') |
---|
| 48 | |
---|
| 49 | out=self.outHandlerGui.toPlainText() |
---|
| 50 | |
---|
| 51 | # Assure everything got logged |
---|
| 52 | self.assertIn('DEBUG: debug message', out) |
---|
| 53 | self.assertIn('INFO: info message', out) |
---|
| 54 | self.assertIn('WARNING: warning message', out) |
---|
| 55 | self.assertIn('ERROR: error message', out) |
---|
| 56 | self.assertIn('with stdout', out) |
---|
| 57 | self.assertIn('with stderr', out) |
---|
[0cd8612] | 58 | |
---|
| 59 | if __name__ == "__main__": |
---|
| 60 | unittest.main() |
---|