sirius.kernel.async.ExecutionPoint Maven / Gradle / Ivy
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - [email protected]
*/
package sirius.kernel.async;
import sirius.kernel.commons.Tuple;
import java.util.List;
/**
* Represents a state of execution, which is kind of an extended stack trace.
*
* In particular, it combines the stack trace of the current thread with
* its MDC ({@link CallContext#getMDC()}). And therefore provides a good insight
* on what was happening at the given point in time.
*
* A readable representation can be generated by calling {@link #toString()}
*/
public class ExecutionPoint {
private String threadName;
private Exception stacktraceHolder;
private List> mdc;
/*
* Use the static factory method snapshot()
*/
private ExecutionPoint() {
}
/**
* Generates a new instance of the current thread.
*
* @return a new instance representing the current state of the current thread
*/
public static ExecutionPoint snapshot() {
return snapshot(false);
}
/**
* Generates a new instance of the current thread.
*
* This does skip saving the MDC and only records the current stack trace, as this is blazing fast.
*
* @return a new instance representing the current state of the current thread
*/
public static ExecutionPoint fastSnapshot() {
return snapshot(true);
}
private static ExecutionPoint snapshot(boolean fast) {
ExecutionPoint result = new ExecutionPoint();
result.threadName = Thread.currentThread().getName();
result.stacktraceHolder = new Exception();
if (!fast) {
result.mdc = CallContext.getCurrent().getMDC();
}
return result;
}
@Override
public String toString() {
StringBuilder msg = new StringBuilder();
msg.append("Execution Point in: ");
msg.append(threadName);
msg.append("\n---------------------------------------------------\n");
for (StackTraceElement e : stacktraceHolder.getStackTrace()) {
msg.append(e.getClassName())
.append(".")
.append(e.getMethodName())
.append(" (")
.append(e.getFileName())
.append(":")
.append(e.getLineNumber())
.append(")\n");
}
if (mdc != null) {
msg.append("\n---------------------------------------------------\n");
for (Tuple t : mdc) {
msg.append(t.getFirst()).append(": ").append(t.getSecond()).append("\n");
}
}
msg.append("\n---------------------------------------------------");
return msg.toString();
}
}