Skip to content

Commit

Permalink
resolved #7.
Browse files Browse the repository at this point in the history
  • Loading branch information
Muyangmin committed Sep 8, 2016
1 parent 3649cc4 commit 3f54be4
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
7 changes: 7 additions & 0 deletions app/src/main/java/org/mym/prettylog/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.mym.plog.config.PLogConfig;
import org.mym.plog.util.SinglePipeLogger;
import org.mym.prettylog.data.User;
import org.mym.prettylog.wrapper.LogWrapper;

import java.util.Random;

Expand Down Expand Up @@ -54,6 +55,12 @@ void basicUsage() {
PLog.i("InfoTag", "This is an info log.");
PLog.w("This is a warn log.");
PLog.e("This is an error log.");

PLogConfig backup = PLog.getCurrentConfig();
//Use wrapper would override globalOffset setting, so DO NOT USE AS MIXED!
LogWrapper.init(this);
LogWrapper.empty();
PLog.init(backup);
}

@OnClick(R.id.btn_log_empty)
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/org/mym/prettylog/wrapper/LogWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ public void addTimingSplit(String splitLabel) {
public void init(Context context) {
PLog.init(PLogConfig.newBuilder()
.useAutoTag(true)
//1 is wrapper layer level
// while LogWrapper is 1 and PLogImpl is 2
.globalStackOffset(2)
.keepLineNumber(true)
.keepInnerClass(true)
.forceConcatGlobalTag(true)
Expand Down
5 changes: 3 additions & 2 deletions plog/src/main/java/org/mym/plog/PLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,8 @@ else if (TextUtils.isEmpty(tag)) {
// * inner class name using only last inner class (if present)
// * if inner class unavailable, use full class name
private static String getAutoTag(int stackOffset) {
final int TARGET_STACK = STACK_TRACE_INDEX + stackOffset - 1;
final int TARGET_STACK = STACK_TRACE_INDEX + mConfig.getGlobalStackOffset()
+ stackOffset - 1;

StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
if (stackTrace == null || stackTrace.length < TARGET_STACK) {
Expand Down Expand Up @@ -368,7 +369,7 @@ private static String wrapLogStr(int stackOffset, String msg, Object... params)
}

private static String getLineNumAndMethodName(int stackOffset) {
final int TARGET_STACK = STACK_TRACE_INDEX + stackOffset;
final int TARGET_STACK = STACK_TRACE_INDEX + mConfig.getGlobalStackOffset() + stackOffset;

StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
if (stackTrace == null || stackTrace.length < TARGET_STACK) {
Expand Down
22 changes: 21 additions & 1 deletion plog/src/main/java/org/mym/plog/config/PLogConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ public class PLogConfig {
private static final int DEFAULT_MAX_LENGTH_PER_LINE = 4000;
// -------------- DEFAULT FIELDS END --------------

/**
* This is a very useful setting when user does NOT directly call `PLog.xxx` but called by
* `PLogWrapper.xxx`, etc.
* Without this setting, the keepLineNumber function would not work correctly.
* @since 1.4.0
*/
private int globalStackOffset;
private String globalTag;
/**
* If this config is set to true, then all tags would be appended after global tag.
Expand Down Expand Up @@ -49,6 +56,8 @@ public class PLogConfig {
private int maxLengthPerLine;

private PLogConfig(Builder builder) {
controller = builder.controller;
globalStackOffset = builder.globalStackOffset;
globalTag = builder.globalTag;
forceConcatGlobalTag = builder.forceConcatGlobalTag;
useAutoTag = builder.useAutoTag;
Expand All @@ -57,7 +66,6 @@ private PLogConfig(Builder builder) {
keepLineNumber = builder.keepLineNumber;
keepInnerClass = builder.keepInnerClass;
logger = builder.logger;
controller = builder.controller;
maxLengthPerLine = builder.maxLengthPerLine;
}

Expand Down Expand Up @@ -108,6 +116,10 @@ public static Builder newBuilder(PLogConfig copy) {
return new Builder(copy);
}

public int getGlobalStackOffset() {
return globalStackOffset;
}

public String getGlobalTag() {
return globalTag;
}
Expand Down Expand Up @@ -160,6 +172,7 @@ public static final class Builder {
private Logger logger;
private int maxLengthPerLine;
private LogController controller;
private int globalStackOffset;

/**
* Create a builder, you can also use static method {@link #newBuilder()}.
Expand All @@ -169,6 +182,7 @@ public Builder() {

public Builder(PLogConfig copy) {
this.controller = copy.controller;
this.globalStackOffset = copy.globalStackOffset;
this.globalTag = copy.globalTag;
this.forceConcatGlobalTag = copy.forceConcatGlobalTag;
this.useAutoTag = copy.useAutoTag;
Expand Down Expand Up @@ -198,6 +212,7 @@ public Builder forceConcatGlobalTag(boolean val) {

/**
* If set to true, then use class name as tag. The concat global tag config is still valid.
*
* @since 1.2.0
*/
public Builder useAutoTag(boolean val) {
Expand Down Expand Up @@ -273,6 +288,11 @@ public Builder controller(LogController val) {
return this;
}

public Builder globalStackOffset(int val) {
globalStackOffset = val;
return this;
}

public PLogConfig build() {
//check fields which can be unsafe
if (emptyMsgLevel < Log.VERBOSE || emptyMsgLevel > Log.ASSERT) {
Expand Down

0 comments on commit 3f54be4

Please sign in to comment.