diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2015-06-16 17:38:52 +0200 |
---|---|---|
committer | Duarte Meneses <duarte.meneses@sonarsource.com> | 2015-06-19 17:08:54 +0200 |
commit | f2e74b6d6935082e4d75a5ddfaf66e8ba1079f1f (patch) | |
tree | ddecebafb089a6a2518aa96a3fcbd03b852ec13c /sonar-runner-impl/src/main/java/org/sonar/runner/impl | |
parent | e4ba9869d57dc4dd92648b5c8406d26fcd550d46 (diff) | |
download | sonar-scanner-cli-f2e74b6d6935082e4d75a5ddfaf66e8ba1079f1f.tar.gz sonar-scanner-cli-f2e74b6d6935082e4d75a5ddfaf66e8ba1079f1f.zip |
SONARUNNER-138 Allow to redirect logs to a custom stream
Diffstat (limited to 'sonar-runner-impl/src/main/java/org/sonar/runner/impl')
3 files changed, 77 insertions, 10 deletions
diff --git a/sonar-runner-impl/src/main/java/org/sonar/runner/impl/BatchLauncherMain.java b/sonar-runner-impl/src/main/java/org/sonar/runner/impl/BatchLauncherMain.java index 1e4ab2c..2921742 100644 --- a/sonar-runner-impl/src/main/java/org/sonar/runner/impl/BatchLauncherMain.java +++ b/sonar-runner-impl/src/main/java/org/sonar/runner/impl/BatchLauncherMain.java @@ -40,8 +40,12 @@ public class BatchLauncherMain { Properties props = loadProperties(args[0]); IsolatedLauncher launcher = launcherFactory.createLauncher(props); launcher.start(props, Collections.emptyList()); - launcher.execute(props); - launcher.stop(); + try { + launcher.execute(props); + } finally { + //persistit has non-daemon threads that need to be stopped or the jvm w'ont quit + launcher.stop(); + } } private static Properties loadProperties(String arg) throws IOException { diff --git a/sonar-runner-impl/src/main/java/org/sonar/runner/impl/IsolatedLauncherFactory.java b/sonar-runner-impl/src/main/java/org/sonar/runner/impl/IsolatedLauncherFactory.java index 3a9b594..5a87996 100644 --- a/sonar-runner-impl/src/main/java/org/sonar/runner/impl/IsolatedLauncherFactory.java +++ b/sonar-runner-impl/src/main/java/org/sonar/runner/impl/IsolatedLauncherFactory.java @@ -69,7 +69,7 @@ public class IsolatedLauncherFactory { } private static void addIsolatedLauncherMaskRule(Properties props) { - String unmask = "UNMASK|org.sonar.runner.batch.IsolatedLauncher"; + String unmask = "UNMASK|org.sonar.runner.batch.IsolatedLauncher,UNMASK|org.sonar.home.log.LogListener"; String currentRules = (String) props.get(InternalProperties.RUNNER_MASK_RULES); if (currentRules == null) { diff --git a/sonar-runner-impl/src/main/java/org/sonar/runner/impl/Logs.java b/sonar-runner-impl/src/main/java/org/sonar/runner/impl/Logs.java index d643162..fd60ce9 100644 --- a/sonar-runner-impl/src/main/java/org/sonar/runner/impl/Logs.java +++ b/sonar-runner-impl/src/main/java/org/sonar/runner/impl/Logs.java @@ -19,11 +19,37 @@ */ package org.sonar.runner.impl; +import org.sonar.home.log.LogListener.Level; + +import org.sonar.home.log.LogListener; + +import javax.annotation.Nullable; + +import java.io.PrintStream; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.EnumMap; +import java.util.HashMap; +import java.util.Map; + public class Logs { + private static LogListener listener = new PrintStreamLogListener(getDefaultFwdMap()); + private static boolean debugEnabled = false; + private Logs() { } - private static boolean debugEnabled = false; + public static void setListener(@Nullable LogListener listener) { + if (listener == null) { + Logs.listener = new PrintStreamLogListener(getDefaultFwdMap()); + } else { + Logs.listener = listener; + } + } + + public static LogListener getListener() { + return Logs.listener; + } public static void setDebugEnabled(boolean debugEnabled) { Logs.debugEnabled = debugEnabled; @@ -35,26 +61,63 @@ public class Logs { public static void debug(String message) { if (isDebugEnabled()) { - System.out.println("DEBUG: " + message); + log(message, Level.DEBUG); } } public static void info(String message) { - System.out.println("INFO: " + message); + log(message, Level.INFO); } public static void warn(String message) { - System.out.println("WARN: " + message); + log(message, Level.WARN); } public static void error(String message) { - System.err.println("ERROR: " + message); + log(message, Level.ERROR); } public static void error(String message, Throwable t) { - System.err.println("ERROR: " + message); + log(message, Level.ERROR); if (t != null) { - t.printStackTrace(System.err); + StringWriter sw = new StringWriter(); + + t.printStackTrace(new PrintWriter(sw)); + String[] lines = sw.toString().split(System.getProperty("line.separator")); + for (String l : lines) { + log(l, Level.ERROR); + } + } + } + + private static void log(String msg, Level level) { + listener.log(msg, level); + } + + private static Map<Level, PrintStream> getDefaultFwdMap() { + Map<Level, PrintStream> map = new EnumMap<>(Level.class); + + map.put(Level.ERROR, System.err); + map.put(Level.WARN, System.out); + map.put(Level.INFO, System.out); + map.put(Level.DEBUG, System.out); + map.put(Level.TRACE, System.out); + return map; + } + + private static class PrintStreamLogListener implements LogListener { + Map<Level, PrintStream> forwardMap; + + PrintStreamLogListener(Map<Level, PrintStream> forwardMap) { + this.forwardMap = new HashMap<>(forwardMap); + } + + @Override + public void log(String msg, Level level) { + PrintStream ps = forwardMap.get(level); + if (ps != null) { + ps.append(level.toString() + ": " + msg + System.lineSeparator()); + } } } } |