Skip to content

Commit b8a1fac

Browse files
author
Chris Wilson
committed
Quick hack to support importing emailed logs from clients.
1 parent 114df6e commit b8a1fac

File tree

4 files changed

+184
-38
lines changed

4 files changed

+184
-38
lines changed

LogCat/LogCatAppDelegate.m

+44
Original file line numberDiff line numberDiff line change
@@ -1330,6 +1330,50 @@ - (IBAction)applyPredicate:(id)sender {
13301330
[self.logDataTable reloadData];
13311331
}
13321332

1333+
- (IBAction)importTextLog:(id)sender {
1334+
NSOpenPanel* openDlg = [NSOpenPanel openPanel];
1335+
[openDlg setCanChooseFiles:YES];
1336+
[openDlg setAllowsMultipleSelection:NO];
1337+
[openDlg setCanChooseDirectories:NO];
1338+
1339+
if ( [openDlg runModal] == NSOKButton )
1340+
{
1341+
[self.logDatasource stopLogger];
1342+
1343+
NSArray* urls = [openDlg URLs];
1344+
if (urls != nil && [urls count] > 0) {
1345+
if (self.logDatasource != nil && [self.logDatasource isLogging]) {
1346+
[self.logDatasource stopLogger];
1347+
self.logDatasource = nil;
1348+
}
1349+
1350+
NSURL* url = urls[0];
1351+
NSLog(@"Open url: %@", url);
1352+
[openDlg close];
1353+
1354+
NSArray *arguments = nil;
1355+
arguments = @[[url path]];
1356+
NSLog(@"Will get log from: %@", arguments);
1357+
[self clearLog:nil];
1358+
[self.logDatasource readLog:arguments];
1359+
1360+
// NSDictionary* filtersToImport = [NSDictionary dictionaryWithContentsOfURL:url];
1361+
//
1362+
// NSArray* keys = [filtersToImport keysSortedByValueUsingSelector:@selector(caseInsensitiveCompare:)];
1363+
// for (NSString* key in keys) {
1364+
// NSString* filter = filtersToImport[key];
1365+
// // TODO: figure out what to do for filters that already exist. For now just overwrite
1366+
// filters[key] = [NSPredicate predicateWithFormat:filter];
1367+
// }
1368+
//
1369+
// [self saveFilters];
1370+
// [self.filterListTable reloadData];
1371+
}
1372+
}
1373+
1374+
1375+
}
1376+
13331377
- (IBAction)importFilters:(id)sender {
13341378
NSOpenPanel* openDlg = [NSOpenPanel openPanel];
13351379
[openDlg setCanChooseFiles:YES];

LogCat/LogDatasource.h

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
@property (strong) NSString* deviceId;
3636
@property (atomic) BOOL isLogging;
37+
@property (atomic) BOOL skipPidLookup;
3738

3839
- (void) startLogger;
3940
- (void) stopLogger;
@@ -42,4 +43,6 @@
4243
- (void) logMessage: (NSString*) message;
4344
- (NSArray*) eventsForPredicate: (NSPredicate*) predicate;
4445

46+
- (void)readLog:(id)param;
47+
4548
@end

LogCat/LogDatasource.m

+80-13
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)