WIP161201

thobaben_serialize
Harald Wolff 2016-12-01 20:40:09 +01:00
parent a133b70953
commit 6423400249
4 changed files with 88 additions and 4 deletions

View File

@ -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;
}
}

View File

@ -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));
}
}

View File

@ -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; };

View File

@ -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<LoggingListener> loggingListeners;
private int limitLogLevel;
public Logging(String filename){
loggingListeners = new LinkedList<LoggingListener>();
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() {