thobaben_serialize
Harald Wolff 2017-08-24 18:03:03 +02:00
parent 75d208589a
commit e4b422a71d
6 changed files with 86 additions and 33 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,9 +1,10 @@
package org.hwo.collections;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class SortedList<T extends Comparable<T>> {
public class SortedList<T extends Comparable<T>> implements Iterable<T>{
private List<T> backingStore;
@ -17,7 +18,7 @@ public class SortedList<T extends Comparable<T>> {
public T after(T element){
for (int i=0;i<backingStore.size();i++){
System.err.format("after(): compare = %d\n", backingStore.get(i).compareTo(element));
//System.err.format("after(): compare = %d\n", backingStore.get(i).compareTo(element));
if (backingStore.get(i).compareTo(element) > 0)
return backingStore.get(i);
}
@ -46,6 +47,10 @@ public class SortedList<T extends Comparable<T>> {
return true;
}
public void clear(){
this.backingStore.clear();
}
public boolean remove(T element){
return backingStore.remove( element );
}
@ -72,6 +77,11 @@ public class SortedList<T extends Comparable<T>> {
public T[] toArray(T[] a){
return backingStore.toArray(a);
}
@Override
public Iterator<T> iterator() {
return backingStore.iterator();
}

View File

@ -362,13 +362,13 @@ public class NewSerialPort {
public void write(byte[] b, int off, int len) throws IOException {
int nWritten = 0;
log(DEBUGDETAIL,"NSP::write(...)");
//log(DEBUGDETAIL,"NSP::write(...)");
for (int redo=0; redo < 5 ; redo++){
nWritten = nsp_write_bytes(nsp, b, off, len);
if (nWritten == EAGAIN){
try {
log(DEBUGDETAIL,"SL-TX-EAGAIN... [%d]",redo);
// log(DEBUGDETAIL,"SL-TX-EAGAIN... [%d]",redo);
Thread.sleep(5);
} catch (Exception e){

View File

@ -6,7 +6,9 @@ import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.text.DateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
@ -19,42 +21,61 @@ public class Logging {
private PrintStream logStream;
private final DateFormat dateFormat;
private DateFormat dateFormat;
List<LoggingListener> loggingListeners;
private int limitLogLevel;
private String logFilename;
public Logging(){
setLogFileName("application.log");
initialize();
}
public Logging(String filename){
setLogFileName(filename);
initialize();
}
private void initialize(){
loggingListeners = new LinkedList<LoggingListener>();
limitLogLevel = 10;
dateFormat = DateFormat.getDateTimeInstance();
try {
File f = new File(filename);
File fold = new File(String.format("%s.old", filename));
if (fold.exists()){
fold.delete();
}
if (f.exists()){
f.renameTo(fold);
}
logStream = new PrintStream(filename);
} catch (FileNotFoundException e) {
liveStream.println("----!!!!! LOGGING ERROR !!!!!-----");
e.printStackTrace();
}
System.setErr(new PrintStream(new LoggingOutputStream(LogLevel.ERROR)));
System.setOut(new PrintStream(new LoggingOutputStream(LogLevel.INFO)));
}
public PrintStream getLogStream(){
if (this.logStream == null){
try {
logStream = new PrintStream(logFilename);
} catch (FileNotFoundException e){
logStream = liveStream;
log(LogLevel.ERROR,"Logging: setLogFileName(%s) failed:",logFilename);
log(e);
}
}
return logStream;
}
public void setLogFileName(String filename){
this.logFilename = filename;
File f = new File(logFilename);
File fold = new File(String.format("%s.old", logFilename));
if (fold.exists()){
fold.delete();
}
if (f.exists()){
f.renameTo(fold);
}
}
public int getLimitLogLevel() {
return limitLogLevel;
}
@ -77,8 +98,8 @@ public class Logging {
formattedLine = String.format("%s [%-12s] %s", dateFormat.format(new Date()),logLevel.toString(),message);
logStream.println(formattedLine);
logStream.flush();
getLogStream().println(formattedLine);
getLogStream().flush();
if (liveStream != null){
liveStream.println(formattedLine);
@ -89,12 +110,7 @@ public class Logging {
l.logMessageArrived(formattedLine);
}
}
static public void setLogFileName(String logFileName){
_inst = new Logging(logFileName);
}
static public void log(LogLevel logLevel,String message){
if (_inst == null){
_inst = new Logging("application.log");
@ -140,6 +156,33 @@ public class Logging {
return _inst;
}
static public String[] Init(String[] arguments,Class<?> mainClass){
return Init(arguments, mainClass.getCanonicalName() + ".log");
}
static public String[] Init(String[] arguments,String defaultFileName){
LinkedList<String> args = new LinkedList<>();
Iterator<String> i = Arrays.asList(arguments).iterator();
Logging._inst = new Logging();
Logging._inst.setLogFileName(defaultFileName);
while (i.hasNext()){
String option = i.next();
switch (option){
case "--log-filename":
Logging._inst.setLogFileName(i.next());
break;
case "--log-level":
Logging._inst.setLimitLogLevel(Integer.parseInt(i.next()));
break;
default:
args.add(option);
break;
}
}
return args.toArray(new String[0]);
}
static {
liveStream = System.out;
}