// Path: src/main/java/com/sumologic/jenkins/jenkinssumologicplugin/sender/LogListener.java
// @Extension(ordinal = -1)
// public class LogListener extends ConsoleLogFilter implements Serializable {
// 
//     private static final long serialVersionUID = -131194971357180671L;
// 
//     private transient Run run;
// 
// 
//     public LogListener() {
//         super();
//     }
// 
//     public LogListener(Run build) {
//         this();
//         this.run = build;
//         //build.addAction(new SearchAction(build));
//     }
// 
//     @Override
//     public OutputStream decorateLogger(Run build, OutputStream outputStream) {
//         /*try {
//             PluginDescriptorImpl pluginDescriptor = PluginDescriptorImpl.getInstance();
//             if (!pluginDescriptor.isJobConsoleLogEnabled()) {
//                 if (build != null) {
//                     build.addAction(new SearchAction(build));
//                     return new SumologicOutputStream(outputStream, build, pluginDescriptor, streamState);
//                 } else {
//                     return new SumologicOutputStream(outputStream, run, pluginDescriptor, streamState);
//                 }
//             }
//         } catch (Exception e) {
//             String errorMessage = CONSOLE_ERROR + Arrays.toString(e.getStackTrace());
//             LOG.log(Level.WARNING, errorMessage);
//             outputStream.write(errorMessage.getBytes());
//         }*/
//         return outputStream;
//     }
// 
//     @Override
//     public OutputStream decorateLogger(@Nonnull Computer computer, OutputStream logger) {
//         return logger;
//     }
// }


import com.sumologic.jenkins.jenkinssumologicplugin.sender.LogListener;
import hudson.Extension;
import hudson.console.ConsoleLogFilter;
import hudson.model.Run;
import org.jenkinsci.plugins.workflow.steps.*;
import org.kohsuke.stapler.DataBoundConstructor;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
package com.sumologic.jenkins.jenkinssumologicplugin.utility;



/**
 * Custom plugin extension step that can be used without a node and build wrapper.
 * <p>
 * Created by - Sourabh Jain 5/2019
 */
public class SumoPipelineLogCollection extends Step {

    @DataBoundConstructor
    public SumoPipelineLogCollection() {
    }

    @Override
    public StepExecution start(StepContext context) {
        return new Execution(context);
    }

    /**
     * Execution for {@link SumoPipelineLogCollection}.
     */
    public static class Execution extends StepExecution {

        private static final long serialVersionUID = 731578971545010547L;

        protected Execution(StepContext context) {
            super(context);
        }

        @Override
        public void onResume() {
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean start() throws Exception {
            StepContext context = getContext();
            context.newBodyInvoker().
                    withContext(createConsoleLogFilter(context)).
                    withCallback(BodyExecutionCallback.wrap(context)).
                    start();
            return false;
        }

        private ConsoleLogFilter createConsoleLogFilter(StepContext context)
                throws IOException, InterruptedException {
            ConsoleLogFilter original = context.get(ConsoleLogFilter.class);
            Run build = context.get(Run.class);
// Path: src/main/java/com/sumologic/jenkins/jenkinssumologicplugin/sender/LogListener.java
// @Extension(ordinal = -1)
// public class LogListener extends ConsoleLogFilter implements Serializable {
// 
//     private static final long serialVersionUID = -131194971357180671L;
// 
//     private transient Run run;
// 
// 
//     public LogListener() {
//         super();
//     }
// 
//     public LogListener(Run build) {
//         this();
//         this.run = build;
//         //build.addAction(new SearchAction(build));
//     }
// 
//     @Override
//     public OutputStream decorateLogger(Run build, OutputStream outputStream) {
//         /*try {
//             PluginDescriptorImpl pluginDescriptor = PluginDescriptorImpl.getInstance();
//             if (!pluginDescriptor.isJobConsoleLogEnabled()) {
//                 if (build != null) {
//                     build.addAction(new SearchAction(build));
//                     return new SumologicOutputStream(outputStream, build, pluginDescriptor, streamState);
//                 } else {
//                     return new SumologicOutputStream(outputStream, run, pluginDescriptor, streamState);
//                 }
//             }
//         } catch (Exception e) {
//             String errorMessage = CONSOLE_ERROR + Arrays.toString(e.getStackTrace());
//             LOG.log(Level.WARNING, errorMessage);
//             outputStream.write(errorMessage.getBytes());
//         }*/
//         return outputStream;
//     }
// 
//     @Override
//     public OutputStream decorateLogger(@Nonnull Computer computer, OutputStream logger) {
//         return logger;
//     }
// }


// Path: src/main/java/com/sumologic/jenkins/jenkinssumologicplugin/utility/SumoPipelineLogCollection.java
import com.sumologic.jenkins.jenkinssumologicplugin.sender.LogListener;
import hudson.Extension;
import hudson.console.ConsoleLogFilter;
import hudson.model.Run;
import org.jenkinsci.plugins.workflow.steps.*;
import org.kohsuke.stapler.DataBoundConstructor;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

package com.sumologic.jenkins.jenkinssumologicplugin.utility;



/**
 * Custom plugin extension step that can be used without a node and build wrapper.
 * <p>
 * Created by - Sourabh Jain 5/2019
 */
public class SumoPipelineLogCollection extends Step {

    @DataBoundConstructor
    public SumoPipelineLogCollection() {
    }

    @Override
    public StepExecution start(StepContext context) {
        return new Execution(context);
    }

    /**
     * Execution for {@link SumoPipelineLogCollection}.
     */
    public static class Execution extends StepExecution {

        private static final long serialVersionUID = 731578971545010547L;

        protected Execution(StepContext context) {
            super(context);
        }

        @Override
        public void onResume() {
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean start() throws Exception {
            StepContext context = getContext();
            context.newBodyInvoker().
                    withContext(createConsoleLogFilter(context)).
                    withCallback(BodyExecutionCallback.wrap(context)).
                    start();
            return false;
        }

        private ConsoleLogFilter createConsoleLogFilter(StepContext context)
                throws IOException, InterruptedException {
            ConsoleLogFilter original = context.get(ConsoleLogFilter.class);
            Run build = context.get(Run.class);
            ConsoleLogFilter subsequent = new LogListener(build);