|
| 1 | +package cn.com.venvy.common.report; |
| 2 | + |
| 3 | +import android.support.annotation.NonNull; |
| 4 | +import android.text.TextUtils; |
| 5 | + |
| 6 | +import cn.com.venvy.Platform; |
| 7 | +import cn.com.venvy.common.utils.VenvyLog; |
| 8 | + |
| 9 | +/** |
| 10 | + * Created by yanjiangbo on 2017/5/4. |
| 11 | + */ |
| 12 | + |
| 13 | +public class Report { |
| 14 | + |
| 15 | + |
| 16 | + private static ReportHelper mReportHelper; |
| 17 | + |
| 18 | + public enum ReportLevel { |
| 19 | + |
| 20 | + i(0, "info", 0b00000001, true), |
| 21 | + w(1, "warning", 0b00000010, true), |
| 22 | + e(2, "error", 0b00000100, true), |
| 23 | + u(3, "u", 0b00001000, true), |
| 24 | + d(4, "default", 0b00010000, true); |
| 25 | + |
| 26 | + private int mSing = -1; |
| 27 | + private boolean mEnable; |
| 28 | + private int mTagNum; |
| 29 | + private String mName; |
| 30 | + |
| 31 | + ReportLevel(int sing, String name, int num, boolean enable) { |
| 32 | + mSing = sing; |
| 33 | + mEnable = enable; |
| 34 | + mTagNum = num; |
| 35 | + mName = name; |
| 36 | + } |
| 37 | + |
| 38 | + public void setEnable(boolean enable) { |
| 39 | + this.mEnable = enable; |
| 40 | + } |
| 41 | + |
| 42 | + public boolean isEnable() { |
| 43 | + return mEnable; |
| 44 | + } |
| 45 | + |
| 46 | + public int getTagNum() { |
| 47 | + return mTagNum; |
| 48 | + } |
| 49 | + |
| 50 | + public String getName() { |
| 51 | + return mName; |
| 52 | + } |
| 53 | + |
| 54 | + public int getValue() { |
| 55 | + return mSing; |
| 56 | + } |
| 57 | + |
| 58 | + public static void buildLevelAble(int value) { |
| 59 | + i.setEnable(((i.getTagNum() & value) >> i.getValue()) == 1); |
| 60 | + w.setEnable(((w.getTagNum() & value) >> w.getValue()) == 1); |
| 61 | + e.setEnable(((e.getTagNum() & value) >> e.getValue()) == 1); |
| 62 | + u.setEnable(((u.getTagNum() & value) >> u.getValue()) == 1); |
| 63 | + d.setEnable(((d.getTagNum() & value) >> d.getValue()) == 1); |
| 64 | + } |
| 65 | + |
| 66 | + public static ReportLevel getLevel(int value) { |
| 67 | + if (value == i.getValue()) { |
| 68 | + return i; |
| 69 | + } |
| 70 | + if (value == w.getValue()) { |
| 71 | + return w; |
| 72 | + } |
| 73 | + if (value == e.getValue()) { |
| 74 | + return e; |
| 75 | + } |
| 76 | + if (value == u.getValue()) { |
| 77 | + return u; |
| 78 | + } |
| 79 | + return d; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public static void initReport(Platform platform) { |
| 84 | + if (mReportHelper == null) { |
| 85 | + mReportHelper = new ReportHelper(platform); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public static void report(@NonNull final ReportLevel level, @NonNull final String tag, @NonNull final String reportString) { |
| 90 | + |
| 91 | + if (!level.isEnable()) { |
| 92 | + VenvyLog.w("the level is " + level.getValue() + " of Report has closed"); |
| 93 | + return; |
| 94 | + } |
| 95 | + if (mReportHelper == null) { |
| 96 | + return; |
| 97 | + } |
| 98 | + ReportInfo reportInfo = new ReportInfo(); |
| 99 | + reportInfo.level = level; |
| 100 | + reportInfo.message = reportString; |
| 101 | + reportInfo.tag = tag; |
| 102 | + mReportHelper.report(reportInfo); |
| 103 | + } |
| 104 | + |
| 105 | + @Deprecated |
| 106 | + public static void report(@NonNull Exception e) { |
| 107 | + report("crash", e); |
| 108 | + } |
| 109 | + |
| 110 | + public static void report(String tag, @NonNull Exception e) { |
| 111 | + report(tag, e, null); |
| 112 | + } |
| 113 | + |
| 114 | + public static void report(String tag, @NonNull Exception e, String ex) { |
| 115 | + if (!ReportLevel.e.isEnable()) { |
| 116 | + VenvyLog.w("the level is " + ReportLevel.e.getName() + " of Report has closed"); |
| 117 | + return; |
| 118 | + } |
| 119 | + StringBuilder builder = new StringBuilder(); |
| 120 | + if (!TextUtils.isEmpty(ex)) { |
| 121 | + builder.append("ex:"); |
| 122 | + builder.append(ex); |
| 123 | + builder.append("\\n"); |
| 124 | + } |
| 125 | + if (!TextUtils.isEmpty(e.toString())) { |
| 126 | + builder.append("Cause by:"); |
| 127 | + builder.append(e.toString()); |
| 128 | + builder.append("\n"); |
| 129 | + } |
| 130 | + builder.append("\\n"); |
| 131 | + StackTraceElement[] element = e.getStackTrace(); |
| 132 | + if (element != null) { |
| 133 | + for (StackTraceElement i : element) { |
| 134 | + builder.append(i.toString()); |
| 135 | + builder.append("\\n"); |
| 136 | + } |
| 137 | + } |
| 138 | + report(ReportLevel.e, tag, builder.toString()); |
| 139 | + } |
| 140 | + |
| 141 | + public static void report(@NonNull final ReportInfo reportInfo) { |
| 142 | + |
| 143 | + if (mReportHelper == null) { |
| 144 | + return; |
| 145 | + } |
| 146 | + if (!reportInfo.level.isEnable()) { |
| 147 | + VenvyLog.w("the level is " + reportInfo.level.getValue() + " of Report has closed"); |
| 148 | + return; |
| 149 | + } |
| 150 | + if (TextUtils.isEmpty(reportInfo.tag) || TextUtils.isEmpty(reportInfo.message) || reportInfo.level == null) { |
| 151 | + VenvyLog.e("reportInfo is not vaild"); |
| 152 | + return; |
| 153 | + } |
| 154 | + mReportHelper.report(reportInfo); |
| 155 | + |
| 156 | + } |
| 157 | + |
| 158 | + public static void setReportAble(boolean reportAble) { |
| 159 | + if (mReportHelper == null) { |
| 160 | + return; |
| 161 | + } |
| 162 | + mReportHelper.setReportEnable(reportAble); |
| 163 | + } |
| 164 | + |
| 165 | + public static boolean isReportAble() { |
| 166 | + return mReportHelper.isEnable(); |
| 167 | + } |
| 168 | + |
| 169 | + public static void onDestroy() { |
| 170 | + if (mReportHelper != null) { |
| 171 | + mReportHelper.onDestroy(); |
| 172 | + } |
| 173 | + mReportHelper = null; |
| 174 | + } |
| 175 | +} |
0 commit comments