From 6423400249b033f88a4db238087f025cc8cbfcb2 Mon Sep 17 00:00:00 2001 From: Harald Wolff Date: Thu, 1 Dec 2016 20:40:09 +0100 Subject: [PATCH] WIP161201 --- .../hwo/exceptions/ExtendedExceptionBase.java | 27 +++++++++++++ .../hwo/io/NewSerialPort/NewSerialPort.java | 20 ++++++++++ src/org/hwo/logging/LogLevel.java | 7 +++- src/org/hwo/logging/Logging.java | 38 +++++++++++++++++-- 4 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 src/org/hwo/exceptions/ExtendedExceptionBase.java diff --git a/src/org/hwo/exceptions/ExtendedExceptionBase.java b/src/org/hwo/exceptions/ExtendedExceptionBase.java new file mode 100644 index 0000000..cc8d365 --- /dev/null +++ b/src/org/hwo/exceptions/ExtendedExceptionBase.java @@ -0,0 +1,27 @@ +package org.hwo.exceptions; + +public abstract class ExtendedExceptionBase extends Exception { + + + public ExtendedExceptionBase(String message) { + super(message); + } + + public ExtendedExceptionBase(Throwable cause){ + super(cause); + } + + public ExtendedExceptionBase(String message,Throwable cause){ + super(message,cause); + } + + public String getDebugMessage(){ + return null; + } + public String getDebugDetails(){ + return null; + } + + + +} diff --git a/src/org/hwo/io/NewSerialPort/NewSerialPort.java b/src/org/hwo/io/NewSerialPort/NewSerialPort.java index 661db45..c2cb111 100644 --- a/src/org/hwo/io/NewSerialPort/NewSerialPort.java +++ b/src/org/hwo/io/NewSerialPort/NewSerialPort.java @@ -267,6 +267,16 @@ public class NewSerialPort { if (ch < 0){ if (ch == nsp_RET_TIMEOUT) return -1; + + if (ch == nsp_RET_NOTOPEN){ + NewSerialPort.this.close(); + return -1; + } + if (ch == nsp_RET_OTHER){ + NewSerialPort.this.close(); + NewSerialPort.this.open(); + } + throw new IOException(String.format("nsp_read()=%d", ch)); }; return ch; @@ -282,8 +292,18 @@ public class NewSerialPort { if (autoOpen && !isOpen()) open(); + if (!isOpen()) + throw new IOException("Port not opened"); + r = nsp_write(nsp, arg0); if (r<0){ + if (r == nsp_RET_NOTOPEN) + NewSerialPort.this.close(); + if (r == nsp_RET_OTHER){ + NewSerialPort.this.close(); + NewSerialPort.this.open(); + } + throw new IOException(String.format("nsp_write()=%d", r)); } } diff --git a/src/org/hwo/logging/LogLevel.java b/src/org/hwo/logging/LogLevel.java index fa2f789..b25d48d 100644 --- a/src/org/hwo/logging/LogLevel.java +++ b/src/org/hwo/logging/LogLevel.java @@ -1,7 +1,12 @@ package org.hwo.logging; public enum LogLevel { - INFO(1),WARN(3),ERROR(5),FATAL(10),DEBUG(25); + FATAL(0), // Fehler welche zum Programmabbruch führen + ERROR(2), // Fehler, Ereignisse welche erwartetes Verhalten beeinflussen / verhindern + WARN(5), // Warnungen, Ereignisse welche zu weiteren Problemen ("Fehlern") führen können + INFO(10), // Allgemeine Informationen (Fortschritt, etc.) + DEBUG(25), // Ausgaben zur Fehlersuche + DEBUGDETAIL(30); // Detailausgaben zur Fehlersuche private final int n; LogLevel(int l){ n=l; }; diff --git a/src/org/hwo/logging/Logging.java b/src/org/hwo/logging/Logging.java index da0ad3e..07c9b22 100644 --- a/src/org/hwo/logging/Logging.java +++ b/src/org/hwo/logging/Logging.java @@ -10,6 +10,8 @@ import java.util.Date; import java.util.LinkedList; import java.util.List; +import org.hwo.exceptions.ExtendedExceptionBase; + public class Logging { private static Logging _inst; @@ -21,8 +23,11 @@ public class Logging { List loggingListeners; + private int limitLogLevel; + public Logging(String filename){ loggingListeners = new LinkedList(); + limitLogLevel = 10; dateFormat = DateFormat.getDateTimeInstance(); try { @@ -50,6 +55,13 @@ public class Logging { } + public int getLimitLogLevel() { + return limitLogLevel; + } + public void setLimitLogLevel(int limitLogLevel) { + this.limitLogLevel = limitLogLevel; + } + public void addLoggingListener(LoggingListener listener){ loggingListeners.add(listener); } @@ -60,6 +72,9 @@ public class Logging { private void _log(LogLevel logLevel,String message){ String formattedLine; + if (logLevel.getLevel() > limitLogLevel) + return; + formattedLine = String.format("%s [%-8s] %s", dateFormat.format(new Date()),logLevel.toString(),message); logStream.println(formattedLine); @@ -99,9 +114,26 @@ public class Logging { log(LogLevel.INFO,message,args); } - static public void log(Exception e){ - log(LogLevel.ERROR,"An Exception occured: %s [%s]",e.getClass().getName(), e.getMessage()); - e.printStackTrace(); + static public void log(Exception e){ + log(LogLevel.ERROR,"Exception: %s [%s]",e.getClass().getName(), e.getMessage()); + if (ExtendedExceptionBase.class.isInstance(e)){ + ExtendedExceptionBase ee = (ExtendedExceptionBase)e; + if (ee.getDebugMessage()!=null) + log(LogLevel.DEBUG,String.format("%s: %s", e.getClass().getName(),ee.getDebugMessage())); + if (ee.getDebugDetails()!=null) + log(LogLevel.DEBUGDETAIL,String.format("%s: %s",e.getClass().getName(),ee.getDebugDetails())); + log(LogLevel.DEBUG,formatStackTrace(ee.getStackTrace())); + } + } + + static String formatStackTrace(StackTraceElement[] stackTrace){ + StringBuilder sb = new StringBuilder(); + + for (StackTraceElement ste: stackTrace){ + sb.append(String.format("-> %s.%s() = %s:%s\n", ste.getClassName(),ste.getMethodName(),ste.getFileName(),ste.getLineNumber())); + } + + return sb.toString(); } static public Logging getInstance() {