1 | import sys |
---|
2 | import unittest |
---|
3 | import logging |
---|
4 | |
---|
5 | from PyQt5.QtGui import * |
---|
6 | from PyQt5.QtCore import * |
---|
7 | from PyQt5.QtWidgets import * |
---|
8 | |
---|
9 | # set up import paths |
---|
10 | import sas.qtgui.path_prepare |
---|
11 | |
---|
12 | # Local |
---|
13 | from sas.qtgui.Utilities.SasviewLogger import XStream |
---|
14 | from sas.qtgui.Utilities.SasviewLogger import QtHandler |
---|
15 | |
---|
16 | if not QApplication.instance(): |
---|
17 | app = QApplication(sys.argv) |
---|
18 | |
---|
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) |
---|
58 | |
---|
59 | if __name__ == "__main__": |
---|
60 | unittest.main() |
---|