Skip to content

Add stack trace capture implementation #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@
<configuration>
<jdkToolchain>
<version>${maven.compiler.release}</version>
<vendor>oracle</vendor>
</jdkToolchain>
</configuration>
</plugin>
Expand Down
50 changes: 50 additions & 0 deletions src/main/java/com/nordstrom/common/base/StackTrace.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.nordstrom.common.base;

/**
* Throwable created purely for the purposes of reporting a stack trace.
*
* This is not an Error or an Exception and is not expected to be thrown or caught.
* This <a href='https://blog.vanillajava.blog/2021/12/unusual-java-stacktrace-extends.html'>blog post</a> provided the
* original implementation.
* @author <a href='https://github.com/peter-lawrey'>Peter K Lawrey</a>
*/

public class StackTrace extends Throwable {

private static final long serialVersionUID = -3623586250962214453L;

public StackTrace() {
this("stack trace");
}

public StackTrace(String message) {
this(message, null);
}

public StackTrace(String message, Throwable cause) {
super(message + " on " + Thread.currentThread().getName(), cause);
}

public static StackTrace forThread(Thread t) {
if (t == null) return null;

StackTrace st = new StackTrace(t.toString());
StackTraceElement[] stackTrace = t.getStackTrace();
int start = 0;

if (stackTrace.length > 2) {
if (stackTrace[0].isNativeMethod()) {
start++;
}
}

if (start > 0) {
StackTraceElement[] ste2 = new StackTraceElement[stackTrace.length - start];
System.arraycopy(stackTrace, start, ste2, 0, ste2.length);
stackTrace = ste2;
}

st.setStackTrace(stackTrace);
return st;
}
}