more fixes to bring things up to date with the new test/doc apis
[fleet.git] / src / edu / berkeley / fleet / interpreter / Log.java
1 package edu.berkeley.fleet.interpreter;
2 import edu.berkeley.fleet.api.*;
3 import edu.berkeley.fleet.api.Instruction;
4 import java.io.*;
5
6 public class Log {
7
8     public static boolean ansi_color = true;
9
10     public static PrintWriter log = new PrintWriter(new OutputStreamWriter(System.out));
11
12     public static void print(Object o) {
13         if (log==null) return;
14         try {
15             log.print(o);
16         } catch (Exception e) {
17             throw new RuntimeException(e);
18         }
19     }
20     public static void println() { println(""); }
21     public static void println(Object o) {
22         if (log==null) return;
23         try {
24             log.println(o);
25             log.flush();
26         } catch (Exception e) {
27             throw new RuntimeException(e);
28         }
29     }
30
31     public static void dispatch(Dispatchable d) {
32         println(green("dispatch: " + indent(d+"", "          ")));
33     }
34     public static void dispatch(Instruction d) {
35         println(green("dispatch: " + indent(d+"", "          ")));
36     }
37
38     public static void data(String data, BenkoBox source, BenkoBox dest) {
39         println(("    data: ") + indent(purple(data) +
40                                         (source==null ? "" :
41                                          (" : " + source))+(" -> "+purple(""+dest)), "          "));
42     }
43
44     public static void token(BenkoBox source, BenkoBox dest) {
45         println(purple("   token: ") + (source + " -> " + purple(dest+"")));
46     }
47
48     public static String clreol() { return ""; }
49
50     public static String black(Object o) { if (!ansi_color) return o+""; return o+""; }
51     public static String red(Object o) { if (!ansi_color) return o+""; return "\033[31m"+o+"\033[0m"; }
52     public static String green(Object o) { if (!ansi_color) return o+""; return "\033[32m"+o+"\033[0m"; }
53     public static String yellow(Object o) { if (!ansi_color) return o+""; return "\033[33m"+o+"\033[0m"; }
54     public static String blue(Object o) { if (!ansi_color) return o+""; return "\033[34m"+o+"\033[0m"; }
55     public static String purple(Object o) { if (!ansi_color) return o+""; return "\033[35m"+o+"\033[0m"; }
56     public static String cyan(Object o) { if (!ansi_color) return o+""; return "\033[36m"+o+"\033[0m"; }
57     public static String invert(Object o) { if (!ansi_color) return o+""; return "\033[7m"+o+"\033[0m"; }
58     public static String bold(Object o) { if (!ansi_color) return o+""; return "\033[1m"+o+"\033[0m"; }
59
60     public static void error(Object o) { println(red(o)); }
61
62     public static String indent(String s, String indent) {
63         StringBuffer ret = new StringBuffer();
64         for(int i=0; i<s.length(); i++) {
65             char c = s.charAt(i);
66             if (!(c=='\n' && i==s.length()-1))
67                 ret.append(c);
68             if (c=='\n' && i<s.length()-1)
69                 ret.append(indent);
70         }
71         return ret.toString();
72     }
73 }