package org.minimalj.util;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ExceptionUtils {

	private static final Logger EXCEPTION_LOGGER = Logger.getLogger("Exception");
	
	/**
	 * Logs only the relevant part of the stack trace. For example
	 * if a getter fails it's irrelevant if the getter is called
	 * by a swing class or something else
	 * 
	 * @param logger the logger where the ouput should go
	 * @param exception the exception to be printed
	 */
	public static void logReducedStackTrace(Logger logger, Exception exception) {
		Exception here = new Exception();
		String[] hereStrings = getStackFrames(here);
		String[] throwableStrings = getStackFrames(exception);
		
		int linesToSkip = 1;
		while (throwableStrings.length - linesToSkip > 0 && hereStrings.length - linesToSkip > 0) {
			if (!StringUtils.equals(hereStrings[hereStrings.length-linesToSkip], throwableStrings[throwableStrings.length-linesToSkip])) {
				break;
			}
			linesToSkip++;
		}
		for (int i = 0; i<=throwableStrings.length-linesToSkip; i++) {
			EXCEPTION_LOGGER.log(Level.SEVERE, throwableStrings[i]);
		}
	}

	// from apache ExceptionUtil
	private static String getStackTrace(final Throwable throwable) {
		final StringWriter sw = new StringWriter();
		final PrintWriter pw = new PrintWriter(sw, true);
		throwable.printStackTrace(pw);
		return sw.getBuffer().toString();
	}
	
	private static String[] getStackFrames(final Throwable throwable) {
		if (throwable == null) {
			return new String[0];
		}
		return getStackFrames(getStackTrace(throwable));
	}

	private static String[] getStackFrames(final String stackTrace) {
		final String linebreak = System.lineSeparator();
		final StringTokenizer frames = new StringTokenizer(stackTrace, linebreak);
		final List<String> list = new ArrayList<String>();
		while (frames.hasMoreTokens()) {
			list.add(frames.nextToken());
		}
		return list.toArray(new String[list.size()]);
	}
	
}

@@ -264,89 +264,8 @@
  %7B%0A%0A
-%09private static final Logger EXCEPTION_LOGGER = Logger.getLogger(%22Exception%22);%0A%09%0A
 %09/**
@@ -476,16 +476,17 @@
 e the ou
+t
 put shou
@@ -1124,24 +1124,14 @@
 %0A%09%09%09
-EXCEPTION_LOGGER
+logger
 .log

use provided logger