-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathUserError.java
413 lines (356 loc) · 14.2 KB
/
UserError.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package com.eveningoutpost.dexdrip.Models;
import android.os.AsyncTask;
import android.provider.BaseColumns;
import com.activeandroid.Cache;
import com.activeandroid.Model;
import com.activeandroid.annotation.Column;
import com.activeandroid.annotation.Table;
import com.activeandroid.query.Delete;
import com.activeandroid.query.Select;
import com.eveningoutpost.dexdrip.UtilityModels.Constants;
import com.eveningoutpost.dexdrip.UtilityModels.Pref;
import com.google.gson.annotations.Expose;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;
//import com.bugfender.sdk.Bugfender;
/**
* Created by Emma Black on 8/3/15.
*/
@Table(name = "UserErrors", id = BaseColumns._ID)
public class UserError extends Model {
private final static String TAG = UserError.class.getSimpleName();
@Expose
@Column(name = "shortError")
public String shortError; // Short error message to be displayed on table
@Expose
@Column(name = "message")
public String message; // Additional text when error is expanded
@Expose
@Column(name = "severity", index = true)
public int severity; // int between 1 and 3, 3 being most severe
// 5 = internal lower level user events
// 6 = higher granularity user events
@Expose
@Column(name = "timestamp", index = true)
public long timestamp; // Time the error was raised
//todo: rather than include multiples of the same error, should we have a "Count" and just increase that on duplicates?
//or rather, perhaps we should group up the errors
public String toString()
{
return severity+" ^ "+JoH.dateTimeText((long)timestamp)+" ^ "+shortError+" ^ "+message;
}
public UserError() {}
public UserError(int severity, String shortError, String message) {
this.severity = severity;
this.shortError = shortError;
this.message = message;
this.timestamp = new Date().getTime();
this.save();
/* if (xdrip.useBF) {
switch (severity) {
case 2:
case 3:
Bugfender.e(shortError, message);
break;
case 5:
case 6:
Bugfender.w(shortError, message);
break;
default:
Bugfender.d(shortError, message);
break;
}
}*/
}
public UserError(String shortError, String message) {
this(2, shortError, message);
}
public static UserError UserErrorHigh(String shortError, String message) {
return new UserError(3, shortError, message);
}
public static UserError UserErrorLow(String shortError, String message) {
return new UserError(1, shortError, message);
}
public static UserError UserEventLow(String shortError, String message) {
return new UserError(5, shortError, message);
}
public static UserError UserEventHigh(String shortError, String message) {
return new UserError(6, shortError, message);
}
// TODO move time calc stuff to JOH, wrap it here with our timestamp
public String bestTime() {
final long since = JoH.msSince(timestamp);
if (since < Constants.DAY_IN_MS) {
return JoH.hourMinuteString(timestamp);
} else {
return JoH.dateTimeText(timestamp);
}
}
public static void cleanup() {
new Cleanup().execute(deletable());
}
// used in unit testing
public static void cleanup(long timestamp) {
List<UserError> userErrors = new Select()
.from(UserError.class)
.where("timestamp < ?", timestamp)
.orderBy("timestamp desc")
.execute();
if (userErrors != null) Log.d(TAG, "cleanup UserError size=" + userErrors.size());
new Cleanup().execute(userErrors);
}
public static void cleanupByTimeAndClause(final long timestamp, final String clause) {
new Delete().from(UserError.class)
.where("timestamp < ?", timestamp)
.where(clause)
.execute();
}
public synchronized static void cleanupRaw() {
final long timestamp = JoH.tsl();
cleanupByTimeAndClause(timestamp - Constants.DAY_IN_MS, "severity < 3");
cleanupByTimeAndClause(timestamp - Constants.DAY_IN_MS * 3, "severity = 3");
cleanupByTimeAndClause(timestamp - Constants.DAY_IN_MS * 7, "severity > 3");
Cache.clear();
}
public static List<UserError> all() {
return new Select()
.from(UserError.class)
.orderBy("timestamp desc")
.execute();
}
public static List<UserError> deletable() {
List<UserError> userErrors = new Select()
.from(UserError.class)
.where("severity < ?", 3)
.where("timestamp < ?", (new Date().getTime() - 1000 * 60 * 60 * 24))
.orderBy("timestamp desc")
.execute();
List<UserError> highErrors = new Select()
.from(UserError.class)
.where("severity = ?", 3)
.where("timestamp < ?", (new Date().getTime() - 1000*60*60*24*3))
.orderBy("timestamp desc")
.execute();
List<UserError> events = new Select()
.from(UserError.class)
.where("severity > ?", 3)
.where("timestamp < ?", (new Date().getTime() - 1000*60*60*24*7))
.orderBy("timestamp desc")
.execute();
userErrors.addAll(highErrors);
userErrors.addAll(events);
return userErrors;
}
public static List<UserError> bySeverity(Integer[] levels) {
String levelsString = " ";
for (int level : levels) {
levelsString += level + ",";
}
Log.d("UserError", "severity in ("+levelsString.substring(0,levelsString.length() - 1)+")");
return new Select()
.from(UserError.class)
.where("severity in ("+levelsString.substring(0,levelsString.length() - 1)+")")
.orderBy("timestamp desc")
.limit(10000)//too many data can kill akp
.execute();
}
public static List<UserError> bySeverityNewerThanID(long id, Integer[] levels, int limit) {
String levelsString = " ";
for (int level : levels) {
levelsString += level + ",";
}
Log.d("UserError", "severity in (" + levelsString.substring(0, levelsString.length() - 1) + ")");
return new Select()
.from(UserError.class)
.where("_ID > ?", id)
.where("severity in (" + levelsString.substring(0, levelsString.length() - 1) + ")")
.orderBy("timestamp desc")
.limit(limit)
.execute();
}
public static List<UserError> newerThanID(long id, int limit) {
return new Select()
.from(UserError.class)
.where("_ID > ?", id)
.orderBy("timestamp desc")
.limit(limit)
.execute();
}
public static List<UserError> olderThanID(long id, int limit) {
return new Select()
.from(UserError.class)
.where("_ID < ?", id)
.orderBy("timestamp desc")
.limit(limit)
.execute();
}
public static List<UserError> bySeverityOlderThanID(long id, Integer[] levels, int limit) {
String levelsString = " ";
for (int level : levels) {
levelsString += level + ",";
}
Log.d("UserError", "severity in (" + levelsString.substring(0, levelsString.length() - 1) + ")");
return new Select()
.from(UserError.class)
.where("_ID < ?", id)
.where("severity in (" + levelsString.substring(0, levelsString.length() - 1) + ")")
.orderBy("timestamp desc")
.limit(limit)
.execute();
}
public static UserError getForTimestamp(UserError error) {
try {
return new Select()
.from(UserError.class)
.where("timestamp = ?", error.timestamp)
.where("shortError = ?", error.shortError)
.where("message = ?", error.message)
.executeSingle();
} catch (Exception e) {
Log.e(TAG,"getForTimestamp() Got exception on Select : "+e.toString());
return null;
}
}
private static class Cleanup extends AsyncTask<List<UserError>, Integer, Boolean> {
@Override
protected Boolean doInBackground(List<UserError>... errors) {
try {
for(UserError userError : errors[0]) {
userError.delete();
//userError.save();
}
return true;
} catch(Exception e) {
return false;
}
}
}
public static List<UserError> bySeverity(int level) {
return bySeverity(new Integer[]{level});
}
public static List<UserError> bySeverity(int level, int level2) {
return bySeverity(new Integer[]{ level, level2 });
}
public static List<UserError> bySeverity(int level, int level2, int level3) {
return bySeverity(new Integer[]{ level, level2, level3 });
}
public static class Log {
public static void e(String a, String b){
android.util.Log.e(a, b);
new UserError(a, b);
}
public static void e(String tag, String b, Exception e){
android.util.Log.e(tag, b, e);
new UserError(tag, b + "\n" + e.toString());
}
public static void w(String tag, String b){
android.util.Log.w(tag, b);
UserError.UserErrorLow(tag, b);
}
public static void w(String tag, String b, Exception e){
android.util.Log.w(tag, b, e);
UserError.UserErrorLow(tag, b + "\n" + e.toString());
}
public static void wtf(String tag, String b){
android.util.Log.wtf(tag, b);
UserError.UserErrorHigh(tag, b);
}
public static void wtf(String tag, String b, Exception e){
android.util.Log.wtf(tag, b, e);
UserError.UserErrorHigh(tag, b + "\n" + e.toString());
}
public static void wtf(String tag, Exception e){
android.util.Log.wtf(tag, e);
UserError.UserErrorHigh(tag, e.toString());
}
public static void uel(String tag, String b) {
android.util.Log.i(tag, b);
UserError.UserEventLow(tag, b);
}
public static void ueh(String tag, String b) {
android.util.Log.i(tag, b);
UserError.UserEventHigh(tag, b);
}
public static void d(String tag, String b){
android.util.Log.d(tag, b);
if(ExtraLogTags.shouldLogTag(tag, android.util.Log.DEBUG)) {
UserErrorLow(tag, b);
}
}
public static void v(String tag, String b){
android.util.Log.v(tag, b);
if(ExtraLogTags.shouldLogTag(tag, android.util.Log.VERBOSE)) {
UserErrorLow(tag, b);
}
}
public static void i(String tag, String b){
android.util.Log.i(tag, b);
if(ExtraLogTags.shouldLogTag(tag, android.util.Log.INFO)) {
UserErrorLow(tag, b);
}
}
static ExtraLogTags extraLogTags = new ExtraLogTags();
}
public static class ExtraLogTags {
static Hashtable <String, Integer> extraTags;
ExtraLogTags () {
extraTags = new Hashtable <String, Integer>();
String extraLogs = Pref.getStringDefaultBlank("extra_tags_for_logging");
readPreference(extraLogs);
}
/*
* This function reads a string representing tags that the user wants to log
* Format of string is tag1:level1,tag2,level2
* Example of string is Alerts:i,BG:W
*
*/
public static void readPreference(String extraLogs) {
extraLogs = extraLogs.trim();
if (extraLogs.length() > 0) UserErrorLow(TAG, "called with string " + extraLogs);
extraTags.clear();
// allow splitting to work with a single entry and no delimiter zzz
if ((extraLogs.length() > 1) && (!extraLogs.contains(","))) {
extraLogs += ",";
}
String[] tags = extraLogs.split(",");
if (tags.length == 0) {
return;
}
// go over all tags and parse them
for(String tag : tags) {
if (tag.length() > 0) parseTag(tag);
}
}
static void parseTag(String tag) {
// Format is tag:level for example Alerts:i
String[] tagAndLevel = tag.trim().split(":");
if(tagAndLevel.length != 2) {
Log.e(TAG, "Failed to parse " + tag);
return;
}
String level = tagAndLevel[1];
String tagName = tagAndLevel[0].toLowerCase();
if (level.compareTo("d") == 0) {
extraTags.put(tagName, android.util.Log.DEBUG);
UserErrorLow(TAG, "Adding tag with DEBUG " + tagAndLevel[0] );
return;
}
if (level.compareTo("v") == 0) {
extraTags.put(tagName, android.util.Log.VERBOSE);
UserErrorLow(TAG,"Adding tag with VERBOSE " + tagAndLevel[0] );
return;
}
if (level.compareTo("i") == 0) {
extraTags.put(tagName, android.util.Log.INFO);
UserErrorLow(TAG, "Adding tag with info " + tagAndLevel[0] );
return;
}
Log.e(TAG, "Unknown level for tag " + tag + " please use d v or i");
}
public static boolean shouldLogTag(final String tag, final int level) {
final Integer levelForTag = extraTags.get(tag != null ? tag.toLowerCase() : "");
return levelForTag != null && level >= levelForTag;
}
}
}