Skip to content

Commit

Permalink
OutputStreamTaskListener.Default for convenience
Browse files Browse the repository at this point in the history
  • Loading branch information
jglick committed Jul 13, 2023
1 parent 89dc150 commit 9b7f964
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@

package org.jenkinsci.plugins.workflow.log;

import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.CloseProofOutputStream;
import hudson.model.BuildListener;
import hudson.remoting.Channel;
Expand All @@ -37,38 +35,29 @@
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.util.logging.Logger;
import org.jenkinsci.remoting.SerializableOnlyOverRemoting;

/**
* Unlike {@link StreamTaskListener} this does not set {@code autoflush} on the reconstructed {@link PrintStream}.
* It also wraps on the remote side in {@link DelayBufferedOutputStream}.
*/
final class BufferedBuildListener implements BuildListener, Closeable, SerializableOnlyOverRemoting, OutputStreamTaskListener {
final class BufferedBuildListener extends OutputStreamTaskListener.Default implements BuildListener, Closeable, SerializableOnlyOverRemoting {

private static final Logger LOGGER = Logger.getLogger(BufferedBuildListener.class.getName());

private final OutputStream out;
@SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "using Replacement anyway, fields here are irrelevant")
private final PrintStream ps;

BufferedBuildListener(OutputStream out) {
this.out = out;
ps = new PrintStream(out, false, StandardCharsets.UTF_8);
}

@Override public OutputStream getOutputStream() {
return out;
}

@NonNull
@Override public PrintStream getLogger() {
return ps;
}

@Override public void close() throws IOException {
ps.close();
getLogger().close();
}

private Object writeReplace() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@
* {@link TaskListener} which can directly return an {@link OutputStream} not wrapped in a {@link PrintStream}.
* This is important for logging since the error-swallowing behavior of {@link PrintStream} is unwanted,
* and {@link PrintStream#checkError} is useless.
* <p>{@link #getLogger} should generally be implemented in terms of {@link #getOutputStream}
* (no autoflush, using {@link StandardCharsets#UTF_8})
* but there is not a {@code default} implementation since the result should be cached in a field.
*/
@Restricted(Beta.class)
public interface OutputStreamTaskListener extends TaskListener {
Expand Down Expand Up @@ -97,4 +94,20 @@ public interface OutputStreamTaskListener extends TaskListener {
return os;

Check warning on line 94 in src/main/java/org/jenkinsci/plugins/workflow/log/OutputStreamTaskListener.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 94 is not covered by tests
}

/**
* Convenience implementation handling {@link #getLogger}.
*/
abstract class Default implements OutputStreamTaskListener {

private transient PrintStream ps;

@Override public synchronized PrintStream getLogger() {
if (ps == null) {
ps = new PrintStream(getOutputStream(), false, StandardCharsets.UTF_8);
}
return ps;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ private static OutputStream decorateAll(OutputStream base, List<TaskListenerDeco
return base;
}

private static final class DecoratedTaskListener implements BuildListener, OutputStreamTaskListener {
private static final class DecoratedTaskListener extends OutputStreamTaskListener.Default implements BuildListener {

private static final long serialVersionUID = 1;

Expand All @@ -253,7 +253,6 @@ private static final class DecoratedTaskListener implements BuildListener, Outpu
private final @NonNull List<TaskListenerDecorator> decorators;

private transient OutputStream out;
private transient PrintStream logger;

DecoratedTaskListener(@NonNull TaskListener delegate, @NonNull List<TaskListenerDecorator> decorators) {
this.delegate = delegate;
Expand All @@ -270,14 +269,6 @@ private static final class DecoratedTaskListener implements BuildListener, Outpu
return out;
}

@NonNull
@Override public PrintStream getLogger() {
if (logger == null) {
logger = new PrintStream(getOutputStream(), false, StandardCharsets.UTF_8);
}
return logger;
}

@Override public String toString() {
return "DecoratedTaskListener[" + delegate + decorators + "]";
}
Expand Down

0 comments on commit 9b7f964

Please sign in to comment.