Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class FBConsoleWrapper(QtCore.QObject):
- """
- This wrapper class creates an object that emits a signal whenever
- console output is written.
- """
- def __init__(self, parent, stdout=True):
- """
- The constructor.
- This redirects the system logging to internal class variables that
- can be recalled.
- :param parent: ``QWidget`` that acts as parent for the console widget
- :param stdout: ``bool`` determining if ``stdout`` logging should be captured.
- :return: ``None``
- """
- # Call base constructor
- super(FBConsoleWrapper, self).__init__(self, parent)
- if stdout:
- self._stream = sys.stdout
- sys.stdout = self
- else:
- self._stream = sys.stderr
- sys.stderr = self
- self._stdout = stdout
- # Define custom QSignal to be emitted when logging has been output to
- self.logging_written_to = QtCore.Signal(object, object)
- def __getattr__(self, name):
- """
- This method is called if the attribute ``name`` has not already been
- explicitly defined.
- :param name: ``string`` that is the attribute to get on the object.
- :return: ``None``
- """
- # Get the stream logging output and return it
- return getattr(self._stream, name)
- def __del__(self):
- """
- The destructor.
- This redirects system logging back to the normal streams.
- :return:
- """
- try:
- if self._stdout:
- sys.stdout = self._stream
- else:
- sys.stderr = self._stream
- except AttributeError:
- pass
- def write(self, text):
- """
- This method is called whenever there is new text to write to the
- logger stream.
- :param text: ``string`` containing logging output to write.
- :return: ``None``
- """
- self._stream.write(text)
- self.logging_written_to.emit(text, self._stdout)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement