diff --git a/rxlib/src/main/java/org/rx/core/Reflects.java b/rxlib/src/main/java/org/rx/core/Reflects.java index 20061e19..3f16bfeb 100644 --- a/rxlib/src/main/java/org/rx/core/Reflects.java +++ b/rxlib/src/main/java/org/rx/core/Reflects.java @@ -111,6 +111,16 @@ public static Linq stackTrace(int takeCount) { return Linq.from(new Throwable().getStackTrace()).skip(2).take(takeCount); } + public static Class getCallerClass() { + try { + Class type = Class.forName("sun.reflect.Reflection"); + return invokeMethod(type, "getCallerClass"); + } catch (ClassNotFoundException e) { +// throw new RuntimeException(e); + return stackClass(0); + } + } + public static Class stackClass(int depth) { //Throwable.class.getDeclaredMethod("getStackTraceElement", int.class) & Reflection.getCallerClass(2 + depth) java 11 not exist return SecurityManagerEx.INSTANCE.stackClass(2 + depth); diff --git a/rxlib/src/main/java/org/rx/core/ThreadPool.java b/rxlib/src/main/java/org/rx/core/ThreadPool.java index c9ee5261..e17a81ec 100644 --- a/rxlib/src/main/java/org/rx/core/ThreadPool.java +++ b/rxlib/src/main/java/org/rx/core/ThreadPool.java @@ -26,6 +26,7 @@ import java.util.function.Supplier; import static org.rx.core.Constants.NON_UNCHECKED; +import static org.rx.core.Extends.ifNull; import static org.rx.core.Extends.require; @SuppressWarnings(NON_UNCHECKED) @@ -166,14 +167,22 @@ static Task as(Object fn) { final Object id; final InternalThreadLocalMap parent; final String traceId; + final Class caller; private Task(Func fn, FlagsEnum flags, Object id) { if (flags == null) { flags = RunFlag.NONE.flags(); } - if (RxConfig.INSTANCE.threadPool.traceName != null) { + RxConfig conf = RxConfig.INSTANCE; + if (conf.threadPool.traceName != null) { flags.add(RunFlag.THREAD_TRACE); } + if (conf.trace.slowMethodElapsedMicros > 0) { + //Reflects.getStackTrace(t) + caller = Reflects.getCallerClass(); + } else { + caller = null; + } this.fn = fn; this.flags = flags; @@ -185,30 +194,29 @@ private Task(Func fn, FlagsEnum flags, Object id) { @SneakyThrows @Override public T call() { - long eMicrosThreshold = RxConfig.INSTANCE.trace.slowMethodElapsedMicros; - if (eMicrosThreshold <= 0) { + if (RxConfig.INSTANCE.trace.slowMethodElapsedMicros > 0) { + T r = null; + Throwable ex = null; + long s = System.nanoTime(); try { - return fn.invoke(); + r = fn.invoke(); } catch (Throwable e) { - TraceHandler.INSTANCE.log(toString(), e); + TraceHandler.INSTANCE.log(toString(), ex = e); throw e; + } finally { + Thread t = Thread.currentThread(); + TraceHandler.INSTANCE.saveMethodTrace(t, ifNull(caller, ThreadPool.class), fn.getClass().getName(), id == null ? null : new Object[]{id}, + r, ex, System.nanoTime() - s); } + return r; } - T r = null; - Throwable ex = null; - long s = System.nanoTime(); try { - r = fn.invoke(); + return fn.invoke(); } catch (Throwable e) { - TraceHandler.INSTANCE.log(toString(), ex = e); + TraceHandler.INSTANCE.log(toString(), e); throw e; - } finally { - Thread t = Thread.currentThread(); - TraceHandler.INSTANCE.saveMethodTrace(t, fn.getClass(), Reflects.getStackTrace(t), id == null ? null : new Object[]{id}, - r, ex, System.nanoTime() - s); } - return r; } @Override diff --git a/rxlib/src/main/java/org/rx/exception/TraceHandler.java b/rxlib/src/main/java/org/rx/exception/TraceHandler.java index 46076b68..9d827019 100644 --- a/rxlib/src/main/java/org/rx/exception/TraceHandler.java +++ b/rxlib/src/main/java/org/rx/exception/TraceHandler.java @@ -84,6 +84,7 @@ public static class MetricsEntity implements Serializable { Date modifyTime; } + String FIRST_FLAG = "\nat "; public static final TraceHandler INSTANCE = new TraceHandler(); public static Object[] getMessageCandidate(Object... args) { @@ -193,10 +194,10 @@ public void saveExceptionTrace(Thread t, String msg, Throwable e) { void innerSave(String thread, String msg, Throwable e) { RxConfig.TraceConfig conf = RxConfig.INSTANCE.getTrace(); String stackTrace = ExceptionUtils.getStackTrace(e); - int eMsgFlag = stackTrace.indexOf("\n"); + int eMsgFlag = stackTrace.indexOf(FIRST_FLAG); String eMsg = stackTrace.substring(0, eMsgFlag); stackTrace = stackTrace.substring(eMsgFlag + 2); - msg = eMsg + ifNull(msg, ""); + msg = msg == null ? eMsg : String.format("%s\n%s", eMsg, msg); long pk = CodecUtil.hash64(stackTrace); // Tasks.nextPool().runSerial(() -> {