/*
 * Copyright 2011 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gradle.launcher.daemon.server.exec;

import org.gradle.api.logging.LogLevel;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.gradle.launcher.daemon.diagnostics.DaemonDiagnostics;
import org.gradle.launcher.daemon.logging.DaemonMessages;
import org.gradle.launcher.daemon.protocol.Build;
import org.gradle.launcher.daemon.server.api.DaemonCommandExecution;
import org.gradle.launcher.daemon.server.api.DaemonConnection;
import org.gradle.logging.internal.LoggingOutputInternal;
import org.gradle.logging.internal.OutputEvent;
import org.gradle.logging.internal.OutputEventListener;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;

public class LogToClient extends BuildCommandOnly {

    public static final String DISABLE_OUTPUT = "org.gradle.daemon.disable-output";
    private static final Logger LOGGER = Logging.getLogger(LogToClient.class);

    private final LoggingOutputInternal loggingOutput;
    private final DaemonDiagnostics diagnostics;

    public LogToClient(LoggingOutputInternal loggingOutput, DaemonDiagnostics diagnostics) {
        this.loggingOutput = loggingOutput;
        this.diagnostics = diagnostics;
    }

    protected void doBuild(final DaemonCommandExecution execution, Build build) {
        if (Boolean.getBoolean(DISABLE_OUTPUT)) {
            execution.proceed();
            return;
        }

        final LogLevel buildLogLevel = build.getParameters().getLogLevel();
        final AsynchronousLogDispatcher dispatcher = new AsynchronousLogDispatcher(execution.getConnection());
        OutputEventListener listener = new OutputEventListener() {
            public void onOutput(OutputEvent event) {
                if (event.getLogLevel() != null && event.getLogLevel().compareTo(buildLogLevel) >= 0) {
                    dispatcher.submit(event);
                }
            }
        };

        LOGGER.debug(DaemonMessages.ABOUT_TO_START_RELAYING_LOGS);
        loggingOutput.addOutputEventListener(listener);
        try {
            LOGGER.info("{}{}). The daemon log file: {}", DaemonMessages.STARTED_RELAYING_LOGS, diagnostics.getPid(), diagnostics.getDaemonLog());
            dispatcher.start();
            execution.proceed();
        } finally {
            dispatcher.waitForCompletion();
            loggingOutput.removeOutputEventListener(listener);
        }
    }

    private class AsynchronousLogDispatcher extends Thread {
        private final BlockingQueue<OutputEvent> eventQueue = new LinkedBlockingDeque<OutputEvent>();
        private final DaemonConnection connection;
        private boolean shouldStop;

        private AsynchronousLogDispatcher(DaemonConnection conn) {
            this.connection = conn;
        }

        public void submit(OutputEvent event) {
            eventQueue.add(event);
        }

        @Override
        public void run() {
            while (!shouldStop) {
                try {
                    OutputEvent event = eventQueue.take();
                    dispatchAsync(event);
                } catch (InterruptedException ex) {
                    shouldStop = true;
                }
            }
        }

        private void sendRemainingEvents() {
            while (!eventQueue.isEmpty()) {
                OutputEvent event = eventQueue.remove();
                dispatchAsync(event);
            }
        }

        private void dispatchAsync(OutputEvent event) {
            try {
                connection.logEvent(event);
            } catch (Exception ex) {
                shouldStop = true;
                //Ignore. It means the client has disconnected so no point sending him any log output.
                //we should be checking if client still listens elsewhere anyway.
            }
        }

        public void waitForCompletion() {
            interrupt();
            sendRemainingEvents();
        }
    }
}

@@ -1021,71 +1021,8 @@
 on;%0A
-import org.gradle.launcher.daemon.server.api.DaemonConnection;%0A
 impo
@@ -1075,16 +1075,16 @@
 ternal;%0A
+
 import o
@@ -1184,101 +1184,8 @@
 r;%0A%0A
-import java.util.concurrent.BlockingQueue;%0Aimport java.util.concurrent.LinkedBlockingDeque;%0A%0A
 publ
@@ -1962,119 +1962,8 @@
 ();%0A
-        final AsynchronousLogDispatcher dispatcher = new AsynchronousLogDispatcher(execution.getConnection());%0A
     
@@ -2071,32 +2071,58 @@
 tEvent event) %7B%0A
+                try %7B%0A    
                 
@@ -2233,33 +2233,309 @@
     
-dispatcher.submit(event);
+    execution.getConnection().logEvent(event);%0A                    %7D%0A                %7D catch (Exception e) %7B%0A                    //Ignore. It means the client has disconnected so no point sending him any log output.%0A                    //we should be checking if client still listens elsewhere anyway.
 %0A   
@@ -2863,40 +2863,8 @@
 ));%0A
-            dispatcher.start();%0A
     
@@ -2916,52 +2916,8 @@
 y %7B%0A
-            dispatcher.waitForCompletion();%0A
     
@@ -2994,1531 +2994,8 @@
    %7D
-%0A%0A    private class AsynchronousLogDispatcher extends Thread %7B%0A        private final BlockingQueue%3COutputEvent%3E eventQueue = new LinkedBlockingDeque%3COutputEvent%3E();%0A        private final DaemonConnection connection;%0A        private boolean shouldStop;%0A%0A        private AsynchronousLogDispatcher(DaemonConnection conn) %7B%0A            this.connection = conn;%0A        %7D%0A%0A        public void submit(OutputEvent event) %7B%0A            eventQueue.add(event);%0A        %7D%0A%0A        @Override%0A        public void run() %7B%0A            while (!shouldStop) %7B%0A                try %7B%0A                    OutputEvent event = eventQueue.take();%0A                    dispatchAsync(event);%0A                %7D catch (InterruptedException ex) %7B%0A                    shouldStop = true;%0A                %7D%0A            %7D%0A        %7D%0A%0A        private void sendRemainingEvents() %7B%0A            while (!eventQueue.isEmpty()) %7B%0A                OutputEvent event = eventQueue.remove();%0A                dispatchAsync(event);%0A            %7D%0A        %7D%0A%0A        private void dispatchAsync(OutputEvent event) %7B%0A            try %7B%0A                connection.logEvent(event);%0A            %7D catch (Exception ex) %7B%0A                shouldStop = true;%0A                //Ignore. It means the client has disconnected so no point sending him any log output.%0A                //we should be checking if client still listens elsewhere anyway.%0A            %7D%0A        %7D%0A%0A        public void waitForCompletion() %7B%0A            interrupt();%0A            sendRemainingEvents();%0A        %7D%0A    %7D
 %0A%7D%0A
+%0A

Revert changes to perform asynchronous dispatch as is seem to break on CI