Skip to content

Commit dfe2d93

Browse files
committed
Merge pull request orhanobut#9 from ccunniff/master
fix for stack index offset
2 parents 0c78fa3 + 31f4591 commit dfe2d93

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

logger/src/main/java/com/orhanobut/logger/Logger.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public final class Logger {
3232
*/
3333
private static final int MAX_METHOD_COUNT = 5;
3434

35+
/**
36+
* The minimum stack trace index, starts at this class after two native calls.
37+
*/
38+
private static final int MIN_STACK_OFFSET = 3;
39+
3540
/**
3641
* It is used to determine log settings such as method count, thread info visibility
3742
*/
@@ -120,7 +125,7 @@ public static void e(String tag, Exception e) {
120125

121126
public static void e(String message, int methodCount) {
122127
validateMethodCount(methodCount);
123-
e(message, null, methodCount);
128+
e(TAG, message, methodCount);
124129
}
125130

126131
public static void e(String tag, String message, int methodCount) {
@@ -303,8 +308,11 @@ private static void logHeaderContent(int logType, String tag, int methodCount) {
303308
logDivider(logType, tag);
304309
}
305310
String level = "";
311+
312+
int stackOffset = getStackOffset(trace);
313+
306314
for (int i = methodCount; i > 0; i--) {
307-
int stackIndex = i + 5;
315+
int stackIndex = i + stackOffset;
308316
StringBuilder builder = new StringBuilder();
309317
builder.append("║ ")
310318
.append(level)
@@ -407,5 +415,20 @@ public Settings setLogLevel(LogLevel logLevel) {
407415
}
408416
}
409417

418+
/**
419+
* Determines the starting index of the stack trace, after method calls made by this class.
420+
*
421+
* @param trace the stack trace
422+
* @return the stack offset
423+
*/
424+
private static int getStackOffset(StackTraceElement[] trace) {
425+
for (int i = MIN_STACK_OFFSET; i < trace.length; i++) {
426+
StackTraceElement e = trace[i];
427+
if (!e.getClassName().equals(Logger.class.getName())) {
428+
return --i;
429+
}
430+
}
431+
return -1;
432+
}
410433

411434
}

0 commit comments

Comments
 (0)