Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>3.25</version>
<version>3.28</version>
<relativePath />
</parent>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,4 +322,9 @@ private void maybeFlush() {
}
}

@Deprecated
@Override public File getLogFile(FlowExecutionOwner.Executable build, boolean complete) {
return log;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,19 @@

package org.jenkinsci.plugins.workflow.log;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.ExtensionList;
import hudson.console.AnnotatedLargeText;
import hudson.console.ConsoleAnnotationOutputStream;
import hudson.model.BuildListener;
import hudson.model.Run;
import hudson.model.TaskListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nonnull;
import org.jenkinsci.plugins.workflow.actions.LogAction;
import org.jenkinsci.plugins.workflow.flow.FlowExecutionOwner;
Expand Down Expand Up @@ -108,6 +114,44 @@ public interface LogStorage {
*/
@Nonnull AnnotatedLargeText<FlowNode> stepLog(@Nonnull FlowNode node, boolean complete);

/**
* Provide a file containing the log text.
* The default implementation creates a temporary file based on the current contents of {@link #overallLog}.
* @param build as in {@link #overallLog}
* @param complete as in {@link #overallLog}
* @return a possibly temporary file
* @deprecated Only used for compatibility with {@link Run#getLogFile}.
*/
@SuppressFBWarnings(value = "REC_CATCH_EXCEPTION", justification = "silly rule")
@Deprecated
default @Nonnull File getLogFile(@Nonnull FlowExecutionOwner.Executable build, boolean complete) {
try {
AnnotatedLargeText<FlowExecutionOwner.Executable> logText = overallLog(build, complete);
FlowExecutionOwner owner = build.asFlowExecutionOwner();
File f = File.createTempFile("deprecated", ".log", owner != null ? owner.getRootDir() : null);
f.deleteOnExit();
try (OutputStream os = new FileOutputStream(f)) {
// Similar to Run#writeWholeLogTo but terminates even if !complete:
long pos = 0;
while (true) {
long pos2 = logText.writeRawLogTo(pos, os);
if (pos2 <= pos) {
break;
}
pos = pos2;
}
}
return f;
} catch (Exception x) {
Logger.getLogger(LogStorage.class.getName()).log(Level.WARNING, null, x);
if (build instanceof Run) {
return new File(((Run) build).getRootDir(), "log");
} else {
return new File("broken.log"); // not much we can do
}
}
}

/**
* Gets the available log storage method for a given build.
* @param b a build about to start
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@
import java.util.concurrent.Callable;
import java.util.function.BiFunction;
import jenkins.security.MasterToSlaveCallable;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.output.NullOutputStream;
import org.apache.commons.io.output.NullWriter;
import org.apache.commons.io.output.WriterOutputStream;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.graph.FlowNode;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.ClassRule;
Expand Down Expand Up @@ -221,6 +225,26 @@ private static final class RemotePrint extends MasterToSlaveCallable<Void, Excep
text("2").writeRawLogTo(0, new NullOutputStream());
}

@SuppressWarnings("deprecation")
@Test public void getLogFile() throws Exception {
LogStorage ls = createStorage();
TaskListener overall = ls.overallListener();
overall.getLogger().println("starting");
TaskListener step1 = ls.nodeListener(new MockNode("1"));
step1.getLogger().println("from step");
step1.getLogger().flush();
overall.getLogger().println("finishing");
overall.getLogger().flush();
WorkflowJob fakeProject = r.createProject(WorkflowJob.class, "fake");
fakeProject.setDefinition(new CpsFlowDefinition("", true));
WorkflowRun fakeBuild = r.buildAndAssertSuccess(fakeProject);
assertOverallLog(0, FileUtils.readFileToString(ls.getLogFile(fakeBuild, false)), false);
close(overall);
ls = createStorage();
assertOverallLog(0, FileUtils.readFileToString(ls.getLogFile(fakeBuild, true)), false);
close(overall);
}

// TODO test missing final newline

protected final long assertOverallLog(long start, String expected, boolean html) throws Exception {
Expand Down