Skip to content
This repository has been archived by the owner on Dec 15, 2021. It is now read-only.

Commit

Permalink
Merge pull request #107 from jglick/ConsoleLogFilter-JENKINS-27392
Browse files Browse the repository at this point in the history
[JENKINS-27392] ConsoleLogFilter
  • Loading branch information
jglick committed Mar 31, 2015
2 parents d60edde + 6344f32 commit 16eb014
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
Expand Up @@ -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}.
Expand All @@ -46,6 +52,7 @@ public abstract class BodyInvoker {
* <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)
*
Expand Down Expand Up @@ -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));
}
}

}
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -61,6 +63,7 @@ public abstract class DefaultStepContext extends StepContext {
* 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) {
Expand Down Expand Up @@ -88,8 +91,12 @@ public abstract class DefaultStepContext extends StepContext {
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 {
Expand Down

0 comments on commit 16eb014

Please sign in to comment.