Skip to content

Commit

Permalink
Added the ability to see original stack traces with log messages
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerBenGera committed May 6, 2023
1 parent 31842fe commit f5c91cf
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
Expand Up @@ -5,6 +5,7 @@
import com.bgsoftware.superiorskyblock.core.database.sql.session.QueryResult;
import com.bgsoftware.superiorskyblock.core.logging.Debug;
import com.bgsoftware.superiorskyblock.core.logging.Log;
import com.bgsoftware.superiorskyblock.core.logging.StackTrace;
import com.bgsoftware.superiorskyblock.core.threads.BukkitExecutor;

import java.sql.Connection;
Expand Down Expand Up @@ -97,7 +98,15 @@ private void executeQuery(boolean async, QueryResult<PreparedStatement> queryRes
return;

if (async && !BukkitExecutor.isDataThread()) {
BukkitExecutor.data(() -> executeQuery(false, queryResult));
StackTrace stackTrace = new StackTrace();
BukkitExecutor.data(() -> {
try {
Log.attachStackTrace(stackTrace);
executeQuery(false, queryResult);
} finally {
Log.detachStackTrace();
}
});
return;
}

Expand Down
Expand Up @@ -12,11 +12,20 @@ public class Log {

private static final EnumSet<Debug> DEBUG_FILTERS = EnumSet.noneOf(Debug.class);
private static boolean debugMode = false;
private static final ThreadLocal<StackTrace> originalStackTrace = new ThreadLocal<>();

private Log() {

}

public static void attachStackTrace(StackTrace stackTrace) {
originalStackTrace.set(stackTrace);
}

public static void detachStackTrace() {
originalStackTrace.set(null);
}

public static void info(Object first, Object... parts) {
logInternal(Level.INFO, first, parts);
}
Expand Down Expand Up @@ -135,7 +144,10 @@ private static String[] getClassAndMethodNames() {
}

private static void printStackTrace() {
new Exception().printStackTrace();
Thread.dumpStack();
StackTrace originalStackTrace = Log.originalStackTrace.get();
if (originalStackTrace != null)
originalStackTrace.dump();
}

}
@@ -0,0 +1,19 @@
package com.bgsoftware.superiorskyblock.core.logging;

public class StackTrace {

private final StackTraceHolder stackTrace = new StackTraceHolder();

public void dump() {
this.stackTrace.printStackTrace();
}

private static class StackTraceHolder extends Throwable {

@Override
public String toString() {
return "Original Stacktrace:";
}
}

}

0 comments on commit f5c91cf

Please sign in to comment.