@@ -59,6 +59,7 @@ @implementation LogDatasource
59
59
@synthesize delegate = _delegate;
60
60
@synthesize deviceId = _deviceId;
61
61
@synthesize isLogging;
62
+ @synthesize skipPidLookup;
62
63
63
64
@synthesize previousString = _previousString;
64
65
@synthesize startTime = _startTime;
@@ -79,6 +80,7 @@ @implementation LogDatasource
79
80
80
81
- (id )init {
81
82
if (self = [super init ]) {
83
+ self.skipPidLookup = false ;
82
84
self.pidMap = [NSMutableDictionary dictionary ];
83
85
self.logData = [NSMutableArray arrayWithCapacity: 0 ];
84
86
self.text = [NSMutableString stringWithCapacity: 0 ];
@@ -134,6 +136,7 @@ - (void) startLogger {
134
136
}
135
137
136
138
- (void ) stopLogger {
139
+ NSLog (@" Stop logging called." );
137
140
isLogging = NO ;
138
141
[self .thread cancel ];
139
142
self.thread = nil ;
@@ -157,6 +160,9 @@ - (void) clearLog {
157
160
#pragma mark -
158
161
159
162
- (void ) loadPID {
163
+ if (self.skipPidLookup ) {
164
+ return ;
165
+ }
160
166
NSArray *arguments = nil ;
161
167
arguments = @[@" shell" , @" ps" ];
162
168
@@ -201,16 +207,21 @@ - (void) parsePID: (NSString*) pidInfo {
201
207
NSArray * lines = [pidInfo componentsSeparatedByCharactersInSet: [NSCharacterSet newlineCharacterSet ]];
202
208
203
209
for (NSString * line in lines) {
204
- if ([line hasPrefix: @" -" ]) {
210
+ if ([line length ] == 0 ) {
211
+ // skip blank lines
212
+ continue ;
213
+ } else if ([line hasPrefix: @" -" ]) {
205
214
continue ;
206
215
} else if ([line hasPrefix: @" error:" ]) {
207
216
NSLog (@" parsePID: %@ " , line);
208
217
if ([line isEqualToString: MULTIPLE_DEVICE_MSG]) {
218
+ NSLog (@" Multiple devices. Abort. (1)" );
209
219
isLogging = NO ;
210
220
[self onMultipleDevicesConnected ];
211
221
[self stopLogger ];
212
222
return ;
213
223
} else if ([line isEqualToString: DEVICE_NOT_FOUND_MSG]) {
224
+ NSLog (@" Device not found. Abort. (1)" );
214
225
isLogging = NO ;
215
226
[self onDeviceNotFound ];
216
227
[self stopLogger ];
@@ -253,13 +264,19 @@ - (void) parsePID: (NSString*) pidInfo {
253
264
#pragma mark Log Loader
254
265
#pragma mark -
255
266
267
+
256
268
- (void )readLog : (id )param
257
269
{
258
270
isLogging = YES ;
259
271
[self performSelectorOnMainThread: @selector (onLoggerStarted ) withObject: nil waitUntilDone: NO ];
260
272
261
273
NSArray *arguments = nil ;
262
- if (LOG_FORMAT == 1 ) {
274
+ if (param != nil ) {
275
+ // assume caller is passing the arguments we need
276
+ self.skipPidLookup = YES ;
277
+ arguments = param;
278
+
279
+ } else if (LOG_FORMAT == 1 ) {
263
280
arguments = @[@" logcat" , @" -v" , @" long" ];
264
281
265
282
} else if (LOG_FORMAT == 2 ) {
@@ -272,7 +289,19 @@ - (void)readLog:(id)param
272
289
273
290
@try {
274
291
275
- NSTask *task = [AdbTaskHelper adbTask: [self argumentsForDevice: arguments]];
292
+ NSTask *task = nil ;
293
+ if (param != nil ) {
294
+ task = [[NSTask alloc ] init ];
295
+
296
+ NSString *catPath = @" /bin/cat" ;
297
+
298
+ [task setLaunchPath: catPath];
299
+ [task setArguments: arguments];
300
+
301
+ } else {
302
+
303
+ task = [AdbTaskHelper adbTask: [self argumentsForDevice: arguments]];
304
+ }
276
305
277
306
NSPipe *pipe ;
278
307
pipe = [NSPipe pipe ];
@@ -284,20 +313,28 @@ - (void)readLog:(id)param
284
313
file = [pipe fileHandleForReading ];
285
314
286
315
[task launch ];
316
+ // NSLog(@"Task isRunning: %d", task.isRunning);
287
317
288
- while (isLogging && [task isRunning ]) {
289
- NSData *data = nil ;
290
- while (data == nil ) {
318
+ NSData *data = nil ;
319
+ while (isLogging && (((data = [file availableData ]) != nil ) || [task isRunning ])) {
320
+ // NSLog(@"Task: %d, data=%@", [task isRunning], data);
321
+ while (data == nil || [data length ] == 0 ) {
291
322
data = [file availableData ];
323
+ if ((data == nil || [data length ] == 0 ) && ![task isRunning ]) {
324
+ isLogging = NO ;
325
+ break ;
326
+ }
292
327
}
293
328
294
-
295
329
if (data != nil ) {
296
330
297
331
NSString *string;
298
332
string = [[NSString alloc ] initWithData: data encoding: NSUTF8StringEncoding];
299
333
// NSLog(@"Data: %@", string);
300
- if (LOG_FORMAT == 1 ) {
334
+ if (param != nil ) {
335
+ NSLog (@" Parse: %@ " , string);
336
+ [self appendThreadtimeLog: string];
337
+ } else if (LOG_FORMAT == 1 ) {
301
338
[self performSelectorOnMainThread: @selector (appendLongLog: ) withObject: string waitUntilDone: YES ];
302
339
303
340
} else if (LOG_FORMAT == 2 ) {
@@ -311,6 +348,7 @@ - (void)readLog:(id)param
311
348
} else {
312
349
NSLog (@" Data was nil..." );
313
350
}
351
+ data = nil ;
314
352
}
315
353
316
354
[task terminate ];
@@ -322,6 +360,7 @@ - (void)readLog:(id)param
322
360
NSBeep ();
323
361
}
324
362
363
+ NSLog (@" Exited readlog loop." );
325
364
isLogging = NO ;
326
365
[self .pidMap removeAllObjects ];
327
366
@@ -331,6 +370,7 @@ - (void)readLog:(id)param
331
370
332
371
[self stopLogger ];
333
372
NSLog (@" ADB Exited." );
373
+ self.skipPidLookup = NO ;
334
374
}
335
375
336
376
- (void ) logData : (NSData *) data {
@@ -549,8 +589,19 @@ - (void) appendThreadtimeLog: (NSString*) paramString {
549
589
}
550
590
551
591
- (void ) parseThreadTimeLine : (NSString *) line {
592
+
593
+
594
+ if ([line hasPrefix: @" -appPID" ] || [line hasPrefix: @" - appPID" ]) {
595
+ NSArray *strings = [line componentsSeparatedByString: @" ," ];
596
+ if ([strings count ] == 3 ) {
597
+ NSString * pid = [strings[1 ] stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet ]];
598
+ NSString * app = [strings[2 ] stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet ]];
599
+ NSLog (@" Adding PID \" %@ \" for app \" %@ \" " , pid, app);
600
+ [self .pidMap setValue: app forKey: pid];
601
+ }
552
602
553
- if ([line hasPrefix: @" -" ]) {
603
+ return ;
604
+ } else if ([line hasPrefix: @" -" ]) {
554
605
return ;
555
606
} else if ([line hasPrefix: @" error:" ]) {
556
607
NSLog (@" parseThreadTimeLine: \" %@ \" , %@ " , line, self);
@@ -561,6 +612,7 @@ - (void) parseThreadTimeLine: (NSString*) line {
561
612
// [self onMultipleDevicesConnected];
562
613
return ;
563
614
} else if ([line hasPrefix: DEVICE_NOT_FOUND_MSG]) {
615
+ NSLog (@" Device Not Found. Abort Logcat." );
564
616
isLogging = NO ;
565
617
[self performSelectorOnMainThread: @selector (onMultipleDevicesConnected ) withObject: nil waitUntilDone: YES ];
566
618
// [self onMultipleDevicesConnected];
@@ -671,15 +723,27 @@ - (void)appendLongLog:(NSString*)paramString
671
723
}
672
724
673
725
for (NSString * line in lines) {
674
- if ([line hasPrefix: @" -" ]) {
726
+
727
+ if ([line hasPrefix: @" -appPID" ] || [line hasPrefix: @" - appPID" ]) {
728
+ NSArray *strings = [line componentsSeparatedByString: @" ," ];
729
+ if ([strings count ] == 3 ) {
730
+ [self .pidMap setValue: strings[2 ] forKey: strings[1 ]];
731
+ }
732
+
733
+ continue ;
734
+ } else if ([line hasPrefix: @" -" ]) {
735
+
736
+
675
737
continue ;
676
738
} else if ([line hasPrefix: @" error:" ]) {
677
739
NSLog (@" appendLongLog: %@ " , line);
678
740
if ([line isEqualToString: MULTIPLE_DEVICE_MSG]) {
741
+ NSLog (@" Mulitple devices. Abort." );
679
742
isLogging = NO ;
680
743
[self onMultipleDevicesConnected ];
681
744
return ;
682
745
} else if ([line isEqualToString: DEVICE_NOT_FOUND_MSG]) {
746
+ NSLog (@" Device not found. Abort." );
683
747
isLogging = NO ;
684
748
[self onMultipleDevicesConnected ];
685
749
return ;
@@ -700,7 +764,7 @@ - (void)appendLongLog:(NSString*)paramString
700
764
self.tid = [line substringWithRange: [match rangeAtIndex: 3 ]];
701
765
self.app = (self.pidMap )[self .pid];
702
766
if (self.app == nil ) {
703
- NSLog (@" %@ not found in pid map." , self.pid );
767
+ NSLog (@" %@ not found in pid map. (1) " , self.pid );
704
768
[self loadPID ];
705
769
self.app = (self.pidMap )[self .pid];
706
770
if (self.app == nil ) {
@@ -789,10 +853,13 @@ - (void) logMessage: (NSString*) message {
789
853
}
790
854
791
855
- (NSString *) appNameForPid : (NSString *) pidVal {
856
+
792
857
NSString * appVal = (self.pidMap )[pidVal];
793
858
if (appVal == nil ) {
794
- NSLog (@" %@ not found in pid map." , pidVal);
795
- [self loadPID ];
859
+ NSLog (@" %@ not found in pid map. (2)" , pidVal);
860
+ if (!self.skipPidLookup ) {
861
+ [self loadPID ];
862
+ }
796
863
appVal = (self.pidMap )[pidVal];
797
864
if (appVal == nil ) {
798
865
// This is normal during startup because there can be log
0 commit comments