|
29 | 29 | import hudson.model.TaskListener; |
30 | 30 | import hudson.remoting.Channel; |
31 | 31 | import hudson.remoting.VirtualChannel; |
| 32 | +import hudson.util.StreamTaskListener; |
32 | 33 | import java.io.EOFException; |
33 | 34 | import java.io.PrintWriter; |
34 | 35 | import java.io.StringWriter; |
|
38 | 39 | import java.util.Random; |
39 | 40 | import java.util.concurrent.Callable; |
40 | 41 | import java.util.function.BiFunction; |
| 42 | +import java.util.logging.Formatter; |
| 43 | +import java.util.logging.Handler; |
41 | 44 | import java.util.logging.Level; |
| 45 | +import java.util.logging.LogRecord; |
| 46 | +import java.util.logging.Logger; |
| 47 | +import java.util.logging.SimpleFormatter; |
42 | 48 | import jenkins.security.MasterToSlaveCallable; |
43 | 49 | import org.apache.commons.io.FileUtils; |
44 | 50 | import org.apache.commons.io.output.NullOutputStream; |
@@ -157,6 +163,7 @@ protected static void close(TaskListener listener) throws Exception { |
157 | 163 | long overallPos = assertOverallLog(0, "overall from master\n<span class=\"pipeline-node-1\">step from master\n</span>", true); |
158 | 164 | long stepPos = assertStepLog("1", 0, "step from master\n", true); |
159 | 165 | VirtualChannel channel = r.createOnlineSlave().getChannel(); |
| 166 | + channel.call(new RemoteLogDumper("agent")); |
160 | 167 | channel.call(new RemotePrint("overall from agent", overall)); |
161 | 168 | channel.call(new RemotePrint("step from agent", step)); |
162 | 169 | channel.call(new GC()); |
@@ -189,6 +196,31 @@ private static final class GC extends MasterToSlaveCallable<Void, Exception> { |
189 | 196 | return null; |
190 | 197 | } |
191 | 198 | } |
| 199 | + // TODO copied from pipeline-log-cloudwatch; consider whether this should be moved into LoggerRule |
| 200 | + private static final class RemoteLogDumper extends MasterToSlaveCallable<Void, RuntimeException> { |
| 201 | + private final String name; |
| 202 | + private final TaskListener stderr = StreamTaskListener.fromStderr(); |
| 203 | + RemoteLogDumper(String name) { |
| 204 | + this.name = name; |
| 205 | + } |
| 206 | + @Override public Void call() throws RuntimeException { |
| 207 | + Handler handler = new Handler() { |
| 208 | + final Formatter formatter = new SimpleFormatter(); |
| 209 | + @Override public void publish(LogRecord record) { |
| 210 | + if (isLoggable(record)) { |
| 211 | + stderr.getLogger().print(formatter.format(record).replaceAll("(?m)^", "[" + name + "] ")); |
| 212 | + } |
| 213 | + } |
| 214 | + @Override public void flush() {} |
| 215 | + @Override public void close() throws SecurityException {} |
| 216 | + }; |
| 217 | + handler.setLevel(Level.ALL); |
| 218 | + Logger logger = Logger.getLogger(LogStorageTestBase.class.getPackage().getName()); |
| 219 | + logger.setLevel(Level.FINER); |
| 220 | + logger.addHandler(handler); |
| 221 | + return null; |
| 222 | + } |
| 223 | + } |
192 | 224 |
|
193 | 225 | /** |
194 | 226 | * Checks what happens when code using {@link TaskListener#getLogger} prints a line with inadequate synchronization. |
|
0 commit comments