914e524ed58b5765d15bed8a0a24ddcddb670c8a
[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 PrintWriter log = new PrintWriter(new OutputStreamWriter(System.out));
9     public static boolean quiet = false;
10
11     public static void print(Object o) {
12         if (log==null || quiet) return;
13         try {
14             log.print(o);
15         } catch (Exception e) { throw new RuntimeException(e); }
16     }
17     public static void println() { println(""); }
18     public static void println(Object o) {
19         if (log==null || quiet) return;
20         try {
21             log.println(o);
22             log.flush();
23         } catch (Exception e) { throw new RuntimeException(e); }
24     }
25
26     public static void packet(Packet p) {
27         BitVector data = p.getValue();
28         Dock source = p.getSource();
29         Destination dest = p.getDestination();
30         if (dest.getDock().getInstructionDestination()==dest) {
31             if (p.isToken()) {
32                 println(ANSI.yellow(ANSI.bold("  torpedo: ")) + (source + " -> " + ANSI.yellow(ANSI.bold(dest+""))));
33             } else {
34                 Instruction d = source.getShip().getFleet().decodeInstruction(data, source);
35                 println(ANSI.red("dispatch: " + indent(d+"", "          ")));
36             }
37         } else {
38             if (p.isToken()) {
39                 println(ANSI.blue(ANSI.bold("   token: ")) + (source + " -> " + ANSI.blue(ANSI.bold(dest+""))));
40             } else {
41                 println(indent(ANSI.cyan("    data: "+data) +
42                                (source==null ? "" :
43                                 (" : " + source))+(" -> "+ANSI.cyan(""+dest)), "          "));
44             }
45         }
46     }
47
48     public static void error(Object o) { println(ANSI.red(o)); }
49
50     public static String indent(String s, String indent) {
51         StringBuffer ret = new StringBuffer();
52         for(int i=0; i<s.length(); i++) {
53             char c = s.charAt(i);
54             if (!(c=='\n' && i==s.length()-1))
55                 ret.append(c);
56             if (c=='\n' && i<s.length()-1)
57                 ret.append(indent);
58         }
59         return ret.toString();
60     }
61 }