Commit 2db76474 authored by Guillaume Seguin's avatar Guillaume Seguin

Merge branch 'master' of github.com:kliment/Printrun

parents 3f75c3f9 d25d792b
...@@ -20,9 +20,11 @@ import getopt ...@@ -20,9 +20,11 @@ import getopt
import sys import sys
from printrun.printcore import printcore from printrun.printcore import printcore
from printrun.utils import setup_logging
from printrun import gcoder from printrun import gcoder
if __name__ == '__main__': if __name__ == '__main__':
setup_logging(sys.stderr)
baud = 115200 baud = 115200
loud = False loud = False
statusreport = False statusreport = False
......
...@@ -25,7 +25,9 @@ import time ...@@ -25,7 +25,9 @@ import time
import platform import platform
import os import os
import sys import sys
stdin, stdout, stderr = sys.stdin, sys.stdout, sys.stderr
reload(sys).setdefaultencoding('utf8') reload(sys).setdefaultencoding('utf8')
sys.stdin, sys.stdout, sys.stderr = stdin, stdout, stderr
import logging import logging
import traceback import traceback
import errno import errno
...@@ -34,11 +36,9 @@ import re ...@@ -34,11 +36,9 @@ import re
from functools import wraps from functools import wraps
from collections import deque from collections import deque
from printrun import gcoder from printrun import gcoder
from .utils import install_locale, decode_utf8, setup_logging from .utils import install_locale, decode_utf8
install_locale('pronterface') install_locale('pronterface')
setup_logging(sys.stderr)
def locked(f): def locked(f):
@wraps(f) @wraps(f)
def inner(*args, **kw): def inner(*args, **kw):
......
...@@ -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,15 +47,18 @@ class LogFormatter(logging.Formatter): ...@@ -47,15 +47,18 @@ 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")
logger.handlers = []
logging_handler = logging.StreamHandler(out) logging_handler = logging.StreamHandler(out)
logging_handler.setFormatter(formatter) logging_handler.setFormatter(formatter)
logger.addHandler(logging_handler) logger.addHandler(logging_handler)
if filepath is not None: if filepath:
if os.path.isdir(filepath):
filepath = os.path.join(filepath, "printrun.log")
formatter = LogFormatter("%(asctime)s - [%(levelname)s] %(message)s", "%(asctime)s - %(message)s") formatter = LogFormatter("%(asctime)s - [%(levelname)s] %(message)s", "%(asctime)s - %(message)s")
logging_handler = logging.FileHandler(filepath) logging_handler = logging.FileHandler(filepath)
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