package no.uio.ifi.refaktor.utils; import java.io.File; import java.io.IOException; import java.io.PrintStream; import java.util.Arrays; import java.util.HashSet; import java.util.Set; import java.util.logging.FileHandler; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.Logger; import java.util.logging.SimpleFormatter; import org.eclipse.core.runtime.Platform; import org.eclipse.ltk.core.refactoring.Change; import org.eclipse.ltk.core.refactoring.CompositeChange; import org.eclipse.text.edits.TextEdit; public class RefaktorDebug { static { createLogger(); } private static final String DEBUG_ARG = "-refaktordebug"; private static boolean debug = false; private static PrintStream out; private static Logger logger; public static void initialize() { out = System.err; Set args = new HashSet(Arrays.asList(Platform.getApplicationArgs())); debug = args.contains(DEBUG_ARG); println("(Refaktor Debug active)"); } public static void println(Object o) { println(o.toString()); } public static void print(Object o) { print(o.toString()); } public static void println(String str) { if (!debug) return; out.println(str); } public static void print(String str) { if (!debug) return; out.print(str); } public static void printChange(Change change, Set s, int i) { if (!debug) return; s.add(change); println("Level " + i++ + ": " + change.getClass()); CompositeChange c = (CompositeChange) change.getAdapter(CompositeChange.class); if (c != null) { for(Change ch: c.getChildren()) { if (!s.contains(ch)) printChange(ch, s, i); } } } public static void printTextEdit(TextEdit edit, int i) { if (!debug) return; println(i++ + ": " + edit.getClass()); for (TextEdit t: edit.getChildren()) printTextEdit(t, i); } public static void log(String string) { logger.log(Level.SEVERE, string); } private static void createLogger() { logger = Logger.getLogger(RefaktorDebug.class.getCanonicalName()); logger.setLevel(Level.ALL); File homeDir = new File(System.getProperty("user.home")); File loggingDir = new File(homeDir, "no.uio.ifi.refaktor.logs"); if (!loggingDir.exists()) loggingDir.mkdir(); try { Handler handler = new FileHandler(loggingDir.getAbsolutePath() + "/no.uio.ifi.refaktor.%g.log", true); handler.setFormatter(new SimpleFormatter()); logger.addHandler(handler); } catch (SecurityException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }