Skip to content

Commit

Permalink
MethodLogger: Allow specifying a different format string for each method
Browse files Browse the repository at this point in the history
  • Loading branch information
palant committed Mar 2, 2021
1 parent 77be944 commit 03b429f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 20 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Configuration options:
* `CallLogger.enabled`: add to enable this component
* `CallLogger.filter`: (optional) restricts functionality to a set of methods, for value format see Method filters section above
* `CallLogger.tag`: (optional) log tag to be used (default is `CallLogger`)
* `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).
* `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).

## StreamLogger component

Expand All @@ -68,14 +68,13 @@ Configuration options:

## MethodLogger component

This component will add logging to the start of each method. In addition to the method signature, the parameter values will be logged.
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.

Configuration options:

* `MethodLogger.enabled`: add to enable this component
* `MethodLogger.filter`: (optional) restricts functionality to a set of methods, for value format see Method filters section above
* `MethodLogger.tag`: (optional) log tag to be used (default is `MethodLogger`)
* `MethodLogger.format`: (optional) extended format string to be used, see Extended format strings section above (default is `Entered method {method:%s} ({args:%s})`)
* `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).

## AssignmentRemover component

Expand Down
5 changes: 2 additions & 3 deletions config.properties.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ keystore=debug.jks
keypass=123456

MethodLogger.enabled = true
MethodLogger.filter = com.example.funnygame.* \
com.example.util.Util:* \
com.example.util.Downloader:download()
MethodLogger.tag = MethodEntered
MethodLogger.com.example.funnygame.* = Entered method {method:%s} ({args:%s})
MethodLogger.com.example.util.Downloader\:download() = Download {this:%x} started

AssignmentRemover.enabled = true
AssignmentRemover.filter = com.example.funnygame.SomeClass:<clinit>()
Expand Down
18 changes: 5 additions & 13 deletions src/info/palant/apkInstrumentation/MethodLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,28 @@

public class MethodLogger extends BodyTransformer
{
private final MethodConfig filter;
private String tag;
private String format;
private MethodConfig methodConfig;

public MethodLogger(Properties config)
{
String filterSpec = config.getProperty("MethodLogger.filter");
if (filterSpec != null)
this.filter = new MethodConfig(filterSpec, "");
else
this.filter = null;

this.tag = config.getProperty("MethodLogger.tag");
if (tag == null)
this.tag = "MethodLogger";

this.format = config.getProperty("MethodLogger.format");
if (this.format == null)
this.format = "Entered method {method:%s} ({args:%s})";
this.methodConfig = new MethodConfig(config, "MethodLogger.");
}

@Override
protected void internalTransform(Body body, String phaseName, Map<String, String> options)
{
if (this.filter != null && this.filter.get(body.getMethod()) == null)
String formatString = this.methodConfig.get(body.getMethod());
if (formatString == null)
return;

UnitSequence units = new UnitSequence(body);
units.log(this.tag, units.extendedFormat(
this.format,
formatString,
null,
body.getThisLocal(),
body.getParameterLocals().stream().map(local -> (Value)local).collect(Collectors.toList())
Expand Down

0 comments on commit 03b429f

Please sign in to comment.