diff --git a/Agent/Utilities/NRAutoLogCollector.h b/Agent/Utilities/NRAutoLogCollector.h index a69ec9a9..6fb4ab6c 100644 --- a/Agent/Utilities/NRAutoLogCollector.h +++ b/Agent/Utilities/NRAutoLogCollector.h @@ -13,7 +13,7 @@ } -+ (void) redirectStandardOutputAndError; ++ (BOOL) redirectStandardOutputAndError; + (void) restoreStandardOutputAndError; @end diff --git a/Agent/Utilities/NRAutoLogCollector.m b/Agent/Utilities/NRAutoLogCollector.m index 81e9c119..1020115f 100644 --- a/Agent/Utilities/NRAutoLogCollector.m +++ b/Agent/Utilities/NRAutoLogCollector.m @@ -20,10 +20,10 @@ @interface NRAutoLogCollector() @implementation NRAutoLogCollector -+ (void) redirectStandardOutputAndError { ++ (BOOL) redirectStandardOutputAndError { // Create pipes for stdout and stderr if (pipe(stdoutPipe) == -1 || pipe(stderrPipe) == -1) { - return; + return false; } // Save the original stdout and stderr file descriptors @@ -34,7 +34,7 @@ + (void) redirectStandardOutputAndError { close(stdoutPipe[1]); close(stderrPipe[0]); close(stderrPipe[1]); - return; + return false; } // Redirect stdout and stderr to the write ends of the pipes @@ -45,7 +45,7 @@ + (void) redirectStandardOutputAndError { close(stderrPipe[1]); close(saved_stdout); close(saved_stderr); - return; + return false; } close(stdoutPipe[1]); // Close the original write end of the stdout pipe close(stderrPipe[1]); // Close the original write end of the stderr pipe @@ -62,6 +62,8 @@ + (void) redirectStandardOutputAndError { atexit_b(^{ [NRAutoLogCollector restoreStandardOutputAndError]; }); + + return true; } + (void) readAndLog:(int) fd { diff --git a/Tests/Unit-Tests/NewRelicAgentTests/Uncategorized/NRLoggerTests.m b/Tests/Unit-Tests/NewRelicAgentTests/Uncategorized/NRLoggerTests.m index 14d654e4..6d13effc 100644 --- a/Tests/Unit-Tests/NewRelicAgentTests/Uncategorized/NRLoggerTests.m +++ b/Tests/Unit-Tests/NewRelicAgentTests/Uncategorized/NRLoggerTests.m @@ -260,19 +260,9 @@ - (void) testAutoCollectedLogs { [NRMAFlags enableFeatures: NRFeatureFlag_RedirectStdOutStdErr]; // Set the remote log level to debug. [NRLogger setRemoteLogLevel:NRLogLevelDebug]; - [NRAutoLogCollector redirectStandardOutputAndError]; + XCTAssertTrue([NRAutoLogCollector redirectStandardOutputAndError]); - XCTestExpectation *delayExpectation1 = [self expectationWithDescription:@"Waiting for Log Queue"]; - - dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ - [delayExpectation1 fulfill]; - }); - - [self waitForExpectationsWithTimeout:7 handler:^(NSError * _Nullable error) { - if (error) { - XCTFail(@"Timeout error"); - } - }]; + sleep(1); // Three messages should reach the remote log file for upload. NSLog(@"NSLog Test \n\n"); @@ -283,17 +273,7 @@ - (void) testAutoCollectedLogs { os_log_error(customLog, "This is an error os_log message.\n"); os_log_fault(customLog, "This is a fault os_log message.\n"); - XCTestExpectation *delayExpectation2 = [self expectationWithDescription:@"Waiting for Log Queue"]; - - dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ - [delayExpectation2 fulfill]; - }); - - [self waitForExpectationsWithTimeout:8 handler:^(NSError * _Nullable error) { - if (error) { - XCTFail(@"Timeout error"); - } - }]; + sleep(1); [NRAutoLogCollector restoreStandardOutputAndError]; NSError* error;