Skip to content

Commit 958c802

Browse files
author
hss01248
committed
上报异常
1 parent ed68839 commit 958c802

File tree

12 files changed

+100
-11
lines changed

12 files changed

+100
-11
lines changed

blockcanary-analyzer/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ apply from: 'gradle-mvn-push.gradle'
33

44
android {
55
compileSdkVersion LIBRARY_COMPILE_SDK_VERSION
6-
buildToolsVersion LIBRARY_BUILD_TOOLS_VERSION
6+
buildToolsVersion '28.0.2'
77

88
defaultConfig {
99
minSdkVersion LIBRARY_MIN_SDK_VERSION

blockcanary-analyzer/src/main/java/com/github/moduth/blockcanary/BlockCanaryInternals.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public void onBlockEvent(long realTimeStart, long realTimeEnd,
5959
.setCpuBusyFlag(cpuSampler.isCpuBusy(realTimeStart, realTimeEnd))
6060
.setRecentCpuRate(cpuSampler.getCpuRateInfo())
6161
.setThreadStackEntries(threadStackEntries)
62+
.setStackTraceElements(stackSampler.getTraceElements())
6263
.flushString();
6364
LogWriter.save(blockInfo.toString());
6465

blockcanary-analyzer/src/main/java/com/github/moduth/blockcanary/StackSampler.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ class StackSampler extends AbstractSampler {
3131
private int mMaxEntryCount = DEFAULT_MAX_ENTRY_COUNT;
3232
private Thread mCurrentThread;
3333

34+
public StackTraceElement[] getTraceElements() {
35+
synchronized (sStackMap) {
36+
return traceElements;
37+
}
38+
}
39+
40+
private static StackTraceElement[] traceElements;
41+
3442
public StackSampler(Thread thread, long sampleIntervalMillis) {
3543
this(thread, DEFAULT_MAX_ENTRY_COUNT, sampleIntervalMillis);
3644
}
@@ -59,8 +67,8 @@ public ArrayList<String> getThreadStackEntries(long startTime, long endTime) {
5967
@Override
6068
protected void doSample() {
6169
StringBuilder stringBuilder = new StringBuilder();
62-
63-
for (StackTraceElement stackTraceElement : mCurrentThread.getStackTrace()) {
70+
traceElements = mCurrentThread.getStackTrace();
71+
for (StackTraceElement stackTraceElement : traceElements) {
6472
stringBuilder
6573
.append(stackTraceElement.toString())
6674
.append(BlockInfo.SEPARATOR);

blockcanary-analyzer/src/main/java/com/github/moduth/blockcanary/internal/BlockInfo.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public class BlockInfo {
9494
public boolean cpuBusy;
9595
public String cpuRateInfo;
9696
public ArrayList<String> threadStackEntries = new ArrayList<>();
97+
public StackTraceElement[] stackTraceElements ;
9798

9899
private StringBuilder basicSb = new StringBuilder();
99100
private StringBuilder cpuSb = new StringBuilder();
@@ -163,6 +164,11 @@ public BlockInfo setThreadStackEntries(ArrayList<String> threadStackEntries) {
163164
return this;
164165
}
165166

167+
public BlockInfo setStackTraceElements(StackTraceElement[] stackTraceElements) {
168+
this.stackTraceElements = stackTraceElements;
169+
return this;
170+
}
171+
166172
public BlockInfo setMainThreadTimeCost(long realTimeStart, long realTimeEnd, long threadTimeStart, long threadTimeEnd) {
167173
timeCost = realTimeEnd - realTimeStart;
168174
threadTimeCost = threadTimeEnd - threadTimeStart;
@@ -220,4 +226,22 @@ public String getTimeString() {
220226
public String toString() {
221227
return String.valueOf(basicSb) + timeSb + cpuSb + stackSb;
222228
}
229+
230+
public Exception buildException(){
231+
StringBuilder sb = new StringBuilder("threadTimeCost time cost:")
232+
.append(threadTimeCost)
233+
.append("ms,real time cost:")
234+
.append(timeCost)
235+
.append("ms");
236+
if(timeCost - threadTimeCost > 300){
237+
sb.append(",thread waiting a long time!!!");
238+
}
239+
if(cpuBusy){
240+
sb.append(",cpu is busy !!!");
241+
}
242+
Exception exception = new BlockcanaryException(sb.toString());
243+
exception.setStackTrace(stackTraceElements);
244+
return exception;
245+
246+
}
223247
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.github.moduth.blockcanary.internal;
2+
3+
/**
4+
* time:2019/6/8
5+
* author:hss
6+
* desription:
7+
*/
8+
public class BlockcanaryException extends Exception{
9+
10+
public BlockcanaryException() {
11+
}
12+
13+
public BlockcanaryException(String detailMessage) {
14+
super(detailMessage);
15+
}
16+
17+
public BlockcanaryException(String detailMessage, Throwable throwable) {
18+
super(detailMessage, throwable);
19+
}
20+
21+
public BlockcanaryException(Throwable throwable) {
22+
super(throwable);
23+
}
24+
}

blockcanary-android-no-op/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ apply from: 'gradle-mvn-push.gradle'
33

44
android {
55
compileSdkVersion LIBRARY_COMPILE_SDK_VERSION
6-
buildToolsVersion LIBRARY_BUILD_TOOLS_VERSION
6+
buildToolsVersion '28.0.2'
77

88
defaultConfig {
99
minSdkVersion LIBRARY_MIN_SDK_VERSION
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
11
package com.github.moduth.blockcanary.internal;
22

33
public class BlockInfo {
4+
public Exception buildException(){
5+
return null;
6+
}
7+
8+
public String getBasicString() {
9+
return "";
10+
}
11+
12+
public String getCpuString() {
13+
return "";
14+
}
15+
16+
public String getTimeString() {
17+
return "";
18+
}
19+
20+
public String toString() {
21+
return "";
22+
}
423
}

blockcanary-android/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ apply from: 'gradle-mvn-push.gradle'
33

44
android {
55
compileSdkVersion LIBRARY_COMPILE_SDK_VERSION
6-
buildToolsVersion LIBRARY_BUILD_TOOLS_VERSION
6+
buildToolsVersion '28.0.2'
77

88
defaultConfig {
99
minSdkVersion LIBRARY_MIN_SDK_VERSION
@@ -21,6 +21,6 @@ android {
2121

2222
dependencies {
2323
compile fileTree(include: ['*.jar'], dir: 'libs')
24-
// compile project(':blockcanary-analyzer')
25-
compile 'com.github.markzhai:blockcanary-analyzer:1.5.0'
24+
compile project(':blockcanary-analyzer')
25+
// compile 'com.github.markzhai:blockcanary-analyzer:1.5.0'
2626
}

blockcanary-sample/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'com.android.application'
22

33
android {
44
compileSdkVersion LIBRARY_COMPILE_SDK_VERSION
5-
buildToolsVersion LIBRARY_BUILD_TOOLS_VERSION
5+
buildToolsVersion '28.0.2'
66

77
defaultConfig {
88
applicationId "com.example.blockcanary"

blockcanary-sample/src/main/java/com/example/blockcanary/AppContext.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
*/
1616
package com.example.blockcanary;
1717

18+
import android.content.Context;
1819
import android.content.pm.PackageInfo;
1920
import android.content.pm.PackageManager;
2021
import android.util.Log;
2122

2223
import com.github.moduth.blockcanary.BlockCanaryContext;
24+
import com.github.moduth.blockcanary.internal.BlockInfo;
2325

2426
import java.util.List;
2527

@@ -82,4 +84,14 @@ public List<String> provideWhiteList() {
8284
public boolean stopWhenDebugging() {
8385
return true;
8486
}
87+
88+
@Override
89+
public void onBlock(Context context, BlockInfo blockInfo) {
90+
super.onBlock(context, blockInfo);
91+
Exception e = blockInfo.buildException();
92+
if(e != null){
93+
e.printStackTrace();
94+
}
95+
Log.e("block",blockInfo.toString());
96+
}
8597
}

0 commit comments

Comments
 (0)