Open 3D Engine AzCore API Reference 23.10.0
O3DE is an open-source, fully-featured, high-fidelity, modular 3D engine for building games and simulations, available to every industry.
AZ::IO::FileDescriptorCapturer Class Reference

#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)
 

Detailed Description

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);

Member Typedef Documentation

◆ OutputRedirectVisitor

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

Member Function Documentation

◆ Start()

void AZ::IO::FileDescriptorCapturer::Start ( OutputRedirectVisitor  redirectCallback,
AZStd::chrono::milliseconds  waitTimeout = AZStd::chrono::seconds(1),
int  pipeSize = DefaultPipeSize 
)

Starts capture of file descriptor

Parameters
redirectCallbackcallback to invoke when data is available for read
waitTimeoutTimeout 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
pipeSizeused internally for pipe used to send captured data to the redirect callback redirectCallback will not receieve more data than pipeSize in a single invocation

The documentation for this class was generated from the following file: