From 1d209993bfcf03c56505e9ca4a54c06e66cf181e Mon Sep 17 00:00:00 2001 From: Kane O'Riley Date: Mon, 10 Aug 2015 21:24:31 +0930 Subject: [PATCH 1/2] Add annotation parameter to enable finer grained control of logging using static final booleans --- README.md | 17 +++++++++++++++++ .../src/main/java/hugo/weaving/DebugLog.java | 1 + .../main/java/hugo/weaving/internal/Hugo.java | 6 +++--- 3 files changed, 21 insertions(+), 3 deletions(-) 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-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()") From 0873beb4d6402a7db41d0254116d59ef6e9604ff Mon Sep 17 00:00:00 2001 From: Kane O'Riley Date: Mon, 10 Aug 2015 21:35:59 +0930 Subject: [PATCH 2/2] Add example of annotation parameter --- .../src/main/java/com/example/hugo/HugoActivity.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) 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; }