diff --git a/README.md b/README.md index d93590c..c3435d9 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,23 @@ V/Example: ⇢ getName(first="Jake", last="Wharton") V/Example: ⇠ getName [16ms] = "Jake Wharton" ``` +You can disable annotated methods selectively by using the `enabled` parameter, allowing you to toggle +groups of methods based on a common value. For example: + +```java +private static final boolean DEBUG_LOG_NAMES = false; + +@DebugLog(enabled = DEBUG_LOG_NAMES) +public String getFirstName() { + return first; +} + +@DebugLog(enabled = DEBUG_LOG_NAMES) +public String getLastName() { + return last; +} +``` + The logging will only happen in debug builds and the annotation itself is never present in the compiled class file for any build type. This means you can keep the annotation and check it into source control. It has zero effect on non-debug builds. diff --git a/hugo-annotations/src/main/java/hugo/weaving/DebugLog.java b/hugo-annotations/src/main/java/hugo/weaving/DebugLog.java index d3ff040..697ca15 100644 --- a/hugo-annotations/src/main/java/hugo/weaving/DebugLog.java +++ b/hugo-annotations/src/main/java/hugo/weaving/DebugLog.java @@ -10,4 +10,5 @@ @Target({TYPE, METHOD, CONSTRUCTOR}) @Retention(CLASS) public @interface DebugLog { + boolean enabled() default true; } diff --git a/hugo-example/src/main/java/com/example/hugo/HugoActivity.java b/hugo-example/src/main/java/com/example/hugo/HugoActivity.java index a03c7b0..ee22c1c 100644 --- a/hugo-example/src/main/java/com/example/hugo/HugoActivity.java +++ b/hugo-example/src/main/java/com/example/hugo/HugoActivity.java @@ -8,6 +8,10 @@ import hugo.weaving.DebugLog; public class HugoActivity extends Activity { + + // set this to false to disable logging for Greeter methods + private static final boolean DEBUG_LOG_GREETER = true; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); @@ -64,12 +68,12 @@ private void sleepyMethod(long milliseconds) { static class Greeter { private final String name; - @DebugLog + @DebugLog(enabled = DEBUG_LOG_GREETER) Greeter(String name) { this.name = name; } - @DebugLog + @DebugLog(enabled = DEBUG_LOG_GREETER) public String sayHello() { return "Hello, " + name; } diff --git a/hugo-runtime/src/main/java/hugo/weaving/internal/Hugo.java b/hugo-runtime/src/main/java/hugo/weaving/internal/Hugo.java index 53041ec..99e0397 100644 --- a/hugo-runtime/src/main/java/hugo/weaving/internal/Hugo.java +++ b/hugo-runtime/src/main/java/hugo/weaving/internal/Hugo.java @@ -16,7 +16,7 @@ @Aspect public class Hugo { - @Pointcut("within(@hugo.weaving.DebugLog *)") + @Pointcut("within(@hugo.weaving.DebugLog(enabled=true) *)") public void withinAnnotatedClass() {} @Pointcut("execution(* *(..)) && withinAnnotatedClass()") @@ -25,10 +25,10 @@ public void methodInsideAnnotatedType() {} @Pointcut("execution(*.new(..)) && withinAnnotatedClass()") public void constructorInsideAnnotatedType() {} - @Pointcut("execution(@hugo.weaving.DebugLog * *(..)) || methodInsideAnnotatedType()") + @Pointcut("execution(@hugo.weaving.DebugLog(enabled=true) * *(..)) || methodInsideAnnotatedType()") public void method() {} - @Pointcut("execution(@hugo.weaving.DebugLog *.new(..)) || constructorInsideAnnotatedType()") + @Pointcut("execution(@hugo.weaving.DebugLog(enabled=true) *.new(..)) || constructorInsideAnnotatedType()") public void constructor() {} @Around("method() || constructor()")