Gromacs
2024.3
|
#include <gromacs/utility/include/gromacs/utility/fileredirector.h>
Allows capturing stdout
and file output from code that supports it.
The calling code should take in this interface and use the stream objects it returns for all output that needs to support this redirection.
Currently, the (nearly) only purpose for this interface is for unit tests to capture the file output without duplicating the knowledge of which files are actually produced. The tests can also replace actual files with in-memory streams (e.g., a StringOutputStream), and test the output without actually accessing the file system and managing actual files.
As the main user for non-default implementation of this interface is tests, code using this interface generally uses a pattern where the redirector is initialized to defaultFileOutputRedirector(), and a separate setter is provided for tests to change the default. This allows code outside the tests (and outside the code actually calling the redirector) to be written as if this interface did not exist (i.e., they do not need to pass the default instance).
Also, the interface only supports text files, but can be generalized if/when there is a need for binary streams (see also TextOutputStream).
Public Member Functions | |
virtual TextOutputStream & | standardOutput ()=0 |
Returns a stream to use for stdout output. | |
virtual TextOutputStreamPointer | openTextOutputFile (const std::filesystem::path &filename)=0 |
Returns a stream to use for output to a file at a given path. More... | |
|
pure virtual |
Returns a stream to use for output to a file at a given path.
[in] | filename | Requested file name. |
Implemented in gmx::test::TestFileOutputRedirector, and gmx::anonymous_namespace{fileredirector.cpp}::DefaultOutputRedirector.