From: Duarte Meneses Date: Tue, 23 Jun 2015 13:17:34 +0000 (+0200) Subject: add LogListener in API X-Git-Tag: 2.5-rc1~76 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a0d8eb29a08a4cb13e548fa88b51dd71f27e556c;p=sonar-scanner-cli.git add LogListener in API --- diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java index 565e330..2a3e2b5 100644 --- a/sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java +++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java @@ -215,19 +215,19 @@ public class ForkedRunner extends Runner { } } } - + private static class OutConsumer implements StreamConsumer { @Override public void consumeLine(String line) { Logs.info(line); } - }; + } private static class ErrConsumer implements StreamConsumer { @Override public void consumeLine(String line) { Logs.error(line); } - }; + } static class ForkCommand { Command command; diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/LogListener.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/LogListener.java new file mode 100644 index 0000000..bfcfdc4 --- /dev/null +++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/LogListener.java @@ -0,0 +1,28 @@ +/* + * SonarQube Runner - API + * Copyright (C) 2011 SonarSource + * dev@sonar.codehaus.org + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.runner.api; + +public interface LogListener { + void log(String msg, Level level); + + enum Level { + ERROR, WARN, INFO, DEBUG, TRACE; + } +} diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/Runner.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/Runner.java index 863b08c..fed9134 100644 --- a/sonar-runner-api/src/main/java/org/sonar/runner/api/Runner.java +++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/Runner.java @@ -20,7 +20,6 @@ package org.sonar.runner.api; import org.sonar.home.log.LogListener; - import org.sonar.runner.impl.Logs; import org.sonar.runner.impl.InternalProperties; @@ -43,14 +42,14 @@ public abstract class Runner> { clone.putAll(globalProperties); return clone; } - + /** * Set a log stream. All log events will be redirected to the listener. * By default, all logs are sent to stdout, except for logs of ERROR level, which are sent to stderr. * If null is given, the default is behavior is set. */ public T setLogListener(LogListener stream) { - Logs.setListener(stream); + Logs.setListener(new LogListenerAdapter(stream)); return (T) this; } @@ -152,5 +151,32 @@ public abstract class Runner> { setGlobalProperty(key, value); } } - + + private class LogListenerAdapter implements org.sonar.home.log.LogListener { + private LogListener adaptedListener; + + LogListenerAdapter(LogListener listener) { + this.adaptedListener = listener; + } + + @Override + public void log(String msg, org.sonar.home.log.LogListener.Level level) { + this.adaptedListener.log(msg, translate(level)); + } + private LogListener.Level translate(org.sonar.home.log.LogListener.Level level) { + switch(level) { + case ERROR: + return LogListener.Level.ERROR; + case WARN: + return LogListener.Level.WARN; + case INFO: + return LogListener.Level.INFO; + case DEBUG: + default: + return LogListener.Level.DEBUG; + case TRACE: + return LogListener.Level.TRACE; + } + } + } } diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/Utils.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/Utils.java index efe39c4..1c0dc96 100644 --- a/sonar-runner-api/src/main/java/org/sonar/runner/api/Utils.java +++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/Utils.java @@ -47,7 +47,11 @@ class Utils { } int endIndex = Math.min(3, version.length()); - return Double.parseDouble(version.substring(0, endIndex)) >= 5.2; + try { + return Double.parseDouble(version.substring(0, endIndex)) >= 5.2; + } catch (NumberFormatException e) { + return false; + } } /** 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 fd60ce9..7ef9c63 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 @@ -20,7 +20,6 @@ package org.sonar.runner.impl; import org.sonar.home.log.LogListener.Level; - import org.sonar.home.log.LogListener; import javax.annotation.Nullable; @@ -28,8 +27,8 @@ import javax.annotation.Nullable; import java.io.PrintStream; import java.io.PrintWriter; import java.io.StringWriter; +import java.util.Collections; import java.util.EnumMap; -import java.util.HashMap; import java.util.Map; public class Logs { @@ -94,6 +93,9 @@ public class Logs { listener.log(msg, level); } + /** + * This is recreated every time to be sure we use the current {@link System#err} and {@link System#out}. + */ private static Map getDefaultFwdMap() { Map map = new EnumMap<>(Level.class); @@ -102,14 +104,14 @@ public class Logs { map.put(Level.INFO, System.out); map.put(Level.DEBUG, System.out); map.put(Level.TRACE, System.out); - return map; + return Collections.unmodifiableMap(map); } private static class PrintStreamLogListener implements LogListener { Map forwardMap; PrintStreamLogListener(Map forwardMap) { - this.forwardMap = new HashMap<>(forwardMap); + this.forwardMap = new EnumMap<>(forwardMap); } @Override diff --git a/sonar-runner-impl/src/test/java/org/sonar/runner/impl/LogsTest.java b/sonar-runner-impl/src/test/java/org/sonar/runner/impl/LogsTest.java index 4517c20..51b088a 100644 --- a/sonar-runner-impl/src/test/java/org/sonar/runner/impl/LogsTest.java +++ b/sonar-runner-impl/src/test/java/org/sonar/runner/impl/LogsTest.java @@ -36,8 +36,8 @@ public class LogsTest { private static final String EXPECTED_INFO = "INFO: info\n"; private static final String EXPECTED_ERROR = "ERROR: error\n"; - private ByteArrayOutputStream recordedSystemOut = new ByteArrayOutputStream(); - private ByteArrayOutputStream recordedSystemErr = new ByteArrayOutputStream(); + private ByteArrayOutputStream recordedSystemOut; + private ByteArrayOutputStream recordedSystemErr; @Before public void restoreDefault() { @@ -50,7 +50,7 @@ public class LogsTest { Logs.setDebugEnabled(false); Logs.setListener(null); } - + @Test public void testNull() throws UnsupportedEncodingException { Logs.setListener(null);