|
| 1 | +package com.annimon.ownlang; |
| 2 | + |
| 3 | +import com.annimon.ownlang.lib.CallStack; |
| 4 | + |
| 5 | +import java.io.ByteArrayOutputStream; |
| 6 | +import java.io.PrintStream; |
| 7 | +import java.io.UnsupportedEncodingException; |
| 8 | + |
| 9 | +public class Console { |
| 10 | + |
| 11 | + public static void print(String value) { |
| 12 | + System.out.print(value); |
| 13 | + } |
| 14 | + |
| 15 | + public static void print(Object value) { |
| 16 | + print(value.toString()); |
| 17 | + } |
| 18 | + |
| 19 | + public static void println() { |
| 20 | + System.out.println(); |
| 21 | + } |
| 22 | + |
| 23 | + public static void println(String value) { |
| 24 | + System.out.println(value); |
| 25 | + } |
| 26 | + |
| 27 | + public static void println(Object value) { |
| 28 | + println(value.toString()); |
| 29 | + } |
| 30 | + |
| 31 | + public static void error(Throwable throwable) { |
| 32 | + error(throwable.toString()); |
| 33 | + } |
| 34 | + |
| 35 | + public static void error(CharSequence value) { |
| 36 | + System.err.println(value); |
| 37 | + } |
| 38 | + |
| 39 | + public static void handleException(Thread thread, Throwable throwable) { |
| 40 | + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 41 | + try(final PrintStream ps = new PrintStream(baos)) { |
| 42 | + ps.printf("%s in %s\n", throwable.getMessage(), thread.getName()); |
| 43 | + for (CallStack.CallInfo call : CallStack.getCalls()) { |
| 44 | + ps.printf("\tat %s\n", call); |
| 45 | + } |
| 46 | + ps.println(); |
| 47 | + throwable.printStackTrace(ps); |
| 48 | + ps.flush(); |
| 49 | + } |
| 50 | + try { |
| 51 | + error(baos.toString("UTF-8")); |
| 52 | + } catch (UnsupportedEncodingException ex) { |
| 53 | + error(baos.toString()); |
| 54 | + } |
| 55 | + } |
| 56 | +} |
0 commit comments