a83d15c8677a05a60cde20f840b4b9e3ab7e846f
[fleet.git] / src / edu / berkeley / fleet / interpreter / Log.java
1 package edu.berkeley.fleet.interpreter;
2 import edu.berkeley.sbp.util.ANSI;
3 import edu.berkeley.fleet.api.*;
4 import java.io.*;
5
6 public class Log {
7
8     public static boolean ansi_color = true;
9     public static PrintWriter log = new PrintWriter(new OutputStreamWriter(System.out));
10     public static boolean quiet = false;
11
12     public static void print(Object o) {
13         if (log==null || quiet) 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 || quiet) return;
23         try {
24             log.println(o);
25             log.flush();
26         } catch (Exception e) {
27             throw new RuntimeException(e);
28         }
29     }
30
31     /*
32     public static void dispatch(Dispatchable d) {
33         println(ANSI.green("dispatch: " + indent(d+"", "          ")));
34     }
35     */
36     public static void dispatch(Instruction d) {
37         println(ANSI.green("dispatch: " + indent(d+"", "          ")));
38     }
39
40     public static void data(String data, Dock source, Destination dest) {
41         println(("    data: ") + indent(ANSI.purple(data) +
42                                         (source==null ? "" :
43                                          (" : " + source))+(" -> "+ANSI.purple(""+dest)), "          "));
44     }
45
46     public static void token(Dock source, Destination dest) {
47         println(ANSI.purple("   token: ") + (source + " -> " + ANSI.purple(dest+"")));
48     }
49
50     public static String clreol() { return ""; }
51     public static void error(Object o) { println(ANSI.red(o)); }
52
53     public static String indent(String s, String indent) {
54         StringBuffer ret = new StringBuffer();
55         for(int i=0; i<s.length(); i++) {
56             char c = s.charAt(i);
57             if (!(c=='\n' && i==s.length()-1))
58                 ret.append(c);
59             if (c=='\n' && i<s.length()-1)
60                 ret.append(indent);
61         }
62         return ret.toString();
63     }
64 }