]>
Commit | Line | Data |
---|---|---|
35df1ff7 EK |
1 | package no.uio.ifi.refaktor.utils; |
2 | ||
b37b9825 EK |
3 | import java.io.File; |
4 | import java.io.IOException; | |
83d73136 | 5 | import java.io.PrintStream; |
fc87f36a EK |
6 | import java.io.PrintWriter; |
7 | import java.io.StringWriter; | |
35df1ff7 | 8 | import java.util.Set; |
b37b9825 EK |
9 | import java.util.logging.FileHandler; |
10 | import java.util.logging.Handler; | |
11 | import java.util.logging.Level; | |
12 | import java.util.logging.Logger; | |
13 | import java.util.logging.SimpleFormatter; | |
35df1ff7 EK |
14 | |
15 | import org.eclipse.ltk.core.refactoring.Change; | |
16 | import org.eclipse.ltk.core.refactoring.CompositeChange; | |
17 | import org.eclipse.text.edits.TextEdit; | |
18 | ||
f9881d70 | 19 | public 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 | |
83d73136 EK |
35 | public static void println(Object o) { |
36 | println(o.toString()); | |
37 | } | |
fc87f36a | 38 | |
83d73136 EK |
39 | public static void print(Object o) { |
40 | print(o.toString()); | |
41 | } | |
fc87f36a | 42 | |
c04308e1 | 43 | public static void println(String str) { |
baa1ab45 EK |
44 | if (!debug) |
45 | return; | |
fc87f36a | 46 | |
baa1ab45 | 47 | out.println(str); |
c04308e1 | 48 | } |
fc87f36a | 49 | |
c04308e1 EK |
50 | public static void print(String str) { |
51 | if (!debug) | |
52 | return; | |
fc87f36a | 53 | |
83d73136 | 54 | out.print(str); |
c04308e1 | 55 | } |
fc87f36a | 56 | |
35df1ff7 | 57 | public static void printChange(Change change, Set<Change> s, int i) { |
c04308e1 EK |
58 | if (!debug) |
59 | return; | |
fc87f36a | 60 | |
35df1ff7 | 61 | s.add(change); |
83d73136 | 62 | println("Level " + i++ + ": " + change.getClass()); |
35df1ff7 EK |
63 | CompositeChange c = (CompositeChange) change.getAdapter(CompositeChange.class); |
64 | if (c != null) { | |
65 | for(Change ch: c.getChildren()) { | |
66 | if (!s.contains(ch)) | |
67 | printChange(ch, s, i); | |
68 | } | |
69 | } | |
70 | } | |
fc87f36a | 71 | |
35df1ff7 | 72 | public static void printTextEdit(TextEdit edit, int i) { |
c04308e1 EK |
73 | if (!debug) |
74 | return; | |
fc87f36a | 75 | |
83d73136 | 76 | println(i++ + ": " + edit.getClass()); |
35df1ff7 EK |
77 | for (TextEdit t: edit.getChildren()) |
78 | printTextEdit(t, i); | |
fc87f36a EK |
79 | |
80 | } | |
81 | ||
82 | public static void log(Throwable t) { | |
83 | StringWriter stringWriter = new StringWriter(); | |
84 | t.printStackTrace(new PrintWriter(stringWriter)); | |
85 | log(stringWriter.toString()); | |
35df1ff7 | 86 | } |
b37b9825 EK |
87 | |
88 | public static void log(String string) { | |
89 | logger.log(Level.SEVERE, string); | |
90 | } | |
91 | ||
92 | private static void createLogger() { | |
93 | logger = Logger.getLogger(RefaktorDebug.class.getCanonicalName()); | |
94 | logger.setLevel(Level.ALL); | |
95 | ||
8e5dbebc EK |
96 | File homeDirectory = new File(System.getProperty("user.home")); |
97 | File defaultDirectory = new File(homeDirectory, "no.uio.ifi.refaktor.logs"); | |
b37b9825 | 98 | |
8e5dbebc EK |
99 | File loggingDirectory = new File(System.getProperty("refaktor.logs", defaultDirectory.getAbsolutePath())); |
100 | ||
101 | if (!loggingDirectory.exists()) | |
102 | loggingDirectory.mkdir(); | |
b37b9825 EK |
103 | |
104 | try { | |
8e5dbebc | 105 | Handler handler = new FileHandler(loggingDirectory.getAbsolutePath() + "/no.uio.ifi.refaktor.%g.log", true); |
b37b9825 EK |
106 | handler.setFormatter(new SimpleFormatter()); |
107 | logger.addHandler(handler); | |
108 | } catch (SecurityException e) { | |
109 | e.printStackTrace(); | |
110 | } catch (IOException e) { | |
111 | e.printStackTrace(); | |
112 | } | |
113 | } | |
35df1ff7 | 114 | } |