mirror of
https://github.com/magefree/mage.git
synced 2025-12-20 10:40:06 -08:00
This commit is contained in:
parent
060f421ee7
commit
45ded61d6f
3 changed files with 50 additions and 35 deletions
|
|
@ -282,7 +282,7 @@ public class HumanPlayer extends PlayerImpl {
|
|||
return;
|
||||
}
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Setting game priority for " + getId() + " [" + DebugUtil.getMethodNameWithSource(2) + ']');
|
||||
logger.debug("Setting game priority for " + getId() + " [" + DebugUtil.getMethodNameWithSource(1) + ']');
|
||||
}
|
||||
game.getState().setPriorityPlayerId(getId());
|
||||
}
|
||||
|
|
@ -313,7 +313,7 @@ public class HumanPlayer extends PlayerImpl {
|
|||
while (loop) {
|
||||
// start waiting for next answer
|
||||
response.clear();
|
||||
response.setActiveAction(game, DebugUtil.getMethodNameWithSource(2));
|
||||
response.setActiveAction(game, DebugUtil.getMethodNameWithSource(1));
|
||||
game.resumeTimer(getTurnControlledBy());
|
||||
responseOpenedForAnswer = true;
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,30 @@
|
|||
package org.mage.test.utils;
|
||||
|
||||
import mage.util.DebugUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.mage.test.serverside.base.CardTestPlayerBase;
|
||||
|
||||
/**
|
||||
* @author JayDi85
|
||||
*/
|
||||
public class DebugUtilTest extends CardTestPlayerBase {
|
||||
|
||||
private void firstMethod() {
|
||||
secondMethod();
|
||||
}
|
||||
|
||||
private void secondMethod() {
|
||||
String resCurrent = DebugUtil.getMethodNameWithSource(0);
|
||||
String resPrev = DebugUtil.getMethodNameWithSource(1);
|
||||
String resPrevPrev = DebugUtil.getMethodNameWithSource(2);
|
||||
Assert.assertTrue("must find secondMethod, but get " + resCurrent, resCurrent.startsWith("secondMethod"));
|
||||
Assert.assertTrue("must find firstMethod, but get " + resPrev, resPrev.startsWith("firstMethod"));
|
||||
Assert.assertTrue("must find test_StackTraceWithSourceName, but get " + resPrevPrev, resPrevPrev.startsWith("test_StackTraceWithSourceName"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_StackTraceWithSourceName() {
|
||||
firstMethod();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +1,5 @@
|
|||
package mage.util;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* Devs only: enable or disable debug features
|
||||
* <p>
|
||||
|
|
@ -58,46 +56,33 @@ public class DebugUtil {
|
|||
public static boolean NETWORK_PROFILE_REQUESTS = false; // collect diff time between requests, http status and url into special log file
|
||||
public static String NETWORK_PROFILE_REQUESTS_DUMP_FILE_NAME = "httpRequests.log";
|
||||
|
||||
public static String getMethodNameWithSource(final int depth) {
|
||||
return TraceHelper.getMethodNameWithSource(depth);
|
||||
/**
|
||||
* Return method and source line number like "secondMethod - DebugUtilTest.java:21"
|
||||
*
|
||||
* @param skipMethodsAmount use 0 to return current method info, use 1 for prev method, use 2 for prev-prev method
|
||||
*/
|
||||
public static String getMethodNameWithSource(final int skipMethodsAmount) {
|
||||
// 3 is default methods amount to skip:
|
||||
// - getMethodNameWithSource
|
||||
// - TraceHelper.getMethodNameWithSource
|
||||
// - Thread.currentThread().getStackTrace()
|
||||
return TraceHelper.getMethodNameWithSource(3 + skipMethodsAmount);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Debug: allows to find a caller's method name
|
||||
* <a href="https://stackoverflow.com/a/11726687/1276632">Original code</a>
|
||||
* Debug: allows to find a caller's method name, compatible with java 8 and 9+
|
||||
* <a href="https://stackoverflow.com/a/10992439">Original code</a>
|
||||
*/
|
||||
class TraceHelper {
|
||||
|
||||
private static Method m;
|
||||
|
||||
static {
|
||||
try {
|
||||
m = Throwable.class.getDeclaredMethod("getStackTraceElement", int.class);
|
||||
m.setAccessible(true);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static String getMethodName(final int depth) {
|
||||
try {
|
||||
StackTraceElement element = (StackTraceElement) m.invoke(new Throwable(), depth + 1);
|
||||
return element.getMethodName();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static String getMethodNameWithSource(final int depth) {
|
||||
try {
|
||||
StackTraceElement element = (StackTraceElement) m.invoke(new Throwable(), depth + 1);
|
||||
return String.format("%s - %s:%d", element.getMethodName(), element.getFileName(), element.getLineNumber());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
|
||||
if (stackTrace.length == 0) {
|
||||
return "[no access to stack]";
|
||||
} else {
|
||||
return String.format("%s - %s:%d", stackTrace[depth].getMethodName(), stackTrace[depth].getFileName(), stackTrace[depth].getLineNumber());
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue