java/ch/wlkl/env/IOSimple.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ch.wlkl.env;
/**
*
* @author walter
*/
public class IOSimple {
final static String[] indents = new String[20];
static {
indents[0] = "";
for (int i = 1; i < indents.length; i++) {
indents[i] = indents[i - 1] + " ";
}
}
;
public int outInd = 0;
public int debugLevel;
final All2S all2s;
String linePa = "";
public IOSimple(int dbg, All2S as) {
debugLevel = dbg;
all2s = as;
}
public IOSimple() {
this(-1, new All2S());
}
public void out(String s) {
outln(outInd == 0 && linePa.length() == 0 ? s : indents[outInd] + linePa + s);
linePa = "";
}
public void outln(String s) {
System.out.println(s);
}
public void outPa(String s) {
linePa += s;
}
final public void outFi(String s) {
System.out.println(s);
}
final static String[] hd = {"###", "***", "---"};
public String hdTxt(int l, String s) {
return l > 2 ? s : hd[l] + " " + s + " " + hd[l] + hd[l] + hd[l];
}
final public void hd(int l, String s) {
out(hdTxt(l, s));
}
final public void hdFi(int l, String s) {
outFi(hdTxt(l, s));
}
public void out(Object... o) {
all2s.a2s(o);
all2s.after("\n" + indents[outInd + 1]);
out(all2s.flush());
}
public void outPa(Object... o) {
all2s.a2s(o);
outPa(all2s.flush());
}
public IOSimple ind(int d) {
outInd += d;
return this;
}
public String msg(int hl, String m, String tab, Object... o) {
String r = m;
String b = linePa;
int oSt = all2s.printStackTrace;
if (oSt == 0) {
all2s.printStackTrace = 1;
}
linePa = "";
hd(hl, m); // output it as early as possible, in case of recursive errors!
all2s.after("\n" + tab);
b += all2s.flush();
if (b.length() != 0) {
b = tab + "<<<before: " + b + ">>>";
out(b);
r += '\n' + b;
}
if (o != null && o.length > 0) {
all2s.restart(); // want current state of all objects and children
all2s.a2s(o);
all2s.after("\n" + tab);
b = tab + "<<<info: " + all2s.flush() + " >>>";
out(b);
r += '\n' + b;
}
all2s.printStackTrace = oSt;
return r;
}
public static String srcAt(String s, int pos) {
return " at " + pos + ": " + (s == null || pos >= s.length() ? "" : s.length() - pos < 15 ? s.substring(pos) : s.substring(pos, pos + 15) + "...") + " in " + s;
}
public void dy(String m, Object... o) {
all2s.restart();
msg(1, "dy " + m, " ", o);
throw new Error("dy " + m);
}
public void dy(String m, String s, int pos) {
dy(m, srcAt(s, pos));
}
public void err(String m, Object... o) {
msg(2, "error " + m, " ", o);
}
public String assFail(String m, Object... o) {
all2s.restart();
return msg(2, "assert failed " + m, " ", o);
}
public String assFail(String m, String s, int pos) {
return assFail(m, srcAt(s, pos));
}
public void debug(int l, String m, Object... o) {
if (l <= debugLevel) {
int oldInd = outInd;
outInd = l;
msg(9, "debug " + m, indents[l+1], o);
outInd = oldInd;
}
}
}