forked from archduke31/ADBLogger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogSource.java
More file actions
580 lines (503 loc) · 20 KB
/
Copy pathLogSource.java
File metadata and controls
580 lines (503 loc) · 20 KB
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package feiw;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
public class LogSource {
public static final int stIdle = 0;
public static final int stConnecting = 1;
public static final int stConnected = 2;
public static abstract class LogFilter {
public static final String OP_EQUALS = "=";
public static final String OP_CONTAINS = "contains";
public static final String OP_GREATERTHAN = ">";
public static final String OP_LESSTHEN = "<";
public static final String FIELD_PRIORITY = "priority";
public static final String FIELD_TAG = "tag";
public static final String FIELD_TIME = "time";
public static final String FIELD_CONTENT = "message";
public static final String FIELD_PID = "PID";
public static final String FIELD_TID = "TID";
public static final String FILTER_OP_AND = "and";
public static final String FILTER_OP_OR = "or";
public abstract boolean filterLog(final LogParser parser, final String item);
String mName;
LogFilter(String n) {
mName = n;
}
void setName(String n) {
mName = n;
}
String getName() {
return mName;
}
@Override
public String toString() {
return mName;
}
@SuppressWarnings("serial")
static class InvalidLogFilterStringException extends Exception {
public InvalidLogFilterStringException(String s) {
super(s);
}
}
static LogFilter fromString(String s) throws InvalidLogFilterStringException {
String fs[] = s.split(FILTER_OP_AND);
if (fs != null && fs.length > 0) {
System.out.println(fs[0]);
}
int idx = s.indexOf(FILTER_OP_AND);
if (idx > 0) {
return fromString(s.substring(0, idx)).and(fromString(s.substring(idx + FILTER_OP_AND.length())));
}
idx = s.indexOf(FILTER_OP_OR);
if (idx > 0) {
return fromString(s.substring(0, idx)).or(fromString(s.substring(idx + FILTER_OP_OR.length())));
}
String[] txt = s.split(" ");
if (txt == null || txt.length != 3 || txt[0] == null || txt[1] == null || txt[2] == null) {
throw new InvalidLogFilterStringException("invalid log filter string: " + s);
}
Object o = txt[2];
if (txt[0].equals(FIELD_PRIORITY)) {
o = Integer.parseInt(txt[2]);
}
return newLogFilter(txt[0], txt[1], o);
}
public LogFilter and(final LogFilter f) {
return new LogFilter(getName() + FILTER_OP_AND + f.getName()) {
@Override
public boolean filterLog(final LogParser parser, final String item) {
return LogFilter.this.filterLog(parser, item) && f.filterLog(parser, item);
}
};
}
public LogFilter or(final LogFilter f) {
return new LogFilter(getName() + FILTER_OP_OR + f.getName()) {
@Override
public boolean filterLog(final LogParser parser, final String item) {
return LogFilter.this.filterLog(parser, item) || f.filterLog(parser, item);
}
};
}
public LogFilter not() {
return new LogFilter("NOT(" + getName() + ")") {
@Override
public boolean filterLog(LogParser parser, String item) {
return !LogFilter.this.filterLog(parser, item);
}
};
}
public static LogFilter newLogFilter(String field, String op, final Object dstObj) {
if (FIELD_PRIORITY.equals(field)) {
if (OP_EQUALS.equals(op)) {
return new LogFilter(field + " " + op + " " + dstObj) {
@Override
public boolean filterLog(final LogParser parser, final String item) {
return parser.parsePriority(item) == ((Integer) dstObj).intValue();
}
};
} else if (OP_GREATERTHAN.equals(op)) {
return new LogFilter(field + " " + op + " " + dstObj) {
@Override
public boolean filterLog(final LogParser parser, final String item) {
return parser.parsePriority(item) > ((Integer) dstObj).intValue();
}
};
} else if (OP_LESSTHEN.equals(op)) {
return new LogFilter(field + " " + op + " " + dstObj) {
@Override
public boolean filterLog(final LogParser parser, final String item) {
return parser.parsePriority(item) < ((Integer) dstObj).intValue();
}
};
}
} else if (FIELD_TIME.equals(field)) {
if (OP_EQUALS.equals(op)) {
} else if (OP_CONTAINS.equals(op)) {
} else if (OP_GREATERTHAN.equals(op)) {
} else if (OP_LESSTHEN.equals(op)) {
}
} else if (FIELD_CONTENT.equals(field)) {
if (OP_CONTAINS.equals(op)) {
return new LogFilter(field + " " + op + " " + dstObj) {
private final StringPattern mPat = new StringPattern((String) dstObj, false);
@Override
public boolean filterLog(final LogParser parser, final String item) {
return mPat.isContainedBy(parser.parseMessage(item)) >= 0;
}
};
}
} else if (FIELD_TAG.equals(field)) {
if (OP_EQUALS.equals(op)) {
return new LogFilter(field + " " + op + " " + dstObj) {
@Override
public boolean filterLog(final LogParser parser, final String item) {
return dstObj.equals(parser.parseTag(item));
}
};
} else if (OP_CONTAINS.equals(op)) {
return new LogFilter(field + " " + op + " " + dstObj) {
@Override
public boolean filterLog(final LogParser parser, final String item) {
String tag = parser.parseTag(item);
return (tag != null) && tag.contains((String) dstObj);
}
};
}
} else if (FIELD_PID.equals(field)) {
if (OP_EQUALS.equals(op)) {
return new LogFilter(field + " " + op + " " + dstObj) {
@Override
public boolean filterLog(final LogParser parser, final String item) {
return dstObj.equals(parser.parsePID(item));
}
};
}
} else if (FIELD_TID.equals(field)) {
if (OP_EQUALS.equals(op)) {
return new LogFilter(field + " " + op + " " + dstObj) {
@Override
public boolean filterLog(final LogParser parser, final String item) {
return dstObj.equals(parser.parseTID(item));
}
};
}
}
return null;
}
}
public interface LogListener {
public void onLogChanged();
public void onSearchResult();
}
public interface StatusListener {
public void onStatusChanged(int oldStatus, int newStatus);
}
protected int mStatus = stIdle;
public synchronized int getStatus() {
return mStatus;
}
public synchronized void addStatusListener(StatusListener slis) {
if (slis != null) {
mStatusListeners.add(slis);
}
}
public synchronized void removeStatusListener(StatusListener slis) {
mStatusListeners.remove(slis);
}
protected synchronized void setStatus(int st) {
if (st != mStatus) {
for (StatusListener li : mStatusListeners) {
li.onStatusChanged(mStatus, st);
}
mStatus = st;
}
}
protected long mNotifyTimeSpan = SystemConfigs.instance().LIVE_LOG_NOTIFYTIME;
protected int fetchLogs(InputStream is) throws IOException {
BufferedReader din = new BufferedReader(new InputStreamReader(is));
int line = 0;
String str = din.readLine();
long start_time = System.currentTimeMillis();
while (str != null) {
str = str.trim();
if (!str.isEmpty()) {
line++;
long curtime = System.currentTimeMillis();
if (is.available() == 0 || curtime - start_time > mNotifyTimeSpan) {
addLogItem(str, true);
start_time = curtime;
} else {
addLogItem(str, false);
}
}
str = din.readLine();
}
return line;
// System.out.println(" log lines = " + line);
}
protected int fetchLogs(InputStream is, LogParser parser) throws IOException {
BufferedReader din = new BufferedReader(new InputStreamReader(is));
int line = 0;
String str = parser.readOneLog(din);
long start_time = System.currentTimeMillis();
while (str != null) {
line++;
long curtime = System.currentTimeMillis();
if (is.available() == 0 || curtime - start_time > mNotifyTimeSpan) {
addLogItem(str, true);
start_time = curtime;
} else {
addLogItem(str, false);
}
str = parser.readOneLog(din);
}
return line;
// System.out.println(" log lines = " + line);
}
/*
* public static final class LogItem { private final String[] texts; private
* Date mTime; static final SimpleDateFormat mDfmt = new SimpleDateFormat(
* "MMM dd HH:mm:ss.SSS"); static final SimpleDateFormat mDfmts = new
* SimpleDateFormat("MMM dd HH:mm:ss"); static private SimpleDateFormat
* mParser = mDfmt; private int mLevel = 7;
*
* Date getTime() { return mTime; }
*
* public String getText() { return texts[4]; } public String getText(int i)
* { if (i >= 0 && i < texts.length) { return texts[i]; } return null; }
*
* static final String[] seperator = { " ", " ", " ", " " }; public
* LogItem(final String str) { final String [] ret = new String[5]; texts =
* ret; int idx = 0, nextidx; final int slen = str.length(); for (int i = 0;
* i < 4; i++) { nextidx = str.indexOf(seperator[i], idx); if (nextidx <= 0)
* { ret[0] = ret[1] = ret[2] = ret[3] = null; ret[4] = str; return; }
* ret[i] = str.substring(idx, nextidx); idx = nextidx +
* seperator[i].length(); while (slen > idx && str.charAt(idx) == ' ')
* idx++; } ret[4] = str.substring(idx); if (!ret[1].isEmpty()) { mLevel =
* ret[1].charAt(0) - '0'; if (mLevel < 0 || mLevel > 7) { mLevel = 6; } }
*
* try { mTime = mParser.parse(ret[0]); } catch (ParseException e) { try {
* mParser = mDfmts; mTime = mParser.parse(ret[0]); } catch (ParseException
* e1) { mTime = null; } } }
*
* public LogItem(String[] txt) { texts = txt; }
*
* public int getTextCount() { if (texts != null) { return texts.length; }
* return 0; }
*
* public int getLevel() { return mLevel; } }
*/
public static class LogView {
private List<String> mFilteredItems;
private LogListener mListener = null;
private LogFilter mFilter = null;
private StringPattern mSearchPattern = null;
private LogParser mParser;
private int mRollLines;
private AtomicBoolean mLogChanged = new AtomicBoolean(false);
private AtomicBoolean mPaused = new AtomicBoolean(false);
public LogParser getLogParser() {
return mParser;
}
public int getRollLines() {
return mRollLines;
}
public final StringPattern getSearchPattern() {
return mSearchPattern;
}
public boolean isPaused() {
return mPaused.get();
}
public void resume() {
mPaused.set(false);
}
public void pause() {
mPaused.set(true);
}
private void notifyListener() {
if (mListener != null && !mPaused.get() && mLogChanged.get()) {
mListener.onLogChanged();
mLogChanged.set(false);
}
}
public void writeLogs(OutputStream os) throws IOException {
BufferedWriter dw = new BufferedWriter(new OutputStreamWriter(os));
synchronized (mFilteredItems) {
final int s = mFilteredItems.size();
int i = 0;
for (i = 0; i < s - 1; i++) {
dw.write(mFilteredItems.get(i));
dw.write('\n');
}
dw.write(mFilteredItems.get(i));
}
dw.flush();
}
private LogView(LogListener listener, LogFilter filter, LogParser parser, List<String> source, int rolllines) {
mListener = listener;
mFilter = filter;
mParser = parser;
mRollLines = rolllines;
mFilteredItems = Collections.synchronizedList(new ArrayList<String>());
if (source != null) {
synchronized (source) {
for (String it : source) {
if (filter.filterLog(parser, it)) {
mFilteredItems.add(it);
}
}
}
}
if (mFilteredItems.size() > 0) {
mLogChanged.set(true);
notifyListener();
}
}
public boolean isSearchResults(final String logmsg) {
if (mSearchPattern != null) {
return mSearchPattern.isContainedBy(logmsg) >= 0;
}
return false;
}
public void add(final String item, boolean notifylistner) {
if (item.contains("fei:clearlogs")) {
mSearchResults = -1;
mSearchPattern = null;
mFilteredItems.clear();
mLogChanged.set(true);
return;
}
if (mFilter == null || mFilter.filterLog(mParser, item)) {
synchronized (mFilteredItems) {
if (isSearchResults(item)) {
mSearchResults++;
}
if (mRollLines > 0 && mFilteredItems.size() >= mRollLines) {
mFilteredItems.remove(0);
}
mFilteredItems.add(item);
}
mLogChanged.set(true);
if (notifylistner) {
notifyListener();
}
}
}
public void clear() {
if (mFilteredItems.size() > 0) {
mSearchResults = -1;
mSearchPattern = null;
mFilteredItems.clear();
mLogChanged.set(true);
notifyListener();
}
}
public int size() {
return mFilteredItems.size();
}
public final String getLog(int index) {
synchronized (mFilteredItems) {
if (index >= 0 && index < mFilteredItems.size()) {
return mFilteredItems.get(index);
}
}
return null;
}
private int mSearchResults = -1;
public int getSearchResults() {
return mSearchResults;
}
public int getPrevSearchResult(int start) {
if (mSearchResults <= 0) {
return -1;
}
synchronized (mFilteredItems) {
if (start < 0 || start >= mFilteredItems.size()) {
start = mFilteredItems.size() - 1;
}
for (int i = start; i >= 0; i--) {
if (isSearchResults(mFilteredItems.get(i))) {
return i;
}
}
for (int i = mFilteredItems.size() - 1; i > start; i--) {
if (isSearchResults(mFilteredItems.get(i))) {
return i;
}
}
}
return -1;
}
public int getNextSearchResult(int start) {
if (mSearchResults <= 0) {
return -1;
}
synchronized (mFilteredItems) {
for (int i = start; i < mFilteredItems.size(); i++) {
if (isSearchResults(mFilteredItems.get(i))) {
return i;
}
}
for (int i = 0; i < start; i++) {
if (isSearchResults(mFilteredItems.get(i))) {
return i;
}
}
}
return -1;
}
public void search(String txt, boolean caseSenstive) {
if (mFilteredItems == null || mFilteredItems.size() <= 0) {
return;
}
mSearchPattern = new StringPattern(txt, caseSenstive);
int results = 0;
synchronized (mFilteredItems) {
for (String it : mFilteredItems) {
if (isSearchResults(it)) {
results++;
}
}
}
mSearchResults = results;
mLogChanged.set(true);
mListener.onSearchResult();
}
}
public synchronized void removeLogView(LogView v) {
mViews.remove(v);
if (mViews.size() == 0) {
disconnect();
}
}
int mRollLines = -1;
public synchronized LogView newLogView(LogListener listener, LogFilter filter, LogParser parser,
LogView parentView) {
LogView v = new LogView(listener, filter, parser, parentView == null ? null : parentView.mFilteredItems,
mRollLines);
mViews.add(v);
return v;
}
public synchronized void addLogItem(final String item, boolean notifylistener) {
for (LogView v : mViews) {
v.add(item, notifylistener);
}
}
public synchronized void notifyViews() {
for (LogView v : mViews) {
v.notifyListener();
}
}
public void disconnect() {
}
// List<LogItem> mItems = (List<LogItem>) Collections.synchronizedList(new
// ArrayList<LogItem>(
// 10000));
List<LogView> mViews = new ArrayList<LogView>(5);
List<StatusListener> mStatusListeners = Collections
.synchronizedList(new ArrayList<StatusListener>(5));
}