From ad63de628d8db92acaa8348e4f7960a51e2dda05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E6=88=9F?= Date: Sat, 5 Dec 2020 00:12:53 +0800 Subject: [PATCH] refactor log methods --- .../com/alibaba/testable/agent/PreMain.java | 8 +++- .../agent/handler/SourceClassHandler.java | 8 ++-- .../transformer/TestableClassTransformer.java | 22 ++++----- .../alibaba/testable/agent/util/LogUtil.java | 33 ------------- .../testable/core/annotation/MockWith.java | 2 +- .../testable/core/model/MockDiagnose.java | 7 +-- .../testable/core/util/InvokeRecordUtil.java | 2 + .../alibaba/testable/core/util/LogUtil.java | 46 +++++++++++++++++++ 8 files changed, 68 insertions(+), 60 deletions(-) delete mode 100644 testable-agent/src/main/java/com/alibaba/testable/agent/util/LogUtil.java create mode 100644 testable-core/src/main/java/com/alibaba/testable/core/util/LogUtil.java diff --git a/testable-agent/src/main/java/com/alibaba/testable/agent/PreMain.java b/testable-agent/src/main/java/com/alibaba/testable/agent/PreMain.java index ba89511..fb79769 100755 --- a/testable-agent/src/main/java/com/alibaba/testable/agent/PreMain.java +++ b/testable-agent/src/main/java/com/alibaba/testable/agent/PreMain.java @@ -1,7 +1,8 @@ package com.alibaba.testable.agent; import com.alibaba.testable.agent.transformer.TestableClassTransformer; -import com.alibaba.testable.agent.util.LogUtil; +import com.alibaba.testable.core.util.LogUtil; +import com.alibaba.testable.core.model.MockDiagnose; import java.lang.instrument.Instrumentation; @@ -13,6 +14,7 @@ public class PreMain { private static final String AND = "&"; private static final String DEBUG = "debug"; + private static final String VERBOSE = "verbose"; public static void premain(String agentArgs, Instrumentation inst) { parseArgs(agentArgs); @@ -25,7 +27,9 @@ public class PreMain { } for (String a : args.split(AND)) { if (a.equals(DEBUG)) { - LogUtil.globalDebugEnable = true; + LogUtil.setDefaultLevel(LogUtil.LEVEL_DIAGNOSE); + } else if (a.equals(VERBOSE)) { + LogUtil.setDefaultLevel(LogUtil.LEVEL_VERBOSE); } } } diff --git a/testable-agent/src/main/java/com/alibaba/testable/agent/handler/SourceClassHandler.java b/testable-agent/src/main/java/com/alibaba/testable/agent/handler/SourceClassHandler.java index 2bc8b53..cd84836 100644 --- a/testable-agent/src/main/java/com/alibaba/testable/agent/handler/SourceClassHandler.java +++ b/testable-agent/src/main/java/com/alibaba/testable/agent/handler/SourceClassHandler.java @@ -4,7 +4,7 @@ import com.alibaba.testable.agent.constant.ConstPool; import com.alibaba.testable.agent.model.MethodInfo; import com.alibaba.testable.agent.util.BytecodeUtil; import com.alibaba.testable.agent.util.ClassUtil; -import com.alibaba.testable.agent.util.LogUtil; +import com.alibaba.testable.core.util.LogUtil; import org.objectweb.asm.Opcodes; import org.objectweb.asm.tree.*; @@ -52,7 +52,7 @@ public class SourceClassHandler extends BaseClassHandler { private void transformMethod(ClassNode cn, MethodNode mn, Set memberInjectMethods, Set newOperatorInjectMethods) { - LogUtil.debug(" Handling method %s", mn.name); + LogUtil.diagnose(" Handling method %s", mn.name); AbstractInsnNode[] instructions = mn.instructions.toArray(); List memberInjectMethodList = new ArrayList(memberInjectMethods); int i = 0; @@ -155,7 +155,7 @@ public class SourceClassHandler extends BaseClassHandler { private AbstractInsnNode[] replaceNewOps(ClassNode cn, MethodNode mn, String newOperatorInjectMethodName, AbstractInsnNode[] instructions, int start, int end) { - LogUtil.debug(" Line %d, mock method %s used", getLineNum(instructions, start), + LogUtil.diagnose(" Line %d, mock method %s used", getLineNum(instructions, start), newOperatorInjectMethodName); String classType = ((TypeInsnNode)instructions[start]).desc; String constructorDesc = ((MethodInsnNode)instructions[end]).desc; @@ -187,7 +187,7 @@ public class SourceClassHandler extends BaseClassHandler { private AbstractInsnNode[] replaceMemberCallOps(ClassNode cn, MethodNode mn, String substitutionMethod, AbstractInsnNode[] instructions, String ownerClass, int opcode, int start, int end) { - LogUtil.debug(" Line %d, mock method %s used", getLineNum(instructions, start), substitutionMethod); + LogUtil.diagnose(" Line %d, mock method %s used", getLineNum(instructions, start), substitutionMethod); mn.maxStack++; MethodInsnNode method = (MethodInsnNode)instructions[end]; String testClassName = ClassUtil.getTestClassName(cn.name); diff --git a/testable-agent/src/main/java/com/alibaba/testable/agent/transformer/TestableClassTransformer.java b/testable-agent/src/main/java/com/alibaba/testable/agent/transformer/TestableClassTransformer.java index 684b107..9b8f804 100644 --- a/testable-agent/src/main/java/com/alibaba/testable/agent/transformer/TestableClassTransformer.java +++ b/testable-agent/src/main/java/com/alibaba/testable/agent/transformer/TestableClassTransformer.java @@ -9,7 +9,7 @@ import com.alibaba.testable.agent.model.MethodInfo; import com.alibaba.testable.agent.tool.ComparableWeakRef; import com.alibaba.testable.agent.util.AnnotationUtil; import com.alibaba.testable.agent.util.ClassUtil; -import com.alibaba.testable.agent.util.LogUtil; +import com.alibaba.testable.core.util.LogUtil; import com.alibaba.testable.core.model.MockDiagnose; import org.objectweb.asm.ClassReader; import org.objectweb.asm.tree.AnnotationNode; @@ -43,15 +43,15 @@ public class TestableClassTransformer implements ClassFileTransformer { try { if (shouldTransformAsSourceClass(className)) { // it's a source class with testable enabled - LogUtil.debug("Handling source class %s", className); + LogUtil.diagnose("Handling source class %s", className); List injectMethods = getTestableMockMethods(ClassUtil.getTestClassName(className)); return new SourceClassHandler(injectMethods).getBytes(classFileBuffer); } else if (shouldTransformAsTestClass(className)) { // it's a test class with testable enabled - LogUtil.debug("Handling test class %s", className); + LogUtil.diagnose("Handling test class %s", className); return new TestClassHandler().getBytes(classFileBuffer); } - resetMockContext(); + LogUtil.resetLogLevel(); } catch (IOException e) { return null; } @@ -79,7 +79,7 @@ public class TestableClassTransformer implements ClassFileTransformer { for (MethodNode mn : cn.methods) { checkMethodAnnotation(cn, methodInfos, mn); } - LogUtil.debug(" Found %d mock methods", methodInfos.size()); + LogUtil.diagnose(" Found %d mock methods", methodInfos.size()); return methodInfos; } catch (Exception e) { return new ArrayList(); @@ -149,18 +149,12 @@ public class TestableClassTransformer implements ClassFileTransformer { } private void setupMockContext(AnnotationNode an) { - MockDiagnose mockDebug = AnnotationUtil.getAnnotationParameter(an, FIELD_DIAGNOSE, null, MockDiagnose.class); - if (MockDiagnose.ENABLE.equals(mockDebug)) { - LogUtil.enableDebugLog(); - } else if (MockDiagnose.DISABLE.equals(mockDebug)) { - LogUtil.disableDebugLog(); + MockDiagnose diagnose = AnnotationUtil.getAnnotationParameter(an, FIELD_DIAGNOSE, null, MockDiagnose.class); + if (diagnose != null) { + LogUtil.enableDiagnose(diagnose == MockDiagnose.ENABLE); } } - private void resetMockContext() { - LogUtil.resetDebugLog(); - } - /** * Split desc to "first parameter" and "desc of rest parameters" * @param desc method desc diff --git a/testable-agent/src/main/java/com/alibaba/testable/agent/util/LogUtil.java b/testable-agent/src/main/java/com/alibaba/testable/agent/util/LogUtil.java deleted file mode 100644 index 0cff7a2..0000000 --- a/testable-agent/src/main/java/com/alibaba/testable/agent/util/LogUtil.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.alibaba.testable.agent.util; - -/** - * @author flin - */ -public class LogUtil { - - private static final int LEVEL_ERROR = 0; - private static final int LEVEL_WARN = 1; - private static final int LEVEL_DIAGNOSE = 2; - private static int level; - - public static boolean globalDebugEnable = false; - - public static void debug(String msg, Object... args) { - if (level >= LEVEL_DIAGNOSE) { - System.out.println(String.format("[DIAGNOSE] " + msg, args)); - } - } - - public static void enableDebugLog() { - level = LEVEL_DIAGNOSE; - } - - public static void disableDebugLog() { - level = LEVEL_ERROR; - } - - public static void resetDebugLog() { - level = globalDebugEnable ? LEVEL_DIAGNOSE : LEVEL_WARN; - } - -} diff --git a/testable-core/src/main/java/com/alibaba/testable/core/annotation/MockWith.java b/testable-core/src/main/java/com/alibaba/testable/core/annotation/MockWith.java index 6a729a7..4b9b5cc 100644 --- a/testable-core/src/main/java/com/alibaba/testable/core/annotation/MockWith.java +++ b/testable-core/src/main/java/com/alibaba/testable/core/annotation/MockWith.java @@ -18,6 +18,6 @@ public @interface MockWith { * switch of mock diagnose information of current test class * @return enable or disable */ - MockDiagnose diagnose() default MockDiagnose.WARN_ONLY; + MockDiagnose diagnose() default MockDiagnose.DISABLE; } diff --git a/testable-core/src/main/java/com/alibaba/testable/core/model/MockDiagnose.java b/testable-core/src/main/java/com/alibaba/testable/core/model/MockDiagnose.java index 1dfad4d..ff781e5 100644 --- a/testable-core/src/main/java/com/alibaba/testable/core/model/MockDiagnose.java +++ b/testable-core/src/main/java/com/alibaba/testable/core/model/MockDiagnose.java @@ -12,12 +12,7 @@ public enum MockDiagnose { DISABLE, /** - * Only show warning message - */ - WARN_ONLY, - - /** - * Print detail diagnose logs + * Print diagnose logs */ ENABLE diff --git a/testable-core/src/main/java/com/alibaba/testable/core/util/InvokeRecordUtil.java b/testable-core/src/main/java/com/alibaba/testable/core/util/InvokeRecordUtil.java index 28874c0..60fab46 100644 --- a/testable-core/src/main/java/com/alibaba/testable/core/util/InvokeRecordUtil.java +++ b/testable-core/src/main/java/com/alibaba/testable/core/util/InvokeRecordUtil.java @@ -35,8 +35,10 @@ public class InvokeRecordUtil { List records = getInvokeRecord(mockMethodName, testCaseName); if (isConstructor) { records.add(args); + LogUtil.verbose("Mock constructor invoked \"%s\"", key); } else { records.add(slice(args, 1)); + LogUtil.verbose("Mock method invoked \"%s\"", key); } INVOKE_RECORDS.put(key, records); } diff --git a/testable-core/src/main/java/com/alibaba/testable/core/util/LogUtil.java b/testable-core/src/main/java/com/alibaba/testable/core/util/LogUtil.java new file mode 100644 index 0000000..220eeb2 --- /dev/null +++ b/testable-core/src/main/java/com/alibaba/testable/core/util/LogUtil.java @@ -0,0 +1,46 @@ +package com.alibaba.testable.core.util; + +/** + * @author flin + */ +public class LogUtil { + + public static final int LEVEL_ERROR = 0; + public static final int LEVEL_WARN = 1; + public static final int LEVEL_DIAGNOSE = 2; + public static final int LEVEL_VERBOSE = 3; + + private static int defaultLogLevel = LEVEL_WARN; + private static int level; + + public static void verbose(String msg, Object... args) { + if (level >= LEVEL_VERBOSE) { + System.out.println(String.format("[VERBOSE] " + msg, args)); + } + } + + public static void diagnose(String msg, Object... args) { + if (level >= LEVEL_DIAGNOSE) { + System.out.println(String.format("[DIAGNOSE] " + msg, args)); + } + } + + public static void warn(String msg, Object... args) { + if (level >= LEVEL_WARN) { + System.out.println(String.format("[WARN] " + msg, args)); + } + } + + public static void enableDiagnose(boolean enable) { + level = enable ? LEVEL_DIAGNOSE : LEVEL_ERROR; + } + + public static void setDefaultLevel(int level) { + defaultLogLevel = level; + } + + public static void resetLogLevel() { + level = defaultLogLevel; + } + +}