added fleet api classes
[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         try {
14             log.print(o);
15         } catch (Exception e) {
16             throw new RuntimeException(e);
17         }
18     }
19     public static void println() { println(""); }
20     public static void println(Object o) {
21         try {
22             log.println(o);
23             log.flush();
24         } catch (Exception e) {
25             throw new RuntimeException(e);
26         }
27     }
28
29     public static void dispatch(Dispatchable d) {
30         println(green("dispatch: " + indent(d+"", "          ")));
31     }
32     public static void dispatch(Instruction d) {
33         println(green("dispatch: " + indent(d+"", "          ")));
34     }
35
36     public static void data(String data, BenkoBox source, BenkoBox dest) {
37         println(("    data: ") + indent(purple(data) +
38                                         (source==null ? "" :
39                                          (" : " + source))+(" -> "+purple(""+dest)), "          "));
40     }
41
42     public static void token(BenkoBox source, BenkoBox dest) {
43         println(purple("   token: ") + (source + " -> " + purple(dest+"")));
44     }
45
46     public static String clreol() { return ""; }
47
48     public static String black(Object o) { if (!ansi_color) return o+""; return o+""; }
49     public static String red(Object o) { if (!ansi_color) return o+""; return "\033[31m"+o+"\033[0m"; }
50     public static String green(Object o) { if (!ansi_color) return o+""; return "\033[32m"+o+"\033[0m"; }
51     public static String yellow(Object o) { if (!ansi_color) return o+""; return "\033[33m"+o+"\033[0m"; }
52     public static String blue(Object o) { if (!ansi_color) return o+""; return "\033[34m"+o+"\033[0m"; }
53     public static String purple(Object o) { if (!ansi_color) return o+""; return "\033[35m"+o+"\033[0m"; }
54     public static String cyan(Object o) { if (!ansi_color) return o+""; return "\033[36m"+o+"\033[0m"; }
55     public static String invert(Object o) { if (!ansi_color) return o+""; return "\033[7m"+o+"\033[0m"; }
56     public static String bold(Object o) { if (!ansi_color) return o+""; return "\033[1m"+o+"\033[0m"; }
57
58     public static void error(Object o) { println(red(o)); }
59
60     public static String indent(String s, String indent) {
61         StringBuffer ret = new StringBuffer();
62         for(int i=0; i<s.length(); i++) {
63             char c = s.charAt(i);
64             if (!(c=='\n' && i==s.length()-1))
65                 ret.append(c);
66             if (c=='\n' && i<s.length()-1)
67                 ret.append(indent);
68         }
69         return ret.toString();
70     }
71 }