Skip to content

Commit

Permalink
Improve log capturing, also capture stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
opa334 committed Jun 12, 2024
1 parent 0726c3e commit 908a99b
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions Application/Dopamine/UI/DOUIManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ - (void)completeJailbreak
[self.logView didComplete];
}

- (void)startLogCapture
- (void)observeFileDescriptor:(int)fd withCallback:(void (^)(char *line))callbackBlock
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
int stdout_pipe[2];
Expand All @@ -263,33 +263,28 @@ - (void)startLogCapture
return;
}

dup2(STDOUT_FILENO, stdout_orig[1]);
dup2(fd, stdout_orig[1]);
close(stdout_orig[0]);

dup2(stdout_pipe[1], STDOUT_FILENO);
dup2(stdout_pipe[1], fd);
close(stdout_pipe[1]);

char buffer[1024];
char cur = 0;
char line[1024];
int line_index = 0;
ssize_t bytes_read;

while ((bytes_read = read(stdout_pipe[0], buffer, sizeof(buffer) - 1)) > 0) {
while ((bytes_read = read(stdout_pipe[0], &cur, sizeof(cur))) > 0) {
@autoreleasepool {
// Tee: Write back to the original standard output
write(stdout_orig[1], buffer, bytes_read);

buffer[bytes_read] = '\0'; // Null terminate to handle as string
for (int i = 0; i < bytes_read; ++i) {
if (buffer[i] == '\n') {
line[line_index] = '\0';
NSString *str = [NSString stringWithUTF8String:line];
[self sendLog:str debug:YES];
line_index = 0;
} else {
if (line_index < sizeof(line) - 1) {
line[line_index++] = buffer[i];
}
write(stdout_orig[1], &cur, bytes_read);

if (cur == '\n') {
line[line_index] = '\0';
callbackBlock(line);
line_index = 0;
} else {
if (line_index < sizeof(line) - 1) {
line[line_index++] = cur;
}
}
}
Expand All @@ -298,6 +293,19 @@ - (void)startLogCapture
});
}

- (void)startLogCapture
{
[self observeFileDescriptor:STDOUT_FILENO withCallback:^(char *line) {
NSString *str = [NSString stringWithUTF8String:line];
[self sendLog:str debug:YES];
}];

[self observeFileDescriptor:STDERR_FILENO withCallback:^(char *line) {
NSString *str = [NSString stringWithUTF8String:line];
[self sendLog:str debug:YES];
}];
}

- (NSString *)localizedStringForKey:(NSString*)key
{
NSString *candidate = NSLocalizedString(key, nil);
Expand Down

0 comments on commit 908a99b

Please sign in to comment.