Skip to content

Commit

Permalink
Quick hack to support importing emailed logs from clients.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Wilson committed Sep 17, 2013
1 parent 114df6e commit b8a1fac
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 38 deletions.
44 changes: 44 additions & 0 deletions LogCat/LogCatAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -1330,6 +1330,50 @@ - (IBAction)applyPredicate:(id)sender {
[self.logDataTable reloadData];
}

- (IBAction)importTextLog:(id)sender {
NSOpenPanel* openDlg = [NSOpenPanel openPanel];
[openDlg setCanChooseFiles:YES];
[openDlg setAllowsMultipleSelection:NO];
[openDlg setCanChooseDirectories:NO];

if ( [openDlg runModal] == NSOKButton )
{
[self.logDatasource stopLogger];

NSArray* urls = [openDlg URLs];
if (urls != nil && [urls count] > 0) {
if (self.logDatasource != nil && [self.logDatasource isLogging]) {
[self.logDatasource stopLogger];
self.logDatasource = nil;
}

NSURL* url = urls[0];
NSLog(@"Open url: %@", url);
[openDlg close];

NSArray *arguments = nil;
arguments = @[[url path]];
NSLog(@"Will get log from: %@", arguments);
[self clearLog:nil];
[self.logDatasource readLog:arguments];

// NSDictionary* filtersToImport = [NSDictionary dictionaryWithContentsOfURL:url];
//
// NSArray* keys = [filtersToImport keysSortedByValueUsingSelector:@selector(caseInsensitiveCompare:)];
// for (NSString* key in keys) {
// NSString* filter = filtersToImport[key];
// // TODO: figure out what to do for filters that already exist. For now just overwrite
// filters[key] = [NSPredicate predicateWithFormat:filter];
// }
//
// [self saveFilters];
// [self.filterListTable reloadData];
}
}


}

