}
}
}
-
+
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;
--- /dev/null
+/*
+ * 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;
+ }
+}
package org.sonar.runner.api;
import org.sonar.home.log.LogListener;
-
import org.sonar.runner.impl.Logs;
import org.sonar.runner.impl.InternalProperties;
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;
}
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;
+ }
+ }
+ }
}
}
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;
+ }
}
/**
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.Collections;
import java.util.EnumMap;
-import java.util.HashMap;
import java.util.Map;
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<Level, PrintStream> getDefaultFwdMap() {
Map<Level, PrintStream> map = new EnumMap<>(Level.class);
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<Level, PrintStream> forwardMap;
PrintStreamLogListener(Map<Level, PrintStream> forwardMap) {
- this.forwardMap = new HashMap<>(forwardMap);
+ this.forwardMap = new EnumMap<>(forwardMap);
}
@Override
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() {
Logs.setDebugEnabled(false);
Logs.setListener(null);
}
-
+
@Test
public void testNull() throws UnsupportedEncodingException {
Logs.setListener(null);