/*******************************************************************************
 * Copyright (c) 2017 Pivotal, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Pivotal, Inc. - initial API and implementation
 *******************************************************************************/
package org.springframework.ide.vscode.commons.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

public class LogRedirect {

	public static void bootRedirectToFile(String name) throws IOException {
		String logfilePath = System.getProperty("sts.log.file");
		if (StringUtil.hasText(logfilePath)) {
			PrintStream logFile = logFileStream(logfilePath);
			System.setErr(logFile);			
		}
	}

	private static PrintStream logFileStream(String logfilePath) throws FileNotFoundException {
		if (logfilePath.equals("/dev/null")) {
			System.err.println("Disabling server log output. No more output will be sent after this.");
			//redirect to a file called "/dev/null" works fine in Unix, but we also want this
			// to work on Mac and Windows. So we create our own '/dev/null' stream
			return new PrintStream(new NullOutputStream());
		} else {
			System.err.println("Redirecting log output to: "+logfilePath);
			File logfile = new File(logfilePath);
			PrintStream logFile = new PrintStream(new FileOutputStream(logfile, false));
			return logFile;
		}
	}

	public static void redirectToFile(String name) throws IOException {
		String logfilePath = System.getProperty("sts.log.file");
		if (StringUtil.hasText(logfilePath)) {
			PrintStream logFile = logFileStream(logfilePath);
			System.setErr(logFile);
		}
	}
	
}

@@ -663,36 +663,8 @@
 am;%0A
-import java.io.IOException;%0A
 impo
@@ -767,35 +767,16 @@
 ng name)
- throws IOException
  %7B%0A%09%09Str
@@ -862,32 +862,42 @@
 logfilePath)) %7B%0A
+%09%09%09try %7B%0A%09
 %09%09%09PrintStream l
@@ -928,32 +928,33 @@
 ogfilePath);%0A%09%09%09
+%09
 System.setErr(lo
@@ -964,89 +964,21 @@
 le);
-%09%09%09%0A%09%09%7D%0A%09%7D%0A%0A%09private static PrintStream logFileStream(String logfilePath) throws 
+%0A%09%09%09%7D catch (
 File
@@ -998,288 +998,330 @@
 tion
+ e)
  %7B%0A%09%09
-if (logfilePath.equals(%22/dev/null%22)) %7B%0A%09%09%09System.err.println(%22Disabling server log output. No more output will be sent after this.%22);%0A%09%09%09//redirect to a file called %22/dev/null%22 works fine in Unix, but we also want this%0A%09%09%09// to work on Mac and Windows. So we create our own '
+%09%09System.err.println(e);%0A%09%09%09%09System.err.println(%22Log file '%22 + logfilePath + %22' cannot be found. Ensure that all folders in the path exist!%22);%0A%09%09%09%09System.setErr(switchOffLogging());%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%0A%09private static PrintStream logFileStream(String logfilePath) throws FileNotFoundException %7B%0A%09%09if (logfilePath.equals(%22
 /dev
@@ -1329,16 +1329,13 @@
 null
-' stream
+%22)) %7B
 %0A%09%09%09
@@ -1345,46 +1345,25 @@
 urn 
-new PrintStream(new NullOutputStream()
+switchOffLogging(
 );%0A%09
@@ -1585,32 +1585,34 @@
 %0A%09%09%7D%0A%09%7D%0A
+%09
 %0A%09p
-ublic
+rivate
  static 
 void red
@@ -1607,249 +1607,344 @@
 tic 
-void redirectToFile(String name) throws IOException %7B%0A%09%09String logfilePath = System.getProperty(%22sts.log.file%22);%0A%09%09if (StringUtil.hasText(logfilePath)) %7B%0A%09%09%09PrintStream logFile = logFileStream(logfilePath);%0A%09%09%09System.setErr(logFile);%0A%09%09%7D
+PrintStream switchOffLogging() %7B%0A%09%09System.err.println(%22Disabling server log output. No more output will be sent after this.%22);%0A%09%09//redirect to a file called %22/dev/null%22 works fine in Unix, but we also want this%0A%09%09// to work on Mac and Windows. So we create our own '/dev/null' stream%0A%09%09return new PrintStream(new NullOutputStream());
 %0A%09%7D%0A
-%09
 %0A%7D%0A

Handle log file not found case