Commit b54c8ba8 authored by Guillaume Seguin's avatar Guillaume Seguin

Merge branch 'logging'

parents e47bc618 f34a8c91
...@@ -22,7 +22,6 @@ from select import error as SelectError ...@@ -22,7 +22,6 @@ from select import error as SelectError
from threading import Thread, Lock from threading import Thread, Lock
from Queue import Queue, Empty as QueueEmpty from Queue import Queue, Empty as QueueEmpty
import time import time
import sys
import platform import platform
import os import os
import traceback import traceback
...@@ -35,6 +34,9 @@ from printrun.GCodeAnalyzer import GCodeAnalyzer ...@@ -35,6 +34,9 @@ from printrun.GCodeAnalyzer import GCodeAnalyzer
from printrun.printrun_utils import install_locale, decode_utf8 from printrun.printrun_utils import install_locale, decode_utf8
install_locale('pronterface') install_locale('pronterface')
import logging
logging.basicConfig(format = "%(levelname)s: %(message)s")
def locked(f): def locked(f):
@wraps(f) @wraps(f)
def inner(*args, **kw): def inner(*args, **kw):
...@@ -163,11 +165,11 @@ class printcore(): ...@@ -163,11 +165,11 @@ class printcore():
self.printer_tcp.settimeout(self.timeout) self.printer_tcp.settimeout(self.timeout)
self.printer = self.printer_tcp.makefile() self.printer = self.printer_tcp.makefile()
except socket.error as e: except socket.error as e:
print _("Could not connect to %s:%s:") % (hostname, port) logging.error(_("Could not connect to %s:%s:") % (hostname, port) +
"\n" + _("Socket error %s:") % e.errno +
"\n" + e.strerror)
self.printer = None self.printer = None
self.printer_tcp = None self.printer_tcp = None
print _("Socket error %s:") % e.errno,
print e.strerror
return return
else: else:
disable_hup(self.port) disable_hup(self.port)
...@@ -177,9 +179,9 @@ class printcore(): ...@@ -177,9 +179,9 @@ class printcore():
baudrate = self.baud, baudrate = self.baud,
timeout = 0.25) timeout = 0.25)
except SerialException as e: except SerialException as e:
print _("Could not connect to %s at baudrate %s:") % (self.port, self.baud) logging.error(_("Could not connect to %s at baudrate %s:") % (self.port, self.baud) +
"\n" + _("Serial error: %s") % e)
self.printer = None self.printer = None
print _("Serial error: %s") % e
return return
self.stop_read_thread = False self.stop_read_thread = False
self.read_thread = Thread(target = self._listen) self.read_thread = Thread(target = self._listen)
...@@ -208,25 +210,25 @@ class printcore(): ...@@ -208,25 +210,25 @@ class printcore():
if self.recvcb: if self.recvcb:
try: self.recvcb(line) try: self.recvcb(line)
except: pass except: pass
if self.loud: print "RECV:", line.rstrip() if self.loud: logging.info("RECV: %s" % line.rstrip())
return line return line
except SelectError as e: except SelectError as e:
if 'Bad file descriptor' in e.args[1]: if 'Bad file descriptor' in e.args[1]:
print _(u"Can't read from printer (disconnected?) (SelectError {0}): {1}").format(e.errno, decode_utf8(e.strerror)) logging.error(_(u"Can't read from printer (disconnected?) (SelectError {0}): {1}").format(e.errno, decode_utf8(e.strerror)))
return None return None
else: else:
print _(u"SelectError ({0}): {1}").format(e.errno, decode_utf8(e.strerror)) logging.error(_(u"SelectError ({0}): {1}").format(e.errno, decode_utf8(e.strerror)))
raise raise
except SerialException as e: except SerialException as e:
print _(u"Can't read from printer (disconnected?) (SerialException): {0}").format(decode_utf8(str(e))) logging.error(_(u"Can't read from printer (disconnected?) (SerialException): {0}").format(decode_utf8(str(e))))
return None return None
except socket.error as e: except socket.error as e:
print _(u"Can't read from printer (disconnected?) (Socket error {0}): {1}").format(e.errno, decode_utf8(e.strerror)) logging.error(_(u"Can't read from printer (disconnected?) (Socket error {0}): {1}").format(e.errno, decode_utf8(e.strerror)))
return None return None
except OSError as e: except OSError as e:
if e.errno == errno.EAGAIN: # Not a real error, no data was available if e.errno == errno.EAGAIN: # Not a real error, no data was available
return "" return ""
print _(u"Can't read from printer (disconnected?) (OS Error {0}): {1}").format(e.errno, e.strerror) logging.error(_(u"Can't read from printer (disconnected?) (OS Error {0}): {1}").format(e.errno, e.strerror))
return None return None
def _listen_can_continue(self): def _listen_can_continue(self):
...@@ -435,7 +437,7 @@ class printcore(): ...@@ -435,7 +437,7 @@ class printcore():
else: else:
self.priqueue.put_nowait(command) self.priqueue.put_nowait(command)
else: else:
print "Not connected to printer." logging.error(_("Not connected to printer."))
def send_now(self, command, wait = 0): def send_now(self, command, wait = 0):
"""Sends a command to the printer ahead of the command queue, without a """Sends a command to the printer ahead of the command queue, without a
...@@ -443,7 +445,7 @@ class printcore(): ...@@ -443,7 +445,7 @@ class printcore():
if self.online: if self.online:
self.priqueue.put_nowait(command) self.priqueue.put_nowait(command)
else: else:
print "Not connected to printer." logging.error(_("Not connected to printer."))
def _print(self, resuming = False): def _print(self, resuming = False):
self._stop_sender() self._stop_sender()
...@@ -452,8 +454,8 @@ class printcore(): ...@@ -452,8 +454,8 @@ class printcore():
#callback for printing started #callback for printing started
try: self.startcb(resuming) try: self.startcb(resuming)
except: except:
print "Print start callback failed with:" logging.error(_("Print start callback failed with:") +
traceback.print_exc(file = sys.stdout) "\n" + traceback.format_exc())
while self.printing and self.printer and self.online: while self.printing and self.printer and self.online:
self._sendnext() self._sendnext()
self.sentlines = {} self.sentlines = {}
...@@ -463,11 +465,11 @@ class printcore(): ...@@ -463,11 +465,11 @@ class printcore():
#callback for printing done #callback for printing done
try: self.endcb() try: self.endcb()
except: except:
print "Print end callback failed with:" logging.error(_("Print end callback failed with:") +
traceback.print_exc(file = sys.stdout) "\n" + traceback.format_exc())
except: except:
print "Print thread died due to the following error:" logging.error(_("Print thread died due to the following error:") +
traceback.print_exc(file = sys.stdout) "\n" + traceback.format_exc())
finally: finally:
self.print_thread = None self.print_thread = None
self._start_sender() self._start_sender()
...@@ -554,10 +556,10 @@ class printcore(): ...@@ -554,10 +556,10 @@ class printcore():
# run the command through the analyzer # run the command through the analyzer
try: self.analyzer.Analyze(command) try: self.analyzer.Analyze(command)
except: except:
print "Warning: could not analyze command %s:" % command logging.error(_("Warning: could not analyze command %s:") % command +
traceback.print_exc(file = sys.stdout) "\n" + traceback.format_exc())
if self.loud: if self.loud:
print "SENT:", command logging.info("SENT: %s" % command)
if self.sendcb: if self.sendcb:
try: self.sendcb(command) try: self.sendcb(command)
except: pass except: pass
...@@ -566,11 +568,11 @@ class printcore(): ...@@ -566,11 +568,11 @@ class printcore():
if self.printer_tcp: self.printer.flush() if self.printer_tcp: self.printer.flush()
self.writefailures = 0 self.writefailures = 0
except socket.error as e: except socket.error as e:
print _(u"Can't write to printer (disconnected?) (Socket error {0}): {1}").format(e.errno, decode_utf8(e.strerror)) logging.error(_(u"Can't write to printer (disconnected?) (Socket error {0}): {1}").format(e.errno, decode_utf8(e.strerror)))
self.writefailures += 1 self.writefailures += 1
except SerialException as e: except SerialException as e:
print _(u"Can't write to printer (disconnected?) (SerialException): {0}").format(decode_utf8(str(e))) logging.error(_(u"Can't write to printer (disconnected?) (SerialException): {0}").format(decode_utf8(str(e))))
self.writefailures += 1 self.writefailures += 1
except RuntimeError as e: except RuntimeError as e:
print _(u"Socket connection broken, disconnected. ({0}): {1}").format(e.errno, decode_utf8(e.strerror)) logging.error(_(u"Socket connection broken, disconnected. ({0}): {1}").format(e.errno, decode_utf8(e.strerror)))
self.writefailures += 1 self.writefailures += 1
...@@ -25,6 +25,7 @@ import codecs ...@@ -25,6 +25,7 @@ import codecs
import shlex import shlex
import argparse import argparse
import locale import locale
import logging
from . import printcore from . import printcore
from printrun.printrun_utils import install_locale from printrun.printrun_utils import install_locale
...@@ -402,7 +403,7 @@ class pronsole(cmd.Cmd): ...@@ -402,7 +403,7 @@ class pronsole(cmd.Cmd):
print u"".join(unicode(i) for i in msg) print u"".join(unicode(i) for i in msg)
def logError(self, *msg): def logError(self, *msg):
print u"".join(unicode(i) for i in msg) logging.error(u"".join(unicode(i) for i in msg))
def promptf(self): def promptf(self):
"""A function to generate prompts so that we can do dynamic prompts. """ """A function to generate prompts so that we can do dynamic prompts. """
...@@ -558,6 +559,7 @@ class pronsole(cmd.Cmd): ...@@ -558,6 +559,7 @@ class pronsole(cmd.Cmd):
if macro_def.strip() == "": if macro_def.strip() == "":
self.logError("Empty macro - cancelled") self.logError("Empty macro - cancelled")
return return
macro = None
pycode = "def macro(self,*arg):\n" pycode = "def macro(self,*arg):\n"
if "\n" not in macro_def.strip(): if "\n" not in macro_def.strip():
pycode += self.compile_macro_line(" " + macro_def.strip()) pycode += self.compile_macro_line(" " + macro_def.strip())
...@@ -1345,7 +1347,7 @@ class pronsole(cmd.Cmd): ...@@ -1345,7 +1347,7 @@ class pronsole(cmd.Cmd):
try: try:
while True: while True:
self.p.send_now("M105") self.p.send_now("M105")
if(self.sdprinting): if self.sdprinting:
self.p.send_now("M27") self.p.send_now("M27")
time.sleep(interval) time.sleep(interval)
#print (self.tempreadings.replace("\r", "").replace("T", "Hotend").replace("B", "Bed").replace("\n", "").replace("ok ", "")) #print (self.tempreadings.replace("\r", "").replace("T", "Hotend").replace("B", "Bed").replace("\n", "").replace("ok ", ""))
...@@ -1551,4 +1553,3 @@ class pronsole(cmd.Cmd): ...@@ -1551,4 +1553,3 @@ class pronsole(cmd.Cmd):
readline.set_completer(self.old_completer) readline.set_completer(self.old_completer)
except ImportError: except ImportError:
pass pass
...@@ -27,6 +27,9 @@ import cStringIO as StringIO ...@@ -27,6 +27,9 @@ import cStringIO as StringIO
import subprocess import subprocess
import shlex import shlex
import glob import glob
import logging
from . import pronsole
from . import printcore
from printrun.printrun_utils import install_locale, RemainingTimeEstimator from printrun.printrun_utils import install_locale, RemainingTimeEstimator
install_locale('pronterface') install_locale('pronterface')
...@@ -34,7 +37,7 @@ install_locale('pronterface') ...@@ -34,7 +37,7 @@ install_locale('pronterface')
try: try:
import wx import wx
except: except:
print _("WX is not installed. This program requires WX to run.") logging.error(_("WX is not installed. This program requires WX to run."))
raise raise
from printrun.pronterface_widgets import SpecialButton, MacroEditor, \ from printrun.pronterface_widgets import SpecialButton, MacroEditor, \
...@@ -49,8 +52,6 @@ if os.name == "nt": ...@@ -49,8 +52,6 @@ if os.name == "nt":
from printrun.printrun_utils import iconfile, configfile from printrun.printrun_utils import iconfile, configfile
from printrun.gui import MainWindow from printrun.gui import MainWindow
from printrun.excluder import Excluder from printrun.excluder import Excluder
from . import pronsole
from . import printcore
from pronsole import dosify, wxSetting, HiddenSetting, StringSetting, SpinSetting, FloatSpinSetting, BooleanSetting, StaticTextSetting from pronsole import dosify, wxSetting, HiddenSetting, StringSetting, SpinSetting, FloatSpinSetting, BooleanSetting, StaticTextSetting
from printrun import gcoder from printrun import gcoder
...@@ -213,7 +214,7 @@ class PronterWindow(MainWindow, pronsole.pronsole): ...@@ -213,7 +214,7 @@ class PronterWindow(MainWindow, pronsole.pronsole):
self.settings._add(BooleanSetting("lockbox", False, _("Display interface lock checkbox"), _("Display a checkbox that, when check, locks most of Pronterface"), "UI")) self.settings._add(BooleanSetting("lockbox", False, _("Display interface lock checkbox"), _("Display a checkbox that, when check, locks most of Pronterface"), "UI"))
self.settings._add(BooleanSetting("lockonstart", False, _("Lock interface upon print start"), _("If lock checkbox is enabled, lock the interface when starting a print"), "UI")) self.settings._add(BooleanSetting("lockonstart", False, _("Lock interface upon print start"), _("If lock checkbox is enabled, lock the interface when starting a print"), "UI"))
self.settings._add(HiddenSetting("last_bed_temperature", 0.0)) self.settings._add(HiddenSetting("last_bed_temperature", 0.0))
self.settings._add(HiddenSetting("last_file_path", "")) self.settings._add(HiddenSetting("last_file_path", u""))
self.settings._add(HiddenSetting("last_temperature", 0.0)) self.settings._add(HiddenSetting("last_temperature", 0.0))
self.settings._add(HiddenSetting("last_extrusion", 5.0)) self.settings._add(HiddenSetting("last_extrusion", 5.0))
self.settings._add(HiddenSetting("default_extrusion", 5.0)) self.settings._add(HiddenSetting("default_extrusion", 5.0))
...@@ -298,10 +299,10 @@ class PronterWindow(MainWindow, pronsole.pronsole): ...@@ -298,10 +299,10 @@ class PronterWindow(MainWindow, pronsole.pronsole):
rco.write(_("# I moved all your custom buttons into .pronsolerc.\n# Please don't add them here any more.\n# Backup of your old buttons is in custombtn.old\n")) rco.write(_("# I moved all your custom buttons into .pronsolerc.\n# Please don't add them here any more.\n# Backup of your old buttons is in custombtn.old\n"))
rco.close() rco.close()
except IOError, x: except IOError, x:
print str(x) logging.error(str(x))
else: else:
print _("Note!!! You have specified custom buttons in both custombtn.txt and .pronsolerc") logging.warning(_("Note!!! You have specified custom buttons in both custombtn.txt and .pronsolerc"))
print _("Ignoring custombtn.txt. Remove all current buttons to revert to custombtn.txt") logging.warning(_("Ignoring custombtn.txt. Remove all current buttons to revert to custombtn.txt"))
except: except:
pass pass
...@@ -1475,7 +1476,7 @@ Printrun. If not, see <http://www.gnu.org/licenses/>.""" ...@@ -1475,7 +1476,7 @@ Printrun. If not, see <http://www.gnu.org/licenses/>."""
wx.CallAfter(self.gviz.Refresh) wx.CallAfter(self.gviz.Refresh)
if self.p.online: if self.p.online:
if self.p.writefailures >= 4: if self.p.writefailures >= 4:
print _("Disconnecting after 4 failed writes.") logging.error(_("Disconnecting after 4 failed writes."))
self.status_thread = None self.status_thread = None
self.disconnect() self.disconnect()
return return
...@@ -1605,7 +1606,7 @@ Printrun. If not, see <http://www.gnu.org/licenses/>.""" ...@@ -1605,7 +1606,7 @@ Printrun. If not, see <http://www.gnu.org/licenses/>."""
if config: if config:
fpath = os.path.join(self.slic3r_configpath, cat, config) fpath = os.path.join(self.slic3r_configpath, cat, config)
pararray += ["--load", fpath] pararray += ["--load", fpath]
print "Slicing: " + " ".join(pararray) print _("Slicing ") + " ".join(pararray)
self.skeinp = subprocess.Popen(pararray, stderr = subprocess.STDOUT, stdout = subprocess.PIPE) self.skeinp = subprocess.Popen(pararray, stderr = subprocess.STDOUT, stdout = subprocess.PIPE)
while True: while True:
o = self.skeinp.stdout.read(1) o = self.skeinp.stdout.read(1)
...@@ -1614,7 +1615,7 @@ Printrun. If not, see <http://www.gnu.org/licenses/>.""" ...@@ -1614,7 +1615,7 @@ Printrun. If not, see <http://www.gnu.org/licenses/>."""
self.skeinp.wait() self.skeinp.wait()
self.stopsf = 1 self.stopsf = 1
except: except:
print _("Failed to execute slicing software: ") logging.error(_("Failed to execute slicing software: "))
self.stopsf = 1 self.stopsf = 1
traceback.print_exc(file = sys.stdout) traceback.print_exc(file = sys.stdout)
...@@ -1835,10 +1836,10 @@ Printrun. If not, see <http://www.gnu.org/licenses/>.""" ...@@ -1835,10 +1836,10 @@ Printrun. If not, see <http://www.gnu.org/licenses/>."""
except SerialException as e: except SerialException as e:
# Currently, there is no errno, but it should be there in the future # Currently, there is no errno, but it should be there in the future
if e.errno == 2: if e.errno == 2:
print _("Error: You are trying to connect to a non-existing port.") logging.error(_("Error: You are trying to connect to a non-existing port."))
elif e.errno == 8: elif e.errno == 8:
print _("Error: You don't have permission to open %s.") % port logging.error(_("Error: You don't have permission to open %s.") % port)
print _("You might need to add yourself to the dialout group.") logging.error(_("You might need to add yourself to the dialout group."))
else: else:
traceback.print_exc() traceback.print_exc()
# Kill the scope anyway # Kill the scope anyway
......
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