Skip to content

Commit 0891699

Browse files
committed
Moved error reporting into common_pipe
1 parent 4d47a47 commit 0891699

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

source/matplot/backend/gnuplot.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ namespace matplot::backend {
8080

8181
// Check if everything is OK
8282
if (perr != 0 || !pipe_.opened()) {
83-
std::cerr << "Opening the gnuplot pipe_ failed: "
84-
<< std::strerror(perr) << std::endl;
83+
std::cerr << "Opening the gnuplot pipe_ failed:\n";
84+
std::cerr << pipe_.error() << std::endl;
8585
std::cerr << "Please install gnuplot 5.2.6+: http://www.gnuplot.info"
8686
<< std::endl;
8787
}

source/matplot/util/popen.cpp

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
#define __STDC_WANT_LIB_EXT1__ 1
12
#include <matplot/util/popen.h>
23
#include <array>
34
#include <system_error>
5+
#include <cstring> // strerror
46

57
#ifdef _WIN32
68

@@ -19,7 +21,7 @@ int common_pipe::open(const std::string& cmd, char mode)
1921

2022
// Create a pipe for the child process's input
2123
if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
22-
return error(GetLastError(), "CreatePipe");
24+
return report(GetLastError(), "CreatePipe");
2325

2426
// Ensure the write handle to the pipe is not inherited by child
2527
// processes
@@ -94,7 +96,7 @@ int common_pipe::open(const std::string& cmd, char mode)
9496
int common_pipe::close(int *exit_code)
9597
{
9698
if (!opened())
97-
return error(EINVAL, "common_pipe::close");
99+
return report(EINVAL, "common_pipe::close");
98100
// Close the pipe to process:
99101
fclose(file_);
100102
file_ = nullptr;
@@ -130,9 +132,9 @@ int common_pipe::open(const std::string &cmd, char mode)
130132
constexpr auto WRITE = 1u;
131133
int fd[2];
132134
if (::pipe(fd) == -1)
133-
return error(errno, "pipe");
135+
return report(errno, "pipe");
134136
if ((pid = ::fork()) == -1)
135-
return error(errno, "fork");
137+
return report(errno, "fork");
136138

137139
if (pid == 0) { // child process
138140
if (mode == 'r') {
@@ -160,7 +162,7 @@ int common_pipe::open(const std::string &cmd, char mode)
160162
else
161163
file_ = ::fdopen(fd[WRITE], "w");
162164
if (file_ == nullptr)
163-
return error(errno, "fdopen");
165+
return report(errno, "fdopen");
164166
return 0;
165167
}
166168

@@ -183,8 +185,15 @@ int common_pipe::close(int *exit_code)
183185

184186
#endif // POSIX implementation
185187

186-
inline int common_pipe::error(int code, const std::string& what) const
188+
inline int common_pipe::report(int code, const std::string& what)
187189
{
190+
#if defined(_MSC_VER)
191+
const auto len = strerrorlen_s(code);
192+
error_.assign(len, ' ');
193+
strerror_s(error_.c_str(), len+1, code);
194+
#else
195+
error_ = std::strerror(code);
196+
#endif
188197
if (exceptions_)
189198
throw std::system_error{code, std::generic_category(), what};
190199
return code;
@@ -198,33 +207,33 @@ int opipe::write(std::string_view data)
198207
{
199208
constexpr auto CSIZE = sizeof(std::string_view::value_type);
200209
if (!opened())
201-
return error(EINVAL, "opipe::write");
210+
return report(EINVAL, "opipe::write");
202211
if (auto sz = std::fwrite(data.data(), CSIZE, data.length(), file_);
203212
sz != data.size()) {
204213
if (auto err = std::ferror(file_); err != 0)
205-
return error(EIO, "fwrite error");
214+
return report(EIO, "fwrite error");
206215
if (auto err = std::feof(file_); err != 0)
207-
return error(EIO, "fwrite eof");
216+
return report(EIO, "fwrite eof");
208217
}
209218
return 0;
210219
}
211220

212221
int opipe::flush(std::string_view data)
213222
{
214223
if (!opened())
215-
return error(EINVAL, "opipe::flush");
224+
return report(EINVAL, "opipe::flush");
216225
if (!data.empty())
217226
if (auto err = write(data); err != 0)
218-
return error(err, "opipe::write");
227+
return report(err, "opipe::write");
219228
if (auto res = std::fflush(file_); res != 0)
220-
return error(errno, "fflush");
229+
return report(errno, "fflush");
221230
return 0;
222231
}
223232

224233
int ipipe::read(std::string &data)
225234
{
226235
if (!opened())
227-
return error(EINVAL, "ipipe::read");
236+
return report(EINVAL, "ipipe::read");
228237
data.clear();
229238
auto buffer = std::array<char, 128>{};
230239
while (!std::feof(file_) && !std::ferror(file_)) {
@@ -234,7 +243,7 @@ int ipipe::read(std::string &data)
234243
data.append(buffer.data(), count);
235244
}
236245
if (std::ferror(file_))
237-
return error(EIO, "fread");
246+
return report(EIO, "fread");
238247
return 0;
239248
}
240249

source/matplot/util/popen.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class common_pipe : public proc_pipe
4747
FILE *file() const { return file_; }
4848
int close(int *exit_code = nullptr);
4949
bool opened() const { return file_ != nullptr; }
50+
const std::string& error() const { return error_; }
5051
bool exceptions() const { return exceptions_; }
5152
void exceptions(bool exc) { exceptions_ = exc; }
5253

@@ -61,7 +62,8 @@ class common_pipe : public proc_pipe
6162
close();
6263
}
6364
bool exceptions_ = false;
64-
int error(int err, const std::string& what) const;
65+
std::string error_{};
66+
int report(int err, const std::string& what);
6567
int open(const std::string &command, char mode);
6668
};
6769

0 commit comments

Comments
 (0)