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
import sys
from printrun.printcore import printcore
from printrun.utils import setup_logging
from printrun import gcoder
if __name__ == '__main__':
setup_logging(sys.stderr)
baud = 115200
loud = False
statusreport = False
......
......@@ -25,7 +25,9 @@ import time
import platform
import os
import sys
stdin, stdout, stderr = sys.stdin, sys.stdout, sys.stderr
reload(sys).setdefaultencoding('utf8')
sys.stdin, sys.stdout, sys.stderr = stdin, stdout, stderr
import logging
import traceback
import errno
......@@ -34,11 +36,9 @@ import re
from functools import wraps
from collections import deque
from printrun import gcoder
from .utils import install_locale, decode_utf8, setup_logging
from .utils import install_locale, decode_utf8
install_locale('pronterface')
setup_logging(sys.stderr)
def locked(f):
@wraps(f)
def inner(*args, **kw):
......
......@@ -65,28 +65,32 @@ from .pronsole import REPORT_NONE, REPORT_POS, REPORT_TEMP
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.
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):
self.stdout = sys.stdout
self.stderr = sys.stderr
sys.stdout = self
sys.stderr = self
if log_path:
self.stdout = None
setup_logging(self, log_path)
self.print_on_stdout = False
setup_logging(self, log_path, reset_handlers = True)
self.target = target
else:
self.stdout = sys.stdout
sys.stdout = self
self.print_on_stdout = True
setup_logging(sys.stdout)
self.target = target
def __del__(self):
sys.stdout = self.stdout
sys.stderr = self.stderr
def write(self, data):
try:
self.target(data)
except:
pass
if self.stdout:
if self.print_on_stdout:
try:
data = data.encode("utf-8")
except:
......
......@@ -47,15 +47,18 @@ class LogFormatter(logging.Formatter):
self._fmt = self.format_default
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.setLevel(logging.INFO)
formatter = LogFormatter("[%(levelname)s] %(message)s", "%(message)s")
if reset_handlers:
logger.handlers = []
formatter = LogFormatter("[%(levelname)s] %(message)s", "%(message)s")
logging_handler = logging.StreamHandler(out)
logging_handler.setFormatter(formatter)
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")
logging_handler = logging.FileHandler(filepath)
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