package org.cobbzilla.util.system;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import static org.cobbzilla.util.daemon.ZillaRuntime.shortError;
import static org.cobbzilla.util.daemon.ZillaRuntime.stacktrace;

@AllArgsConstructor @Slf4j
public class OutOfMemoryErrorUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {

    public static final OutOfMemoryErrorUncaughtExceptionHandler EXIT_ON_OOME = new OutOfMemoryErrorUncaughtExceptionHandler();

    private final int status;

    public OutOfMemoryErrorUncaughtExceptionHandler() { status = 2; }

    @Override public void uncaughtException(Thread t, Throwable e) {
        if (e instanceof OutOfMemoryError) {
            try {
                log.error("!!!!! OutOfMemoryError: calling System.exit("+status+") from "+stacktrace(t), e);
            } catch (Throwable ignored) {}
            System.exit(status);
        } else {
            log.error("!!!!! Uncaught Exception: " + shortError(e));
        }
    }

}

@@ -120,53 +120,67 @@
 org.
-cobbzilla.util.daemon.ZillaRuntime.shortError
+apache.commons.lang3.exception.ExceptionUtils.getStackTrace
 ;%0Aim
@@ -231,25 +231,25 @@
 untime.s
-tacktrace
+hortError
 ;%0A%0A@AllA
@@ -824,27 +824,29 @@
 from
- %22+s
+:%5Cn%22+getS
 tack
-t
+T
 race(
-t), 
 e
+)
 );%0A 

print stack trace