]> git.uio.no Git - ifi-stolz-refaktor.git/blame - software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/utils/RefaktorDebug.java
MethodSignature: fixing api mismatch between Java 6 and 7
[ifi-stolz-refaktor.git] / software / no.uio.ifi.refaktor / src / no / uio / ifi / refaktor / utils / RefaktorDebug.java
CommitLineData
35df1ff7
EK
1package no.uio.ifi.refaktor.utils;
2
b37b9825
EK
3import java.io.File;
4import java.io.IOException;
83d73136 5import java.io.PrintStream;
fc87f36a
EK
6import java.io.PrintWriter;
7import java.io.StringWriter;
35df1ff7 8import java.util.Set;
b37b9825
EK
9import java.util.logging.FileHandler;
10import java.util.logging.Handler;
11import java.util.logging.Level;
12import java.util.logging.Logger;
13import java.util.logging.SimpleFormatter;
35df1ff7
EK
14
15import org.eclipse.ltk.core.refactoring.Change;
16import org.eclipse.ltk.core.refactoring.CompositeChange;
17import org.eclipse.text.edits.TextEdit;
18
f9881d70 19public class RefaktorDebug {
fc87f36a 20
b37b9825
EK
21 static {
22 createLogger();
23 }
fc87f36a 24
41d536e4 25 private static boolean debug = true;
83d73136 26 private static PrintStream out;
b37b9825 27 private static Logger logger;
fc87f36a 28
c04308e1 29 public static void initialize() {
83d73136 30 out = System.err;
41d536e4 31 debug = Boolean.parseBoolean(System.getProperty("refaktor.debug", "true"));
c04308e1
EK
32 println("(Refaktor Debug active)");
33 }
fc87f36a 34
eaddf2a7
EK
35 public static void println() {
36 println("");
37 }
38
83d73136
EK
39 public static void println(Object o) {
40 println(o.toString());
41 }
fc87f36a 42
83d73136
EK
43 public static void print(Object o) {
44 print(o.toString());
45 }
fc87f36a 46
eaddf2a7
EK
47 public static void println(Object caller, Object object) {
48 println(caller, object.toString());
49 }
50
51 public static void println(Object caller, String string) {
52 println(caller.getClass().getSimpleName() + ": " + string);
53 }
54
c04308e1 55 public static void println(String str) {
baa1ab45
EK
56 if (!debug)
57 return;
fc87f36a 58
baa1ab45 59 out.println(str);
c04308e1 60 }
fc87f36a 61
c04308e1
EK
62 public static void print(String str) {
63 if (!debug)
64 return;
fc87f36a 65
83d73136 66 out.print(str);
c04308e1 67 }
fc87f36a 68
35df1ff7 69 public static void printChange(Change change, Set<Change> s, int i) {
c04308e1
EK
70 if (!debug)
71 return;
fc87f36a 72
35df1ff7 73 s.add(change);
83d73136 74 println("Level " + i++ + ": " + change.getClass());
35df1ff7
EK
75 CompositeChange c = (CompositeChange) change.getAdapter(CompositeChange.class);
76 if (c != null) {
77 for(Change ch: c.getChildren()) {
78 if (!s.contains(ch))
79 printChange(ch, s, i);
80 }
81 }
82 }
fc87f36a 83
35df1ff7 84 public static void printTextEdit(TextEdit edit, int i) {
c04308e1
EK
85 if (!debug)
86 return;
fc87f36a 87
83d73136 88 println(i++ + ": " + edit.getClass());
35df1ff7
EK
89 for (TextEdit t: edit.getChildren())
90 printTextEdit(t, i);
fc87f36a
EK
91
92 }
93
94 public static void log(Throwable t) {
95 StringWriter stringWriter = new StringWriter();
96 t.printStackTrace(new PrintWriter(stringWriter));
97 log(stringWriter.toString());
35df1ff7 98 }
b37b9825
EK
99
100 public static void log(String string) {
101 logger.log(Level.SEVERE, string);
102 }
103
104 private static void createLogger() {
105 logger = Logger.getLogger(RefaktorDebug.class.getCanonicalName());
106 logger.setLevel(Level.ALL);
107
8e5dbebc
EK
108 File homeDirectory = new File(System.getProperty("user.home"));
109 File defaultDirectory = new File(homeDirectory, "no.uio.ifi.refaktor.logs");
b37b9825 110
8e5dbebc
EK
111 File loggingDirectory = new File(System.getProperty("refaktor.logs", defaultDirectory.getAbsolutePath()));
112
113 if (!loggingDirectory.exists())
114 loggingDirectory.mkdir();
b37b9825
EK
115
116 try {
8e5dbebc 117 Handler handler = new FileHandler(loggingDirectory.getAbsolutePath() + "/no.uio.ifi.refaktor.%g.log", true);
b37b9825
EK
118 handler.setFormatter(new SimpleFormatter());
119 logger.addHandler(handler);
120 } catch (SecurityException e) {
121 e.printStackTrace();
122 } catch (IOException e) {
123 e.printStackTrace();
124 }
125 }
35df1ff7 126}