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