From 6e3a2e7f44e70d6f0eb0fe430dcdb2b2a4f39826 Mon Sep 17 00:00:00 2001 From: Sami Salonen Date: Thu, 25 Jun 2020 14:03:00 +0300 Subject: [PATCH 1/2] Remove stack trace printing to console. Signed-off-by: Sami Salonen --- src/main/java/net/wimpi/modbus/io/ModbusTCPTransport.java | 4 ++-- src/main/java/net/wimpi/modbus/io/ModbusUDPTransport.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/wimpi/modbus/io/ModbusTCPTransport.java b/src/main/java/net/wimpi/modbus/io/ModbusTCPTransport.java index fdf12dd..543827d 100755 --- a/src/main/java/net/wimpi/modbus/io/ModbusTCPTransport.java +++ b/src/main/java/net/wimpi/modbus/io/ModbusTCPTransport.java @@ -162,7 +162,7 @@ public ModbusRequest readRequest() throws ModbusIOException { // connection reset by peer, also EOF throw new ModbusIOException(true); } catch (Exception ex) { - ex.printStackTrace(); + // ex.printStackTrace(); throw new ModbusIOException("I/O exception - failed to read."); } }// readRequest @@ -223,7 +223,7 @@ public ModbusResponse readResponse() throws ModbusIOException { * return response; */ } catch (Exception ex) { - ex.printStackTrace(); + // ex.printStackTrace(); throw new ModbusIOException( String.format("I/O exception: %s %s", ex.getClass().getSimpleName(), ex.getMessage())); } diff --git a/src/main/java/net/wimpi/modbus/io/ModbusUDPTransport.java b/src/main/java/net/wimpi/modbus/io/ModbusUDPTransport.java index f9ac93e..231daf3 100755 --- a/src/main/java/net/wimpi/modbus/io/ModbusUDPTransport.java +++ b/src/main/java/net/wimpi/modbus/io/ModbusUDPTransport.java @@ -106,7 +106,7 @@ public ModbusResponse readResponse() throws ModbusIOException { } catch (InterruptedIOException ioex) { throw new ModbusIOException("Socket timed out."); } catch (Exception ex) { - ex.printStackTrace(); + // ex.printStackTrace(); throw new ModbusIOException("I/O exception - failed to read."); } }// readResponse From dcb8c951c019030c59ee0e9bc56f0920afbe63da Mon Sep 17 00:00:00 2001 From: Sami Salonen Date: Thu, 25 Jun 2020 14:13:50 +0300 Subject: [PATCH 2/2] Reduced logging verbosity to match modbus transport bundle. Most error and warn level logs are logged in transport bundle, no need to log them here. Signed-off-by: Sami Salonen --- .../net/wimpi/modbus/io/ModbusRTUTransport.java | 6 +++--- .../wimpi/modbus/io/ModbusSerialTransaction.java | 9 +++++---- .../wimpi/modbus/io/ModbusSerialTransport.java | 6 +++--- .../wimpi/modbus/io/ModbusTCPTransaction.java | 6 +++--- .../net/wimpi/modbus/net/SerialConnection.java | 16 ++++++++-------- .../wimpi/modbus/net/TCPMasterConnection.java | 4 ++-- .../net/wimpi/modbus/net/TCPSlaveConnection.java | 2 +- 7 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/main/java/net/wimpi/modbus/io/ModbusRTUTransport.java b/src/main/java/net/wimpi/modbus/io/ModbusRTUTransport.java index d8e110a..4ec65e2 100755 --- a/src/main/java/net/wimpi/modbus/io/ModbusRTUTransport.java +++ b/src/main/java/net/wimpi/modbus/io/ModbusRTUTransport.java @@ -167,8 +167,8 @@ public ModbusResponse readResponse() throws ModbusIOException { return response; } catch (Exception ex) { final String errMsg = "failed to read"; - logger.error("Last request: {}", ModbusUtil.toHex(lastRequest)); - logger.error("{}: {}", errMsg, ex.getMessage()); + logger.debug("Last request: {}", ModbusUtil.toHex(lastRequest)); + logger.debug("{}: {}", errMsg, ex.getMessage()); throw new ModbusIOException( String.format("I/O exception: %s %s", ex.getClass().getSimpleName(), ex.getMessage())); } finally { @@ -228,7 +228,7 @@ private void getResponse(int fn, BytesOutputStream out) throws IOException { out.write(inpBuf, 0, inpBytes); m_CommPort.disableReceiveThreshold(); if (inpBytes != bc + 2) { - logger.error("awaited {} bytes, but received {}", (bc + 2), inpBytes); + logger.debug("awaited {} bytes, but received {}", (bc + 2), inpBytes); } break; case 0x05: diff --git a/src/main/java/net/wimpi/modbus/io/ModbusSerialTransaction.java b/src/main/java/net/wimpi/modbus/io/ModbusSerialTransaction.java index ce61e1c..7c1d34d 100755 --- a/src/main/java/net/wimpi/modbus/io/ModbusSerialTransaction.java +++ b/src/main/java/net/wimpi/modbus/io/ModbusSerialTransaction.java @@ -184,7 +184,8 @@ public void execute() throws ModbusIOException, ModbusSlaveException, ModbusExce try { Thread.sleep(m_TransDelayMS); } catch (InterruptedException ex) { - logger.error("InterruptedException: {}", ex.getMessage()); + logger.debug("InterruptedException: {}", ex.getMessage()); + throw; } } @@ -195,12 +196,12 @@ public void execute() throws ModbusIOException, ModbusSlaveException, ModbusExce break; } catch (ModbusIOException e) { tries++; - logger.error( + logger.debug( "execute try {}/{} error: {}. Request: {} (unit id {} & transaction {}). Serial parameters: {}", tries, m_Retries + 1, e.getMessage(), m_Request, m_Request.getUnitID(), m_Request.getTransactionID(), m_SerialCon.getParameters()); if (tries >= m_Retries) { - logger.error( + logger.debug( "execute reached max tries {}, throwing last error: {}. Request: {} (unit id {} & transaction {}). Serial parameters: {}", m_Retries + 1, e.getMessage(), m_Request, m_Request.getUnitID(), m_Request.getTransactionID(), m_SerialCon.getParameters()); @@ -210,7 +211,7 @@ public void execute() throws ModbusIOException, ModbusSlaveException, ModbusExce } } while (true); if (tries > 0) { - logger.info( + logger.debug( "execute eventually succeeded with {} re-tries. Request: {} (unit id {} & transaction id {}). Serial parameters: {}", tries, m_Request, m_Request.getUnitID(), m_Request.getTransactionID(), m_SerialCon.getParameters()); diff --git a/src/main/java/net/wimpi/modbus/io/ModbusSerialTransport.java b/src/main/java/net/wimpi/modbus/io/ModbusSerialTransport.java index 81b9ad7..66dd25a 100755 --- a/src/main/java/net/wimpi/modbus/io/ModbusSerialTransport.java +++ b/src/main/java/net/wimpi/modbus/io/ModbusSerialTransport.java @@ -141,7 +141,7 @@ public void setReceiveThreshold(int th) { try { m_CommPort.enableReceiveThreshold(th); /* chars */ } catch (UnsupportedCommOperationException e) { - logger.error("Failed to setReceiveThreshold: {}", e.getMessage()); + logger.warn("Failed to setReceiveThreshold: {}", e.getMessage()); } } @@ -154,7 +154,7 @@ public void setReceiveTimeout(int ms) { try { m_CommPort.enableReceiveTimeout(ms); /* milliseconds */ } catch (UnsupportedCommOperationException e) { - logger.error("Failed to setReceiveTimeout: {}", e.getMessage()); + logger.warn("Failed to setReceiveTimeout: {}", e.getMessage()); } } @@ -177,7 +177,7 @@ public void readEcho(int len) throws IOException { m_CommPort.disableReceiveThreshold(); if (echoLen != len) { final String errMsg = "Echo not received"; - logger.error("Transmit {}", errMsg); + logger.debug("Transmit {}", errMsg); throw new IOException(errMsg); } }// readEcho diff --git a/src/main/java/net/wimpi/modbus/io/ModbusTCPTransaction.java b/src/main/java/net/wimpi/modbus/io/ModbusTCPTransaction.java index 4b58ae3..1a20753 100755 --- a/src/main/java/net/wimpi/modbus/io/ModbusTCPTransaction.java +++ b/src/main/java/net/wimpi/modbus/io/ModbusTCPTransaction.java @@ -208,12 +208,12 @@ public void execute() throws ModbusIOException, ModbusSlaveException, ModbusExce break; } catch (ModbusIOException ex) { tries++; - logger.error( + logger.debug( "execute try {}/{} error: {}. Request: {} (unit id {} & transaction {}). Address: {}:{}", tries, m_Retries + 1, ex.getMessage(), m_Request, m_Request.getUnitID(), m_Request.getTransactionID(), m_Connection.getAddress(), m_Connection.getPort()); if (tries >= m_Retries) { - logger.error( + logger.debug( "execute reached max tries {}, throwing last error: {}. Request: {} (unit id {} & transaction {}). Address: {}:{}", m_Retries + 1, ex.getMessage(), m_Request, m_Request.getUnitID(), m_Request.getTransactionID(), m_Connection.getAddress(), m_Connection.getPort()); @@ -225,7 +225,7 @@ public void execute() throws ModbusIOException, ModbusSlaveException, ModbusExce } while (true); if (tries > 0) { - logger.info( + logger.debug( "execute eventually succeeded with {} re-tries. Request: {} (unit id {} & transaction {}). Address: {}:{}", tries, m_Request, m_Request.getUnitID(), m_Request.getTransactionID(), m_Connection.getAddress(), m_Connection.getPort()); diff --git a/src/main/java/net/wimpi/modbus/net/SerialConnection.java b/src/main/java/net/wimpi/modbus/net/SerialConnection.java index 5d55045..e972165 100755 --- a/src/main/java/net/wimpi/modbus/net/SerialConnection.java +++ b/src/main/java/net/wimpi/modbus/net/SerialConnection.java @@ -122,7 +122,7 @@ public void open() throws Exception { m_PortIdentifyer = CommPortIdentifier.getPortIdentifier(m_Parameters.getPortName()); } catch (NoSuchPortException e) { final String errMsg = "Could not get port identifier, maybe insufficient permissions. " + e.getMessage(); - logger.error("Could not get port identifier, maybe insufficient permissions. {}: {}", + logger.debug("Could not get port identifier, maybe insufficient permissions. {}: {}", e.getClass().getName(), e.getMessage()); throw new Exception(errMsg); } @@ -133,7 +133,7 @@ public void open() throws Exception { m_SerialPort = m_PortIdentifyer.open("Modbus Serial Master", 30000); } catch (PortInUseException e) { String msg = "open port failed: " + e.getMessage(); - logger.error("open port failed: {}: {}", e.getClass().getName(), e.getMessage()); + logger.debug("open port failed: {}: {}", e.getClass().getName(), e.getMessage()); throw new Exception(msg); } logger.trace("Got Serial Port"); @@ -144,7 +144,7 @@ public void open() throws Exception { } catch (Exception e) { // ensure it is closed m_SerialPort.close(); - logger.error("parameter setup failed. {}: {}", e.getClass().getName(), e.getMessage()); + logger.debug("parameter setup failed. {}: {}", e.getClass().getName(), e.getMessage()); throw e; } @@ -169,7 +169,7 @@ public void open() throws Exception { } catch (IOException e) { m_SerialPort.close(); String msg = "Error opening i/o streams: " + e.getMessage(); - logger.error("Error opening i/o streams. {}: {}", e.getClass().getName(), e.getMessage()); + logger.debug("Error opening i/o streams. {}: {}", e.getClass().getName(), e.getMessage()); throw new Exception(msg); } logger.trace("i/o Streams prepared"); @@ -180,7 +180,7 @@ public void open() throws Exception { } catch (TooManyListenersException e) { m_SerialPort.close(); final String errMsg = "too many listeners added:" + e.getMessage(); - logger.error("too many listeners added. {}: {}", e.getClass().getName(), e.getMessage()); + logger.debug("too many listeners added. {}: {}", e.getClass().getName(), e.getMessage()); throw new Exception(errMsg); } @@ -196,7 +196,7 @@ public void setReceiveTimeout(int ms) { try { m_SerialPort.enableReceiveTimeout(ms); } catch (UnsupportedCommOperationException e) { - logger.error("Failed to set receive timeout: {}", e.getMessage()); + logger.warn("Failed to set receive timeout: {}", e.getMessage()); } }// setReceiveTimeout @@ -261,13 +261,13 @@ public void close() { try { m_Transport.close(); } catch (IOException e) { - logger.error("Error occurred when closing transport. {}: {}", e.getClass().getName(), e.getMessage()); + logger.warn("Error occurred when closing transport. {}: {}", e.getClass().getName(), e.getMessage()); } try { m_SerialIn.close(); } catch (IOException e) { - logger.error("Error occurred when closing serial input stream. {}: {}", e.getClass().getName(), + logger.warn("Error occurred when closing serial input stream. {}: {}", e.getClass().getName(), e.getMessage()); } diff --git a/src/main/java/net/wimpi/modbus/net/TCPMasterConnection.java b/src/main/java/net/wimpi/modbus/net/TCPMasterConnection.java index d090442..02664ff 100755 --- a/src/main/java/net/wimpi/modbus/net/TCPMasterConnection.java +++ b/src/main/java/net/wimpi/modbus/net/TCPMasterConnection.java @@ -107,7 +107,7 @@ public void close() { try { m_ModbusTransport.close(); } catch (IOException ex) { - logger.debug("close()"); + logger.warn("close()", ex); } m_Connected = false; } @@ -157,7 +157,7 @@ public void setTimeout(int timeout) { try { m_Socket.setSoTimeout(m_Timeout); } catch (IOException ex) { - logger.error("Could not set socket timeout on connection {} {}: {}", getAddress(), getPort(), + logger.warn("Could not set socket timeout on connection {} {}: {}", getAddress(), getPort(), ex.getMessage()); } } diff --git a/src/main/java/net/wimpi/modbus/net/TCPSlaveConnection.java b/src/main/java/net/wimpi/modbus/net/TCPSlaveConnection.java index eda559b..59efaea 100755 --- a/src/main/java/net/wimpi/modbus/net/TCPSlaveConnection.java +++ b/src/main/java/net/wimpi/modbus/net/TCPSlaveConnection.java @@ -67,7 +67,7 @@ public TCPSlaveConnection(Socket socket, ModbusSocketBasedTransportFactory trans setSocket(socket); } catch (IOException ex) { final String errMsg = "Socket invalid"; - logger.debug(errMsg); + logger.warn(errMsg); // @commentstart@ throw new IllegalStateException(errMsg); // @commentend@