Skip to content

Commit 028cec0

Browse files
committed
Fix missing C stderr output (fprintf(stderr, ...) not shown)
1 parent 190caf6 commit 028cec0

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

src/xinterpreter.cpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,17 @@ using namespace std::placeholders;
5656
namespace xcpp
5757
{
5858
struct StreamRedirectRAII {
59+
std::string &out;
5960
std::string &err;
60-
StreamRedirectRAII(std::string &e) : err(e) {
61-
Cpp::BeginStdStreamCapture(Cpp::kStdErr);
61+
StreamRedirectRAII(std::string &o, std::string &e)
62+
: out(o), err(e)
63+
{
6264
Cpp::BeginStdStreamCapture(Cpp::kStdOut);
65+
Cpp::BeginStdStreamCapture(Cpp::kStdErr);
6366
}
6467
~StreamRedirectRAII() {
65-
std::string out = Cpp::EndStdStreamCapture();
6668
err = Cpp::EndStdStreamCapture();
67-
std::cout << out;
69+
out = Cpp::EndStdStreamCapture();
6870
}
6971
};
7072

@@ -163,11 +165,12 @@ __get_cxx_version ()
163165
}
164166

165167
std::string err;
168+
std::string out;
166169

167170
// Attempt normal evaluation
168171
try
169172
{
170-
StreamRedirectRAII R(err);
173+
StreamRedirectRAII R(out, err);
171174
compilation_result = Cpp::Process(code.c_str());
172175
}
173176
catch (std::exception& e)
@@ -182,13 +185,8 @@ __get_cxx_version ()
182185
ename = "Error: ";
183186
}
184187

185-
if (compilation_result)
186-
{
187-
errorlevel = 1;
188-
ename = "Error: ";
189-
evalue = "Compilation error! " + err;
190-
std::cerr << err;
191-
}
188+
if (!out.empty()) std::cout << out;
189+
if (!err.empty()) std::cerr << err;
192190

193191
// Flush streams
194192
std::cout << std::flush;
@@ -201,6 +199,13 @@ __get_cxx_version ()
201199
std::cerr.rdbuf(cerr_strbuf);
202200
}
203201

202+
if (compilation_result)
203+
{
204+
errorlevel = 1;
205+
ename = "Error: ";
206+
evalue = "Compilation error! " + err;
207+
}
208+
204209
// Depending of error level, publish execution result or execution
205210
// error, and compose execute_reply message.
206211
if (errorlevel)

0 commit comments

Comments
 (0)