Skip to content
Permalink
Browse files

Merge pull request #107 from jglick/ConsoleLogFilter-JENKINS-27392

[JENKINS-27392] ConsoleLogFilter
  • Loading branch information
jglick committed Mar 31, 2015
2 parents d60edde + 6344f32 commit 16eb0143aa3f04e5e89eab55ca60101b98b9b449
@@ -26,10 +26,16 @@
import com.google.common.util.concurrent.FutureCallback;
import hudson.EnvVars;
import hudson.console.ConsoleLogFilter;
import hudson.model.AbstractBuild;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;

import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.Collection;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

/**
* Builder pattern for controlling how to execute a body block of a {@link Step}.
@@ -46,6 +52,7 @@
* <dl>
* <dt>{@link EnvVars}<dd>use {@link EnvironmentExpander} instead
* <dt>{@link EnvironmentExpander}<dd>use {@link EnvironmentExpander#merge}
* <dt>{@link ConsoleLogFilter}<dd>use {@link #mergeConsoleLogFilters}
* </dl>
* @see StepContext#get(Class)
*
@@ -97,4 +104,31 @@ public final BodyInvoker withCallback(FutureCallback<Object> callback) {
* configured on this object via other methods.
*/
public abstract BodyExecution start();

/**
* Merge two console log filters so that both are applied.
* @param original the original filter in {@link StepContext#get}, if any
* @param subsequent your implementation; should expect {@code null} for the {@code build} parameter, and be {@link Serializable}
* @return a merge of the two, or just yours if there was no original
* @see #withContext
*/
public static ConsoleLogFilter mergeConsoleLogFilters(@CheckForNull ConsoleLogFilter original, @Nonnull ConsoleLogFilter subsequent) {
if (original == null) {
return subsequent;
}
return new MergedFilter(original, subsequent);
}
private static final class MergedFilter extends ConsoleLogFilter implements Serializable {
private static final long serialVersionUID = 1;
private final ConsoleLogFilter original, subsequent;
MergedFilter(ConsoleLogFilter original, ConsoleLogFilter subsequent) {
this.original = original;
this.subsequent = subsequent;
}
@SuppressWarnings("rawtypes") // not my fault
@Override public OutputStream decorateLogger(AbstractBuild _ignore, OutputStream logger) throws IOException, InterruptedException {
return subsequent.decorateLogger(_ignore, original.decorateLogger(_ignore, logger));
}
}

}
@@ -26,6 +26,7 @@

import hudson.EnvVars;
import hudson.Launcher;
import hudson.console.ConsoleLogFilter;
import hudson.model.Computer;
import hudson.model.Job;
import hudson.model.Node;
@@ -34,6 +35,7 @@
import hudson.util.StreamTaskListener;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -61,6 +63,7 @@
* Uses {@link #doGet} but automatically translates certain kinds of objects into others.
* <p>{@inheritDoc}
*/
@edu.umd.cs.findbugs.annotations.SuppressWarnings("OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE") // stream closed later
@Override public final <T> T get(Class<T> key) throws IOException, InterruptedException {
T value = doGet(key);
if (key == EnvVars.class) {
@@ -88,8 +91,12 @@
la = new LogActionImpl(getNode(), Charset.defaultCharset());
getNode().addAction(la);
}

listener = new StreamTaskListener(new FileOutputStream(la.getLogFile(), true));
ConsoleLogFilter filter = get(ConsoleLogFilter.class);
OutputStream os = new FileOutputStream(la.getLogFile(), true);
if (filter != null) {
os = filter.decorateLogger(null, os);
}
listener = new StreamTaskListener(os);
getExecution().addListener(new GraphListener() {
@Override public void onNewHead(FlowNode node) {
try {

0 comments on commit 16eb014

Please sign in to comment.
You can’t perform that action at this time.