@@ -59,6 +59,7 @@ @implementation LogDatasource
5959@synthesize delegate = _delegate;
6060@synthesize deviceId = _deviceId;
6161@synthesize isLogging;
62+ @synthesize skipPidLookup;
6263
6364@synthesize previousString = _previousString;
6465@synthesize startTime = _startTime;
@@ -79,6 +80,7 @@ @implementation LogDatasource
7980
8081- (id )init {
8182 if (self = [super init ]) {
83+ self.skipPidLookup = false ;
8284 self.pidMap = [NSMutableDictionary dictionary ];
8385 self.logData = [NSMutableArray arrayWithCapacity: 0 ];
8486 self.text = [NSMutableString stringWithCapacity: 0 ];
@@ -134,6 +136,7 @@ - (void) startLogger {
134136}
135137
136138- (void ) stopLogger {
139+ NSLog (@" Stop logging called." );
137140 isLogging = NO ;
138141 [self .thread cancel ];
139142 self.thread = nil ;
@@ -157,6 +160,9 @@ - (void) clearLog {
157160#pragma mark -
158161
159162- (void ) loadPID {
163+ if (self.skipPidLookup ) {
164+ return ;
165+ }
160166 NSArray *arguments = nil ;
161167 arguments = @[@" shell" , @" ps" ];
162168
@@ -201,16 +207,21 @@ - (void) parsePID: (NSString*) pidInfo {
201207 NSArray * lines = [pidInfo componentsSeparatedByCharactersInSet: [NSCharacterSet newlineCharacterSet ]];
202208
203209 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: @" -" ]) {
205214 continue ;
206215 } else if ([line hasPrefix: @" error:" ]) {
207216 NSLog (@" parsePID: %@ " , line);
208217 if ([line isEqualToString: MULTIPLE_DEVICE_MSG]) {
218+ NSLog (@" Multiple devices. Abort. (1)" );
209219 isLogging = NO ;
210220 [self onMultipleDevicesConnected ];
211221 [self stopLogger ];
212222 return ;
213223 } else if ([line isEqualToString: DEVICE_NOT_FOUND_MSG]) {
224+ NSLog (@" Device not found. Abort. (1)" );
214225 isLogging = NO ;
215226 [self onDeviceNotFound ];
216227 [self stopLogger ];
@@ -253,13 +264,19 @@ - (void) parsePID: (NSString*) pidInfo {
253264#pragma mark Log Loader
254265#pragma mark -
255266
267+
256268- (void )readLog : (id )param
257269{
258270 isLogging = YES ;
259271 [self performSelectorOnMainThread: @selector (onLoggerStarted ) withObject: nil waitUntilDone: NO ];
260272
261273 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 ) {
263280 arguments = @[@" logcat" , @" -v" , @" long" ];
264281
265282 } else if (LOG_FORMAT == 2 ) {
@@ -272,7 +289,19 @@ - (void)readLog:(id)param
272289
273290 @try {
274291
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+ }
276305
277306 NSPipe *pipe;
278307 pipe = [NSPipe pipe ];
@@ -284,20 +313,28 @@ - (void)readLog:(id)param
284313 file = [pipe fileHandleForReading ];
285314
286315 [task launch ];
316+ // NSLog(@"Task isRunning: %d", task.isRunning);
287317
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 ) {
291322 data = [file availableData ];
323+ if ((data == nil || [data length ] == 0 ) && ![task isRunning ]) {
324+ isLogging = NO ;
325+ break ;
326+ }
292327 }
293328
294-
295329 if (data != nil ) {
296330
297331 NSString *string;
298332 string = [[NSString alloc ] initWithData: data encoding: NSUTF8StringEncoding];
299333 // 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 ) {
301338 [self performSelectorOnMainThread: @selector (appendLongLog: ) withObject: string waitUntilDone: YES ];
302339
303340 } else if (LOG_FORMAT == 2 ) {
@@ -311,6 +348,7 @@ - (void)readLog:(id)param
311348 } else {
312349 NSLog (@" Data was nil..." );
313350 }
351+ data = nil ;
314352 }
315353
316354 [task terminate ];
@@ -322,6 +360,7 @@ - (void)readLog:(id)param
322360 NSBeep ();
323361 }
324362
363+ NSLog (@" Exited readlog loop." );
325364 isLogging = NO ;
326365 [self .pidMap removeAllObjects ];
327366
@@ -331,6 +370,7 @@ - (void)readLog:(id)param
331370
332371 [self stopLogger ];
333372 NSLog (@" ADB Exited." );
373+ self.skipPidLookup = NO ;
334374}
335375
336376- (void ) logData : (NSData *) data {
@@ -549,8 +589,19 @@ - (void) appendThreadtimeLog: (NSString*) paramString {
549589}
550590
551591- (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+ }
552602
553- if ([line hasPrefix: @" -" ]) {
603+ return ;
604+ } else if ([line hasPrefix: @" -" ]) {
554605 return ;
555606 } else if ([line hasPrefix: @" error:" ]) {
556607 NSLog (@" parseThreadTimeLine: \" %@ \" , %@ " , line, self);
@@ -561,6 +612,7 @@ - (void) parseThreadTimeLine: (NSString*) line {
561612// [self onMultipleDevicesConnected];
562613 return ;
563614 } else if ([line hasPrefix: DEVICE_NOT_FOUND_MSG]) {
615+ NSLog (@" Device Not Found. Abort Logcat." );
564616 isLogging = NO ;
565617 [self performSelectorOnMainThread: @selector (onMultipleDevicesConnected ) withObject: nil waitUntilDone: YES ];
566618// [self onMultipleDevicesConnected];
@@ -671,15 +723,27 @@ - (void)appendLongLog:(NSString*)paramString
671723 }
672724
673725 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+
675737 continue ;
676738 } else if ([line hasPrefix: @" error:" ]) {
677739 NSLog (@" appendLongLog: %@ " , line);
678740 if ([line isEqualToString: MULTIPLE_DEVICE_MSG]) {
741+ NSLog (@" Mulitple devices. Abort." );
679742 isLogging = NO ;
680743 [self onMultipleDevicesConnected ];
681744 return ;
682745 } else if ([line isEqualToString: DEVICE_NOT_FOUND_MSG]) {
746+ NSLog (@" Device not found. Abort." );
683747 isLogging = NO ;
684748 [self onMultipleDevicesConnected ];
685749 return ;
@@ -700,7 +764,7 @@ - (void)appendLongLog:(NSString*)paramString
700764 self.tid = [line substringWithRange: [match rangeAtIndex: 3 ]];
701765 self.app = (self.pidMap )[self .pid];
702766 if (self.app == nil ) {
703- NSLog (@" %@ not found in pid map." , self.pid );
767+ NSLog (@" %@ not found in pid map. (1) " , self.pid );
704768 [self loadPID ];
705769 self.app = (self.pidMap )[self .pid];
706770 if (self.app == nil ) {
@@ -789,10 +853,13 @@ - (void) logMessage: (NSString*) message {
789853}
790854
791855- (NSString *) appNameForPid : (NSString *) pidVal {
856+
792857 NSString * appVal = (self.pidMap )[pidVal];
793858 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+ }
796863 appVal = (self.pidMap )[pidVal];
797864 if (appVal == nil ) {
798865 // This is normal during startup because there can be log
0 commit comments