Commit b207ee82 authored by Guillaume Seguin's avatar Guillaume Seguin

Reset handlers in Pronterface, grab & restore stdout & stderr

parent 0a5dca82
...@@ -65,28 +65,32 @@ from .pronsole import REPORT_NONE, REPORT_POS, REPORT_TEMP ...@@ -65,28 +65,32 @@ from .pronsole import REPORT_NONE, REPORT_POS, REPORT_TEMP
class ConsoleOutputHandler(object): class ConsoleOutputHandler(object):
"""Handle console output. All messages go through the logging submodule. We setup a logging handler to get logged messages and write them to both stdout (unless a log file path is specified, in which case we add another logging handler to write to this file) and the log panel. """Handle console output. All messages go through the logging submodule. We setup a logging handler to get logged messages and write them to both stdout (unless a log file path is specified, in which case we add another logging handler to write to this file) and the log panel.
When no file path is specified, we also redirect stdout to ourself to catch print messages and al.""" We also redirect stdout and stderr to ourself to catch print messages and al."""
def __init__(self, target, log_path): def __init__(self, target, log_path):
self.stdout = sys.stdout
self.stderr = sys.stderr
sys.stdout = self
sys.stderr = self
if log_path: if log_path:
self.stdout = None self.print_on_stdout = False
setup_logging(self, log_path) setup_logging(self, log_path, reset_handlers = True)
self.target = target self.target = target
else: else:
self.stdout = sys.stdout self.print_on_stdout = True
sys.stdout = self
setup_logging(sys.stdout) setup_logging(sys.stdout)
self.target = target self.target = target
def __del__(self): def __del__(self):
sys.stdout = self.stdout sys.stdout = self.stdout
sys.stderr = self.stderr
def write(self, data): def write(self, data):
try: try:
self.target(data) self.target(data)
except: except:
pass pass
if self.stdout: if self.print_on_stdout:
try: try:
data = data.encode("utf-8") data = data.encode("utf-8")
except: except:
......
...@@ -47,9 +47,11 @@ class LogFormatter(logging.Formatter): ...@@ -47,9 +47,11 @@ class LogFormatter(logging.Formatter):
self._fmt = self.format_default self._fmt = self.format_default
return super(LogFormatter, self).format(record) return super(LogFormatter, self).format(record)
def setup_logging(out, filepath = None): def setup_logging(out, filepath = None, reset_handlers = False):
logger = logging.getLogger() logger = logging.getLogger()
logger.setLevel(logging.INFO) logger.setLevel(logging.INFO)
if reset_handlers:
logger.handlers = []
formatter = LogFormatter("[%(levelname)s] %(message)s", "%(message)s") formatter = LogFormatter("[%(levelname)s] %(message)s", "%(message)s")
logging_handler = logging.StreamHandler(out) logging_handler = logging.StreamHandler(out)
logging_handler.setFormatter(formatter) logging_handler.setFormatter(formatter)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment