#include <SystemFile.h>
Public Types | |
| using | OutputRedirectVisitor = AZStd::function< void(AZStd::span< AZStd::byte >)> |
Public Member Functions | |
| FileDescriptorCapturer (int sourceDescriptor) | |
| void | Start (OutputRedirectVisitor redirectCallback, AZStd::chrono::milliseconds waitTimeout=AZStd::chrono::seconds(1), int pipeSize=DefaultPipeSize) |
| void | Stop () |
| Stops capture of file descriptor and reset it back to it's previous value. | |
| int | WriteBypassingCapture (const void *data, unsigned int size) |
Static Public Attributes | |
| static constexpr int | DefaultPipeSize = (1 << 16) |
Utility class for capturing the output of file descriptor redirection using with RAII behavior. NOTE: This class creates a new thread when Start() is called to pump the read end of the pipe when it is filled with data. The thread is terminated in Stop Example:
printf("Test"); // prints to stdout AZ::IO::FileDescriptorCapturer redirectStdoutToFile(1); redirectStdoutToFile.Start(); printf("Test"); // capture stdout as part of pipe bool testWasOutput{}; auto StdoutVisitor [&testWasOutput](AZStd::span<const AZStd::byte> capturedBytes) { AZStd::string_view capturedString(reinterpret_cast<const char*>(capturedBytes.data()), captureBytes.size()); testWasOutput = capturedString.contains("Test"); }; redirectStdout.Stop(StdoutVisitor); // Invokes visitor 0 or more times with captured data EXPECT_TRUE(testWasOutput);
| using AZ::IO::FileDescriptorCapturer::OutputRedirectVisitor = AZStd::function<void(AZStd::span<AZStd::byte>)> |
Redirects file descriptor to a visitor callback Internally a pipe is used to send output to the visitor
NOTE: This function will be called on a different thread than the one used to invoke Start() The caller is responsible for ensuring thread safety with the callback function
| void AZ::IO::FileDescriptorCapturer::Start | ( | OutputRedirectVisitor | redirectCallback, |
| AZStd::chrono::milliseconds | waitTimeout = AZStd::chrono::seconds(1), |
||
| int | pipeSize = DefaultPipeSize |
||
| ) |
Starts capture of file descriptor
| redirectCallback | callback to invoke when data is available for read |
| waitTimeout | Timeout to wait for the descriptor to be signaled with data default to 1 seconds NOTE: This is not a timeout of the capture operation, but determines how frequently a polling operation retries. Using AZStd::chrono::milliseconds::max() results in an infinite timeout |
| pipeSize | used internally for pipe used to send captured data to the redirect callback redirectCallback will not receieve more data than pipeSize in a single invocation |