Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions hugo-annotations/src/main/java/hugo/weaving/DebugLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@

@Target({TYPE, METHOD, CONSTRUCTOR}) @Retention(CLASS)
public @interface DebugLog {
boolean enabled() default true;
}
8 changes: 6 additions & 2 deletions hugo-example/src/main/java/com/example/hugo/HugoActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions hugo-runtime/src/main/java/hugo/weaving/internal/Hugo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()")
Expand All @@ -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()")
Expand Down