package tracer;
import java.util.ArrayList;
public class MyPrintStream extends Thread {
private static MyPrintStream theInstance;
private static ArrayList<String> output;
private static String s;
// private static boolean bFlushed = false;
// private static int count = 0;
private static MyPrintStream getInstance() {
if (theInstance == null) {
theInstance = new MyPrintStream();
output = new ArrayList<String>();
Runtime.getRuntime().addShutdownHook(theInstance); // シャットダウン用
// theInstance.start();
}
return theInstance;
}
public static void print(int n) {
getInstance()._print(n);
}
public static void print(String s) {
getInstance()._print(s);
}
public static void println() {
getInstance()._println();
}
public static void println(String s) {
getInstance()._println(s);
}
public void run() {
// if (count == 0) {
// // 通常のトレース出力
// count++;
// String s;
// Runtime.getRuntime().addShutdownHook(new MyPrintStream()); // シャットダウン用にもうひとつインスタンスを作成する
// while(!bFlushed) {
// try {
// Thread.sleep(10);
// if (output.size() > 0) {
// synchronized (output) {
// s = output.remove(0);
// }
// System.out.println(s);
// }
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
// } else {
// シャットダウン時にバッファに残ったトレースを出力し切る
// bFlushed = true;
for (int n = 0; n < output.size(); n++) {
System.out.println(output.get(n));
}
// }
}
private void _print(int n) {
if (s == null) s = new String();
s += n;
}
private void _print(String s1) {
if (s == null) s = new String();
s += s1;
}
private void _println() {
if (s == null) s = new String();
synchronized (output) {
output.add(s);
}
s = new String();
}
private void _println(String s1) {
if (s == null) s = new String();
s += s1;
synchronized (output) {
output.add(s);
}
s = new String();
}
}