- (IBAction)importFilters:(id)sender {
NSOpenPanel* openDlg = [NSOpenPanel openPanel];
[openDlg setCanChooseFiles:YES];
Expand Down
3 changes: 3 additions & 0 deletions LogCat/LogDatasource.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

@property (strong) NSString* deviceId;
@property (atomic) BOOL isLogging;
@property (atomic) BOOL skipPidLookup;

- (void) startLogger;
- (void) stopLogger;
Expand All @@ -42,4 +43,6 @@
- (void) logMessage: (NSString*) message;
- (NSArray*) eventsForPredicate: (NSPredicate*) predicate;

- (void)readLog:(id)param;

@end
93 changes: 80 additions & 13 deletions LogCat/LogDatasource.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ @implementation LogDatasource
@synthesize delegate = _delegate;
@synthesize deviceId = _deviceId;
@synthesize isLogging;
@synthesize skipPidLookup;

@synthesize previousString = _previousString;
@synthesize startTime = _startTime;
Expand All @@ -79,6 +80,7 @@ @implementation LogDatasource

- (id)init {
if (self = [super init]) {
self.skipPidLookup = false;
self.pidMap = [NSMutableDictionary dictionary];
self.logData = [NSMutableArray arrayWithCapacity:0];
self.text = [NSMutableString stringWithCapacity:0];
Expand Down Expand Up @@ -134,6 +136,7 @@ - (void) startLogger {
}

- (void) stopLogger {
NSLog(@"Stop logging called.");
isLogging = NO;
[self.thread cancel];
self.thread = nil;
Expand All @@ -157,6 +160,9 @@ - (void) clearLog {
#pragma mark -

- (void) loadPID {
if (self.skipPidLookup) {
return;
}
NSArray *arguments = nil;
arguments = @[@"shell", @"ps"];

Expand Down Expand Up @@ -201,16 +207,21 @@ - (void) parsePID: (NSString*) pidInfo {
NSArray* lines = [pidInfo componentsSeparatedByCharactersInSet: [NSCharacterSet newlineCharacterSet]];

for (NSString* line in lines) {
if ([line hasPrefix:@"-"]) {
if ([line length] == 0) {
// skip blank lines
continue;
} else if ([line hasPrefix:@"-"]) {
continue;
} else if ([line hasPrefix:@"error:"]) {
NSLog(@"parsePID: %@", line);
if ([line isEqualToString:MULTIPLE_DEVICE_MSG]) {
NSLog(@"Multiple devices. Abort. (1)");
isLogging = NO;
[self onMultipleDevicesConnected];
[self stopLogger];
return;
} else if ([line isEqualToString:DEVICE_NOT_FOUND_MSG]) {
NSLog(@"Device not found. Abort. (1)");
isLogging = NO;
[self onDeviceNotFound];
[self stopLogger];
Expand Down Expand Up @@ -253,13 +264,19 @@ - (void) parsePID: (NSString*) pidInfo {
#pragma mark Log Loader
#pragma mark -


- (void)readLog:(id)param
{
isLogging = YES;
[self performSelectorOnMainThread:@selector(onLoggerStarted) withObject:nil waitUntilDone:NO];

NSArray *arguments = nil;
if (LOG_FORMAT == 1) {
if (param != nil) {
// assume caller is passing the arguments we need
self.skipPidLookup = YES;
arguments = param;

} else if (LOG_FORMAT == 1) {
arguments = @[@"logcat", @"-v", @"long"];

} else if (LOG_FORMAT == 2) {
Expand All @@ -272,7 +289,19 @@ - (void)readLog:(id)param

@try {

NSTask *task = [AdbTaskHelper adbTask:[self argumentsForDevice:arguments]];
NSTask *task = nil;
if (param != nil) {
task = [[NSTask alloc] init];

NSString *catPath = @"/bin/cat";

[task setLaunchPath:catPath];
[task setArguments: arguments];

} else {

task = [AdbTaskHelper adbTask:[self argumentsForDevice:arguments]];
}

NSPipe *pipe;
pipe = [NSPipe pipe];
Expand All @@ -284,20 +313,28 @@ - (void)readLog:(id)param
file = [pipe fileHandleForReading];

[task launch];
//NSLog(@"Task isRunning: %d", task.isRunning);

while (isLogging && [task isRunning]) {
NSData *data = nil;
while (data == nil) {
NSData *data = nil;
while (isLogging && (((data = [file availableData]) != nil) || [task isRunning])) {
//NSLog(@"Task: %d, data=%@", [task isRunning], data);
while (data == nil || [data length] == 0) {
data = [file availableData];
if ((data == nil || [data length] == 0) && ![task isRunning]) {
isLogging = NO;
break;
}
}


if (data != nil) {

NSString *string;
string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
// NSLog(@"Data: %@", string);
if (LOG_FORMAT == 1) {
if (param != nil) {
NSLog(@"Parse: %@", string);
[self appendThreadtimeLog:string];
} else if (LOG_FORMAT == 1) {
[self performSelectorOnMainThread:@selector(appendLongLog:) withObject:string waitUntilDone:YES];

} else if (LOG_FORMAT == 2) {
Expand All @@ -311,6 +348,7 @@ - (void)readLog:(id)param
} else {
NSLog(@"Data was nil...");
}
data = nil;
}

[task terminate];
Expand All @@ -322,6 +360,7 @@ - (void)readLog:(id)param
NSBeep();
}

NSLog(@"Exited readlog loop.");
isLogging = NO;
[self.pidMap removeAllObjects];

Expand All @@ -331,6 +370,7 @@ - (void)readLog:(id)param

[self stopLogger];
NSLog(@"ADB Exited.");
self.skipPidLookup = NO;
}

- (void) logData:(NSData*) data {
Expand Down Expand Up @@ -549,8 +589,19 @@ - (void) appendThreadtimeLog: (NSString*) paramString {
}

- (void) parseThreadTimeLine: (NSString*) line {


if ([line hasPrefix:@"-appPID"] || [line hasPrefix:@"- appPID"]) {
NSArray *strings = [line componentsSeparatedByString:@","];
if ([strings count] == 3) {
NSString* pid = [strings[1] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString* app = [strings[2] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"Adding PID \"%@\" for app \"%@\"", pid, app);
[self.pidMap setValue:app forKey:pid];
}

if ([line hasPrefix:@"-"]) {
return;
} else if ([line hasPrefix:@"-"]) {
return;
} else if ([line hasPrefix:@"error:"]) {
NSLog(@"parseThreadTimeLine: \"%@\", %@", line, self);
Expand All @@ -561,6 +612,7 @@ - (void) parseThreadTimeLine: (NSString*) line {
// [self onMultipleDevicesConnected];
return;
} else if ([line hasPrefix:DEVICE_NOT_FOUND_MSG]) {
NSLog(@"Device Not Found. Abort Logcat.");
isLogging = NO;
[self performSelectorOnMainThread:@selector(onMultipleDevicesConnected) withObject:nil waitUntilDone:YES];
// [self onMultipleDevicesConnected];
Expand Down Expand Up @@ -671,15 +723,27 @@ - (void)appendLongLog:(NSString*)paramString
}

for (NSString* line in lines) {
if ([line hasPrefix:@"-"]) {

if ([line hasPrefix:@"-appPID"] || [line hasPrefix:@"- appPID"]) {
NSArray *strings = [line componentsSeparatedByString:@","];
if ([strings count] == 3) {
[self.pidMap setValue:strings[2] forKey:strings[1]];
}

continue;
} else if ([line hasPrefix:@"-"]) {


continue;
} else if ([line hasPrefix:@"error:"]) {
NSLog(@"appendLongLog: %@", line);
if ([line isEqualToString:MULTIPLE_DEVICE_MSG]) {
NSLog(@"Mulitple devices. Abort.");
isLogging = NO;
[self onMultipleDevicesConnected];
return;
} else if ([line isEqualToString:DEVICE_NOT_FOUND_MSG]) {
NSLog(@"Device not found. Abort.");
isLogging = NO;
[self onMultipleDevicesConnected];
return;
Expand All @@ -700,7 +764,7 @@ - (void)appendLongLog:(NSString*)paramString
self.tid = [line substringWithRange:[match rangeAtIndex:3]];
self.app = (self.pidMap)[self.pid];
if (self.app == nil) {
NSLog(@"%@ not found in pid map.", self.pid);
NSLog(@"%@ not found in pid map. (1)", self.pid);
[self loadPID];
self.app = (self.pidMap)[self.pid];
if (self.app == nil) {
Expand Down Expand Up @@ -789,10 +853,13 @@ - (void) logMessage: (NSString*) message {
}

- (NSString*) appNameForPid:(NSString*) pidVal {

NSString* appVal = (self.pidMap)[pidVal];
if (appVal == nil) {
NSLog(@"%@ not found in pid map.", pidVal);
[self loadPID];
NSLog(@"%@ not found in pid map. (2)", pidVal);
if (!self.skipPidLookup) {
[self loadPID];
}
appVal = (self.pidMap)[pidVal];
if (appVal == nil) {
// This is normal during startup because there can be log
Expand Down
Loading

0 comments on commit b8a1fac

Please sign in to comment.