Skip to content

Commit 03b429f

Browse files
committed
MethodLogger: Allow specifying a different format string for each method
1 parent 77be944 commit 03b429f

File tree

3 files changed

+10
-20
lines changed

3 files changed

+10
-20
lines changed

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Configuration options:
5353
* `CallLogger.enabled`: add to enable this component
5454
* `CallLogger.filter`: (optional) restricts functionality to a set of methods, for value format see Method filters section above
5555
* `CallLogger.tag`: (optional) log tag to be used (default is `CallLogger`)
56-
* `CallLogger.<class>:<method>`: specifies a call to be logged. `<class>` has to be a full class name like `java.net.URL`. `<method>` can be either a method name like `openConnection` or a more specific method name along with parameter types like `getHeaderField(java.lang.String)`. The value is a format string (see Extended format strings section above).
56+
* `CallLogger.<method filter>`: specifies that calls to the specified method should be logged. `<method filter>` is a method specification as outlined in the Method filters section above. Note that `.properties` format requires colons to be prefixed with a backslash: `\:`. The value is a format string (see Extended format strings section above).
5757

5858
## StreamLogger component
5959

@@ -68,14 +68,13 @@ Configuration options:
6868

6969
## MethodLogger component
7070

71-
This component will add logging to the start of each method. In addition to the method signature, the parameter values will be logged.
71+
This component will add logging to the start of a method. This might be more efficient than `CallLogger` for methods called from many places, and it will log even calls resulting in an exception. The disadvantage is that the method’s return value cannot be logged as it isn’t known at this stage.
7272

7373
Configuration options:
7474

7575
* `MethodLogger.enabled`: add to enable this component
76-
* `MethodLogger.filter`: (optional) restricts functionality to a set of methods, for value format see Method filters section above
7776
* `MethodLogger.tag`: (optional) log tag to be used (default is `MethodLogger`)
78-
* `MethodLogger.format`: (optional) extended format string to be used, see Extended format strings section above (default is `Entered method {method:%s} ({args:%s})`)
77+
* `MethodLogger.<method filter>`: specifies a method that should be logged. `<method filter>` is a method specification as outlined in the Method filters section above. Note that `.properties` format requires colons to be prefixed with a backslash: `\:`. The value is a format string like `Entered method {method:%s} ({args:%s})` (see Extended format strings section above).
7978

8079
## AssignmentRemover component
8180

config.properties.example

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ keystore=debug.jks
55
keypass=123456
66

77
MethodLogger.enabled = true
8-
MethodLogger.filter = com.example.funnygame.* \
9-
com.example.util.Util:* \
10-
com.example.util.Downloader:download()
118
MethodLogger.tag = MethodEntered
9+
MethodLogger.com.example.funnygame.* = Entered method {method:%s} ({args:%s})
10+
MethodLogger.com.example.util.Downloader\:download() = Download {this:%x} started
1211

1312
AssignmentRemover.enabled = true
1413
AssignmentRemover.filter = com.example.funnygame.SomeClass:<clinit>()

src/info/palant/apkInstrumentation/MethodLogger.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,28 @@
1616

1717
public class MethodLogger extends BodyTransformer
1818
{
19-
private final MethodConfig filter;
2019
private String tag;
21-
private String format;
20+
private MethodConfig methodConfig;
2221

2322
public MethodLogger(Properties config)
2423
{
25-
String filterSpec = config.getProperty("MethodLogger.filter");
26-
if (filterSpec != null)
27-
this.filter = new MethodConfig(filterSpec, "");
28-
else
29-
this.filter = null;
30-
3124
this.tag = config.getProperty("MethodLogger.tag");
3225
if (tag == null)
3326
this.tag = "MethodLogger";
3427

35-
this.format = config.getProperty("MethodLogger.format");
36-
if (this.format == null)
37-
this.format = "Entered method {method:%s} ({args:%s})";
28+
this.methodConfig = new MethodConfig(config, "MethodLogger.");
3829
}
3930

4031
@Override
4132
protected void internalTransform(Body body, String phaseName, Map<String, String> options)
4233
{
43-
if (this.filter != null && this.filter.get(body.getMethod()) == null)
34+
String formatString = this.methodConfig.get(body.getMethod());
35+
if (formatString == null)
4436
return;
4537

4638
UnitSequence units = new UnitSequence(body);
4739
units.log(this.tag, units.extendedFormat(
48-
this.format,
40+
formatString,
4941
null,
5042
body.getThisLocal(),
5143
body.getParameterLocals().stream().map(local -> (Value)local).collect(Collectors.toList())

0 commit comments

Comments
 (0)