Simple class to just discard anything written to stream by redirecting it to /dev/null.
More...
#include <IOIntercept.h>
|
void | setReplacementFD (int fd) |
| set the replacement file descriptor, should be called in the constructor of derived classes
|
|
bool | replaceFD (int fileDescriptor) |
| Replace the file descriptor of m_fileObject with the one passed.
|
|
|
static void | readFD (int fd, std::string &out) |
| Read the contents of a file descriptor until there is no more input and place them in out.
|
|
|
std::ostream & | m_stream |
| C++ stream object, only needed to flush before replacement.
|
|
FILE * | m_fileObject |
| File object of the file we want to replace, needed to obtain file descriptor and to flush.
|
|
int | m_savedFD { -1} |
| Saved file descriptor: a duplicate of the file descriptor of m_fileObject.
|
|
int | m_replacementFD { -1} |
| Replacement file descriptor to be used while capturing.
|
|
bool | m_capturing {false} |
| Check whether we are already capturing.
|
|
Simple class to just discard anything written to stream by redirecting it to /dev/null.
Definition at line 88 of file IOIntercept.h.
◆ DiscardStream()
Create StreamInterceptor which will redirect to /dev/null.
Definition at line 145 of file IOIntercept.cc.
146 {
147 int devNull = open("/dev/null", O_WRONLY);
148 if (devNull < 0) {
149
150 B2ERROR("Cannot open /dev/null: " << std::strerror(errno));
151 return;
152 }
154 }
void setReplacementFD(int fd)
set the replacement file descriptor, should be called in the constructor of derived classes
StreamInterceptor(std::ostream &stream, FILE *fileObject)
Construct keeping a reference to the std::ostream and the file descriptor which are associated with t...
◆ finish()
stop intercepting the stream.
- Returns
- true if the object was capturing and stream could be restored
Definition at line 46 of file IOIntercept.h.
47 {
51 }
bool replaceFD(int fileDescriptor)
Replace the file descriptor of m_fileObject with the one passed.
int m_savedFD
Saved file descriptor: a duplicate of the file descriptor of m_fileObject.
bool m_capturing
Check whether we are already capturing.
◆ readFD()
void readFD |
( |
int |
fd, |
|
|
std::string & |
out |
|
) |
| |
|
staticprotectedinherited |
Read the contents of a file descriptor until there is no more input and place them in out.
- Parameters
-
fd | file descriptor to read, should be opened in non blocking mode O_NOBLOCK |
out | string to be replaced with all the bytes read from fd |
Definition at line 102 of file IOIntercept.cc.
103 {
104 out.clear();
105 if (fd <= 0) return;
106
107 static std::unique_ptr<char[]> buffer(new char[1024]);
108
109 while (true) {
110 ssize_t size = read(fd, buffer.get(), 1024);
111 if (size <= 0) {
112
113 if (size < 0 && errno == EINTR) continue;
114 break;
115 }
116 out.append(buffer.get(), static_cast<size_t>(size));
117 }
118 }
◆ replaceFD()
bool replaceFD |
( |
int |
fileDescriptor | ) |
|
|
protectedinherited |
Replace the file descriptor of m_fileObject with the one passed.
- Parameters
-
fileDescriptor | file descriptor to be set for m_fileObject using dup2() |
Definition at line 120 of file IOIntercept.cc.
121 {
122
123 if (fileDescriptor < 0) return false;
124
127
129
131 if (currentFD < 0) {
132 B2ERROR("Error obtaining file descriptor: " << std::strerror(errno));
133 return false;
134 }
135 while (dup2(fileDescriptor, currentFD) < 0) {
136 if (errno != EINTR && errno != EBUSY) {
137 B2ERROR("Error in dup2(), cannot replace file descriptor: " << std::strerror(errno));
138 return false;
139 }
140
141 }
142 return true;
143 }
std::ostream & m_stream
C++ stream object, only needed to flush before replacement.
FILE * m_fileObject
File object of the file we want to replace, needed to obtain file descriptor and to flush.
◆ setReplacementFD()
void setReplacementFD |
( |
int |
fd | ) |
|
|
inlineprotectedinherited |
set the replacement file descriptor, should be called in the constructor of derived classes
Definition at line 54 of file IOIntercept.h.
int m_replacementFD
Replacement file descriptor to be used while capturing.
◆ start()
start intercepting the stream.
- Returns
- true on success, false on any error
Definition at line 38 of file IOIntercept.h.
◆ m_capturing
Check whether we are already capturing.
Definition at line 73 of file IOIntercept.h.
◆ m_fileObject
File object of the file we want to replace, needed to obtain file descriptor and to flush.
Definition at line 67 of file IOIntercept.h.
◆ m_replacementFD
int m_replacementFD { -1} |
|
protectedinherited |
Replacement file descriptor to be used while capturing.
Definition at line 71 of file IOIntercept.h.
◆ m_savedFD
Saved file descriptor: a duplicate of the file descriptor of m_fileObject.
Definition at line 69 of file IOIntercept.h.
◆ m_stream
C++ stream object, only needed to flush before replacement.
Definition at line 65 of file IOIntercept.h.
The documentation for this class was generated from the following files: