1 /* 2 Custom Google Test assertions. 3 4 Copyright (c) 2012-2014, Victor Zverovich 5 All rights reserved. 6 7 Redistribution and use in source and binary forms, with or without 8 modification, are permitted provided that the following conditions are met: 9 10 1. Redistributions of source code must retain the above copyright notice, this 11 list of conditions and the following disclaimer. 12 2. Redistributions in binary form must reproduce the above copyright notice, 13 this list of conditions and the following disclaimer in the documentation 14 and/or other materials provided with the distribution. 15 16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 20 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include "gtest-extra.h" 29 30 #if FMT_USE_FILE_DESCRIPTORS 31 32 using fmt::File; 33 34 void OutputRedirect::flush() { 35 #if EOF != -1 36 # error "FMT_RETRY assumes return value of -1 indicating failure" 37 #endif 38 int result = 0; 39 FMT_RETRY(result, fflush(file_)); 40 if (result != 0) 41 throw fmt::SystemError(errno, "cannot flush stream"); 42 } 43 44 void OutputRedirect::restore() { 45 if (original_.descriptor() == -1) 46 return; // Already restored. 47 flush(); 48 // Restore the original file. 49 original_.dup2(FMT_POSIX(fileno(file_))); 50 original_.close(); 51 } 52 53 OutputRedirect::OutputRedirect(FILE *file) : file_(file) { 54 flush(); 55 int fd = FMT_POSIX(fileno(file)); 56 // Create a File object referring to the original file. 57 original_ = File::dup(fd); 58 // Create a pipe. 59 File write_end; 60 File::pipe(read_end_, write_end); 61 // Connect the passed FILE object to the write end of the pipe. 62 write_end.dup2(fd); 63 } 64 65 OutputRedirect::~OutputRedirect() FMT_NOEXCEPT { 66 try { 67 restore(); 68 } catch (const std::exception &e) { 69 std::fputs(e.what(), stderr); 70 } 71 } 72 73 std::string OutputRedirect::restore_and_read() { 74 // Restore output. 75 restore(); 76 77 // Read everything from the pipe. 78 std::string content; 79 if (read_end_.descriptor() == -1) 80 return content; // Already read. 81 enum { BUFFER_SIZE = 4096 }; 82 char buffer[BUFFER_SIZE]; 83 std::size_t count = 0; 84 do { 85 count = read_end_.read(buffer, BUFFER_SIZE); 86 content.append(buffer, count); 87 } while (count != 0); 88 read_end_.close(); 89 return content; 90 } 91 92 std::string read(File &f, std::size_t count) { 93 std::string buffer(count, '\0'); 94 std::size_t n = 0, offset = 0; 95 do { 96 n = f.read(&buffer[offset], count - offset); 97 // We can't read more than size_t bytes since count has type size_t. 98 offset += static_cast<std::size_t>(n); 99 } while (offset < count && n != 0); 100 buffer.resize(offset); 101 return buffer; 102 } 103 104 #endif // FMT_USE_FILE_DESCRIPTORS 105 106 std::string format_system_error(int error_code, fmt::StringRef message) { 107 fmt::MemoryWriter out; 108 fmt::format_system_error(out, error_code, message); 109 return out.str(); 110 } 111