MCPcopy Create free account
hub / github.com/awslabs/aws-lambda-cpp / CapturedStream

Class CapturedStream

tests/gtest/gtest-all.cc:10776–10862  ·  view source on GitHub ↗

Object that captures an output stream (stdout/stderr).

Source from the content-addressed store, hash-verified

10774
10775// Object that captures an output stream (stdout/stderr).
10776class CapturedStream {
10777 public:
10778 // The ctor redirects the stream to a temporary file.
10779 explicit CapturedStream(int fd) : fd_(fd), uncaptured_fd_(dup(fd)) {
10780# if GTEST_OS_WINDOWS
10781 char temp_dir_path[MAX_PATH + 1] = { '\0' }; // NOLINT
10782 char temp_file_path[MAX_PATH + 1] = { '\0' }; // NOLINT
10783
10784 ::GetTempPathA(sizeof(temp_dir_path), temp_dir_path);
10785 const UINT success = ::GetTempFileNameA(temp_dir_path,
10786 "gtest_redir",
10787 0, // Generate unique file name.
10788 temp_file_path);
10789 GTEST_CHECK_(success != 0)
10790 << "Unable to create a temporary file in " << temp_dir_path;
10791 const int captured_fd = creat(temp_file_path, _S_IREAD | _S_IWRITE);
10792 GTEST_CHECK_(captured_fd != -1) << "Unable to open temporary file "
10793 << temp_file_path;
10794 filename_ = temp_file_path;
10795# else
10796 // There's no guarantee that a test has write access to the current
10797 // directory, so we create the temporary file in the /tmp directory
10798 // instead. We use /tmp on most systems, and /sdcard on Android.
10799 // That's because Android doesn't have /tmp.
10800# if GTEST_OS_LINUX_ANDROID
10801 // Note: Android applications are expected to call the framework's
10802 // Context.getExternalStorageDirectory() method through JNI to get
10803 // the location of the world-writable SD Card directory. However,
10804 // this requires a Context handle, which cannot be retrieved
10805 // globally from native code. Doing so also precludes running the
10806 // code as part of a regular standalone executable, which doesn't
10807 // run in a Dalvik process (e.g. when running it through 'adb shell').
10808 //
10809 // The location /sdcard is directly accessible from native code
10810 // and is the only location (unofficially) supported by the Android
10811 // team. It's generally a symlink to the real SD Card mount point
10812 // which can be /mnt/sdcard, /mnt/sdcard0, /system/media/sdcard, or
10813 // other OEM-customized locations. Never rely on these, and always
10814 // use /sdcard.
10815 char name_template[] = "/sdcard/gtest_captured_stream.XXXXXX";
10816# else
10817 char name_template[] = "/tmp/captured_stream.XXXXXX";
10818# endif // GTEST_OS_LINUX_ANDROID
10819 const int captured_fd = mkstemp(name_template);
10820 if (captured_fd == -1) {
10821 GTEST_LOG_(WARNING)
10822 << "Failed to create tmp file " << name_template
10823 << " for test; does the test have access to the /tmp directory?";
10824 }
10825 filename_ = name_template;
10826# endif // GTEST_OS_WINDOWS
10827 fflush(nullptr);
10828 dup2(captured_fd, fd_);
10829 close(captured_fd);
10830 }
10831
10832 ~CapturedStream() {
10833 remove(filename_.c_str());

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected