]>
git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/fmt/test/gtest-extra.cc
1 // Formatting library for C++ - custom Google Test assertions
3 // Copyright (c) 2012 - present, Victor Zverovich
4 // All rights reserved.
6 // For the license information refer to format.h.
8 #include "gtest-extra.h"
10 #if FMT_USE_FILE_DESCRIPTORS
14 void OutputRedirect::flush() {
16 # error "FMT_RETRY assumes return value of -1 indicating failure"
19 FMT_RETRY(result
, fflush(file_
));
21 throw fmt::system_error(errno
, "cannot flush stream");
24 void OutputRedirect::restore() {
25 if (original_
.descriptor() == -1)
26 return; // Already restored.
28 // Restore the original file.
29 original_
.dup2(FMT_POSIX(fileno(file_
)));
33 OutputRedirect::OutputRedirect(FILE *f
) : file_(f
) {
35 int fd
= FMT_POSIX(fileno(f
));
36 // Create a file object referring to the original file.
37 original_
= file::dup(fd
);
40 file::pipe(read_end_
, write_end
);
41 // Connect the passed FILE object to the write end of the pipe.
45 OutputRedirect::~OutputRedirect() FMT_NOEXCEPT
{
48 } catch (const std::exception
&e
) {
49 std::fputs(e
.what(), stderr
);
53 std::string
OutputRedirect::restore_and_read() {
57 // Read everything from the pipe.
59 if (read_end_
.descriptor() == -1)
60 return content
; // Already read.
61 enum { BUFFER_SIZE
= 4096 };
62 char buffer
[BUFFER_SIZE
];
63 std::size_t count
= 0;
65 count
= read_end_
.read(buffer
, BUFFER_SIZE
);
66 content
.append(buffer
, count
);
72 std::string
read(file
&f
, std::size_t count
) {
73 std::string
buffer(count
, '\0');
74 std::size_t n
= 0, offset
= 0;
76 n
= f
.read(&buffer
[offset
], count
- offset
);
77 // We can't read more than size_t bytes since count has type size_t.
79 } while (offset
< count
&& n
!= 0);
80 buffer
.resize(offset
);
84 #endif // FMT_USE_FILE_DESCRIPTORS
86 std::string
format_system_error(int error_code
, fmt::string_view message
) {
87 fmt::memory_buffer out
;
88 format_system_error(out
, error_code
, message
);
89 return to_string(out
);