/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * 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 com.intellij.execution.process;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

/**
 * @author yole
 */
public class ProcessOutput {
  private final StringBuilder myStdoutBuilder = new StringBuilder();
  private final StringBuilder myStderrBuilder = new StringBuilder();
  private int myExitCode;
  private boolean myTimeout;
  private boolean myCancelled;

  public ProcessOutput() {
    myExitCode = -1; // until set explicitly, exit code denotes an error.
  }

  public ProcessOutput(final int exitCode) {
    myExitCode = exitCode;
  }

  public void appendStdout(@Nullable String text) {
    myStdoutBuilder.append(text);
  }

  public void appendStderr(@Nullable String text) {
    myStderrBuilder.append(text);
  }

  @NotNull
  public String getStdout() {
    return myStdoutBuilder.toString();
  }

  @NotNull
  public String getStderr() {
    return myStderrBuilder.toString();
  }

  @NotNull
  public List<String> getStdoutLines() {
    return splitLines(getStdout());
  }

  @NotNull
  public List<String> getStderrLines() {
    return splitLines(getStderr());
  }

  @NotNull
  private static List<String> splitLines(String s) {
    String converted = StringUtil.convertLineSeparators(s);
    return StringUtil.split(converted, "\n");
  }

  /**
   * If exit code is nonzero or the process timed out, logs stderr and exit code and returns false,
   * else just returns true.
   *
   * @param logger where to put error information
   * @return true iff exit code is zero
   */
  public boolean checkSuccess(@NotNull final Logger logger) {
    if (getExitCode() != 0 || isTimeout()) {
      logger.info(getStderr() + (isTimeout()? "\nTimed out" : "\nExit code " + getExitCode()));
      return false;
    }
    return true;
  }

  public void setExitCode(int exitCode) {
    myExitCode = exitCode;
  }

  public int getExitCode() {
    return myExitCode;
  }

  public void setTimeout() {
    myTimeout = true;
  }

  public boolean isTimeout() {
    return myTimeout;
  }

  public void setCancelled() {
    myCancelled = true;
  }

  public boolean isCancelled() {
    return myCancelled;
  }
}

@@ -1699,32 +1699,147 @@
 StdoutLines() %7B%0A
+    return getStdoutLines(true);%0A  %7D%0A%0A  @NotNull%0A  public List%3CString%3E getStdoutLines(boolean excludeEmptyLines) %7B%0A
     return split
@@ -1855,16 +1855,35 @@
 Stdout()
+, excludeEmptyLines
 );%0A  %7D%0A%0A
@@ -1926,32 +1926,147 @@
 StderrLines() %7B%0A
+    return getStderrLines(true);%0A  %7D%0A%0A  @NotNull%0A  public List%3CString%3E getStderrLines(boolean excludeEmptyLines) %7B%0A
     return split
@@ -2082,16 +2082,35 @@
 Stderr()
+, excludeEmptyLines
 );%0A  %7D%0A%0A
@@ -2169,16 +2169,43 @@
 String s
+, boolean excludeEmptyLines
 ) %7B%0A    
@@ -2303,16 +2303,41 @@
 ed, %22%5Cn%22
+, true, excludeEmptyLines
 );%0A  %7D%0A%0A

Handle the case with one non-default vagrant machine.