Commit 585bad62 authored by Guillaume Seguin's avatar Guillaume Seguin

Log errors using errorcb in printcore

parent 8002d268
...@@ -107,6 +107,13 @@ class printcore(): ...@@ -107,6 +107,13 @@ class printcore():
self.z_feedrate = None self.z_feedrate = None
self.pronterface = None self.pronterface = None
def logError(self, error):
if self.errorcb:
try: self.errorcb(error)
except: pass
else:
logging.error(error)
@locked @locked
def disconnect(self): def disconnect(self):
"""Disconnects from printer and pauses the print """Disconnects from printer and pauses the print
...@@ -165,7 +172,7 @@ class printcore(): ...@@ -165,7 +172,7 @@ 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:
logging.error(_("Could not connect to %s:%s:") % (hostname, port) + self.logError(_("Could not connect to %s:%s:") % (hostname, port) +
"\n" + _("Socket error %s:") % e.errno + "\n" + _("Socket error %s:") % e.errno +
"\n" + e.strerror) "\n" + e.strerror)
self.printer = None self.printer = None
...@@ -179,7 +186,7 @@ class printcore(): ...@@ -179,7 +186,7 @@ class printcore():
baudrate = self.baud, baudrate = self.baud,
timeout = 0.25) timeout = 0.25)
except SerialException as e: except SerialException as e:
logging.error(_("Could not connect to %s at baudrate %s:") % (self.port, self.baud) + self.logError(_("Could not connect to %s at baudrate %s:") % (self.port, self.baud) +
"\n" + _("Serial error: %s") % e) "\n" + _("Serial error: %s") % e)
self.printer = None self.printer = None
return return
...@@ -214,21 +221,21 @@ class printcore(): ...@@ -214,21 +221,21 @@ class printcore():
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]:
logging.error(_(u"Can't read from printer (disconnected?) (SelectError {0}): {1}").format(e.errno, decode_utf8(e.strerror))) self.logError(_(u"Can't read from printer (disconnected?) (SelectError {0}): {1}").format(e.errno, decode_utf8(e.strerror)))
return None return None
else: else:
logging.error(_(u"SelectError ({0}): {1}").format(e.errno, decode_utf8(e.strerror))) self.logError(_(u"SelectError ({0}): {1}").format(e.errno, decode_utf8(e.strerror)))
raise raise
except SerialException as e: except SerialException as e:
logging.error(_(u"Can't read from printer (disconnected?) (SerialException): {0}").format(decode_utf8(str(e)))) self.logError(_(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:
logging.error(_(u"Can't read from printer (disconnected?) (Socket error {0}): {1}").format(e.errno, decode_utf8(e.strerror))) self.logError(_(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 ""
logging.error(_(u"Can't read from printer (disconnected?) (OS Error {0}): {1}").format(e.errno, e.strerror)) self.logError(_(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):
...@@ -285,10 +292,7 @@ class printcore(): ...@@ -285,10 +292,7 @@ class printcore():
try: self.tempcb(line) try: self.tempcb(line)
except: pass except: pass
elif line.startswith('Error'): elif line.startswith('Error'):
if self.errorcb: self.logError(line)
#callback for errors
try: self.errorcb(line)
except: pass
# Teststrings for resend parsing # Firmware exp. result # Teststrings for resend parsing # Firmware exp. result
# line="rs N2 Expected checksum 67" # Teacup 2 # line="rs N2 Expected checksum 67" # Teacup 2
if line.lower().startswith("resend") or line.startswith("rs"): if line.lower().startswith("resend") or line.startswith("rs"):
...@@ -434,7 +438,7 @@ class printcore(): ...@@ -434,7 +438,7 @@ class printcore():
else: else:
self.priqueue.put_nowait(command) self.priqueue.put_nowait(command)
else: else:
logging.error(_("Not connected to printer.")) self.logError(_("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
...@@ -442,7 +446,7 @@ class printcore(): ...@@ -442,7 +446,7 @@ class printcore():
if self.online: if self.online:
self.priqueue.put_nowait(command) self.priqueue.put_nowait(command)
else: else:
logging.error(_("Not connected to printer.")) self.logError(_("Not connected to printer."))
def _print(self, resuming = False): def _print(self, resuming = False):
self._stop_sender() self._stop_sender()
...@@ -451,7 +455,7 @@ class printcore(): ...@@ -451,7 +455,7 @@ class printcore():
#callback for printing started #callback for printing started
try: self.startcb(resuming) try: self.startcb(resuming)
except: except:
logging.error(_("Print start callback failed with:") + self.logError(_("Print start callback failed with:") +
"\n" + traceback.format_exc()) "\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()
...@@ -462,10 +466,10 @@ class printcore(): ...@@ -462,10 +466,10 @@ class printcore():
#callback for printing done #callback for printing done
try: self.endcb() try: self.endcb()
except: except:
logging.error(_("Print end callback failed with:") + self.logError(_("Print end callback failed with:") +
"\n" + traceback.format_exc()) "\n" + traceback.format_exc())
except: except:
logging.error(_("Print thread died due to the following error:") + self.logError(_("Print thread died due to the following error:") +
"\n" + traceback.format_exc()) "\n" + traceback.format_exc())
finally: finally:
self.print_thread = None self.print_thread = None
...@@ -553,8 +557,8 @@ class printcore(): ...@@ -553,8 +557,8 @@ 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:
logging.error(_("Warning: could not analyze command %s:") % command + logging.warning(_("Could not analyze command %s:") % command +
"\n" + traceback.format_exc()) "\n" + traceback.format_exc())
if self.loud: if self.loud:
logging.info("SENT: %s" % command) logging.info("SENT: %s" % command)
if self.sendcb: if self.sendcb:
...@@ -565,11 +569,11 @@ class printcore(): ...@@ -565,11 +569,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:
logging.error(_(u"Can't write to printer (disconnected?) (Socket error {0}): {1}").format(e.errno, decode_utf8(e.strerror))) self.logError(_(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:
logging.error(_(u"Can't write to printer (disconnected?) (SerialException): {0}").format(decode_utf8(str(e)))) self.logError(_(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:
logging.error(_(u"Socket connection broken, disconnected. ({0}): {1}").format(e.errno, decode_utf8(e.strerror))) self.logError(_(u"Socket connection broken, disconnected. ({0}): {1}").format(e.errno, decode_utf8(e.strerror)))
self.writefailures += 1 self.writefailures += 1
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