/*******************************************************************************
 * 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.springsource.ide.eclipse.commons.livexp.util;

import java.util.concurrent.CompletableFuture;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.springsource.ide.eclipse.commons.livexp.Activator;

public class Log {

	public static void log(Throwable e) {
		if (ExceptionUtil.isCancelation(e)) {
			//Don't log canceled operations, those aren't real errors.
			return;
		}
		try {
			Activator.getDefault().getLog().log(ExceptionUtil.status(e));
		} catch (NullPointerException npe) {
			//Can happen if errors are trying to be logged during Eclipse's shutdown
			e.printStackTrace();
		}
	}

	public static void info(String msg) {
		Activator.getDefault().getLog().log(ExceptionUtil.status(IStatus.INFO, msg));
	}

	public static void warn(String msg) {
		Activator.getDefault().getLog().log(ExceptionUtil.status(IStatus.WARNING, msg));
	}

	public static void warn(Throwable e) {
		if (ExceptionUtil.isCancelation(e)) {
			//Don't log canceled operations, those aren't real errors.
			return;
		}
		try {
			Activator.getDefault().getLog().log(ExceptionUtil.status(IStatus.WARNING, e));
		} catch (NullPointerException npe) {
			//Can happen if errors are trying to be logged during Eclipse's shutdown
			e.printStackTrace();
		}
	}

	public static void error(String string) {
		try {
			throw ExceptionUtil.coreException(string);
		} catch (CoreException e) {
			Log.log(e);
		}
	}

	public static void async(CompletableFuture<?> asyncWork) {
		asyncWork.exceptionally(e -> {
			Log.log(e);
			return null;
		});
	}

}

@@ -618,16 +618,52 @@
 eFuture;
+%0Aimport java.util.function.Consumer;
 %0A%0Aimport
@@ -827,16 +827,206 @@
  Log %7B%0A%0A
+%09/**%0A%09 * This is for testing and allows test code to 'observe' whether any errors are%0A%09 * being logged during test execution. %0A%09 */%0A%09public static Consumer%3CThrowable%3E errorHandler = null;%0A%09%0A
 %09public 
@@ -1169,32 +1169,32 @@
 return;%0A%09%09%7D%0A
-
 %09%09try %7B%0A
 %09%09%09Activator
@@ -1173,32 +1173,116 @@
 rn;%0A%09%09%7D%0A%09%09try %7B%0A
+%09%09%09Consumer%3CThrowable%3E eh = errorHandler;%0A%09%09%09if (eh!=null) %7B%0A%09%09%09%09eh.accept(e);%0A%09%09%09%7D%0A
 %09%09%09Activator.get

Allow test code to intercept logged exceptions