-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Made it work on OS X. #16
Changes from all commits
1f6378c
4f4f9ee
38e7680
d8e599c
8d9c63d
1de8da6
73ad987
3ae14b9
7bd151d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,8 +55,10 @@ struct C5T_LOGGER_SINGLETON_Interface { | |
virtual C5T_LOGGER_Interface& operator[](std::string const& log_file_name) = 0; | ||
}; | ||
|
||
#ifndef C5T_DLIB | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Enabled by now-merged C5T/Current#1004, before this was silently OK on Linux, but failing on OS X. |
||
// NOTE(dkorolev): This is deliberately not "pimpl", since it's not to be used from `dlib_*.cc` sources! | ||
C5T_LOGGER_SINGLETON_Interface& C5T_LOGGER_CREATE_SINGLETON(); | ||
#endif | ||
|
||
struct C5T_LOGGER_SINGLETON_Holder final { | ||
C5T_LOGGER_SINGLETON_Interface* ptr = nullptr; | ||
|
@@ -66,7 +68,12 @@ struct C5T_LOGGER_SINGLETON_Holder final { | |
} | ||
C5T_LOGGER_SINGLETON_Interface& Val() { | ||
if (ptr == nullptr) { | ||
#ifndef C5T_DLIB | ||
return C5T_LOGGER_CREATE_SINGLETON(); | ||
#else | ||
std::cerr << "FATAL: No logger initialized.\n"; | ||
__builtin_trap(); | ||
#endif | ||
} | ||
return *ptr; | ||
} | ||
|
@@ -87,7 +94,9 @@ void C5T_LOGGER_USE(T&& wrapper) { | |
C5T_LOGGER_USE(wrapper.Logger()); | ||
} | ||
|
||
#ifndef C5T_DLIB | ||
inline void C5T_LOGGER_SET_LOGS_DIR(std::string dir) { current::logger::C5T_LOGGER_CREATE_SINGLETON().SetLogsDir(dir); } | ||
#endif | ||
|
||
#define C5T_LOGGER_LIST(cb) C5T_LOGGER().C5T_LOGGER_LIST_Impl(cb) | ||
#define C5T_LOGGER_FIND(key, cb_found, cb_notfound) C5T_LOGGER().C5T_LOGGER_FIND_Impl(key, cb_found, cb_notfound) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -141,9 +141,9 @@ void C5T_POPEN2(std::vector<std::string> const& cmdline, | |
return std::thread( | ||
[](std::function<void(const std::string)> cb_line, int read_fd, int terminate_fd) { | ||
struct pollfd fds[2]; | ||
fds[0].fd = read_fd; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here the tweak works since I now check the termination signal first. (Strictly speaking, there was no need to flip them in the args to My best guess is that the true reason this code did not work before on OS X is that once the child process is terminated, I did not explore much further, just made sure to give priority to the check that the Tested afresh with https://github.com/dimacurrentai/exec_on_macos/. |
||
fds[0].fd = terminate_fd; | ||
fds[0].events = POLLIN; | ||
fds[1].fd = terminate_fd; | ||
fds[1].fd = read_fd; | ||
fds[1].events = POLLIN; | ||
|
||
char buf[1000]; | ||
|
@@ -153,6 +153,9 @@ void C5T_POPEN2(std::vector<std::string> const& cmdline, | |
while (true) { | ||
::poll(fds, 2, -1); | ||
if (fds[0].revents & POLLIN) { | ||
// NOTE(dkorolev): Termination signaled. | ||
break; | ||
} else if (fds[1].revents & POLLIN) { | ||
ssize_t const n = ::read(read_fd, buf, sizeof(buf) - 1); | ||
if (n < 0) { | ||
// NOTE(dkorolev): This may or may not be a major issue. | ||
|
@@ -161,10 +164,6 @@ void C5T_POPEN2(std::vector<std::string> const& cmdline, | |
} | ||
buf[n] = '\0'; | ||
grouper.Feed(buf); | ||
} else if (fds[1].revents & POLLIN) { | ||
// NOTE(dkorolev): Termination signaled. | ||
// std::cerr << __LINE__ << std::endl; | ||
break; | ||
} | ||
} | ||
}, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,34 +10,34 @@ | |
|
||
TEST(Popen2Test, Smoke) { | ||
std::string result; | ||
C5T_POPEN2({"/usr/bin/bash", "-c", "echo PASS"}, [&result](std::string const& line) { result = line; }); | ||
C5T_POPEN2({"bash", "-c", "echo PASS"}, [&result](std::string const& line) { result = line; }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And the last change was that |
||
EXPECT_EQ(result, "PASS"); | ||
} | ||
|
||
TEST(Popen2Test, WithDelay) { | ||
std::string result; | ||
C5T_POPEN2({"/usr/bin/bash", "-c", "sleep 0.01; echo PASS2"}, [&result](std::string const& line) { result = line; }); | ||
C5T_POPEN2({"bash", "-c", "sleep 0.01; echo PASS2"}, [&result](std::string const& line) { result = line; }); | ||
EXPECT_EQ(result, "PASS2"); | ||
} | ||
|
||
TEST(Popen2Test, InnerBashBecauseParentheses) { | ||
std::string result; | ||
C5T_POPEN2({"/usr/bin/bash", "-c", "(sleep 0.01; echo PASS3)"}, | ||
C5T_POPEN2({"bash", "-c", "(sleep 0.01; echo PASS3)"}, | ||
[&result](std::string const& line) { result = line; }); | ||
EXPECT_EQ(result, "PASS3"); | ||
} | ||
|
||
TEST(Popen2Test, ThreePrints) { | ||
std::string result; | ||
C5T_POPEN2({"/usr/bin/bash", "-c", "echo ONE; sleep 0.01; echo TWO; sleep 0.01; echo THREE"}, | ||
C5T_POPEN2({"bash", "-c", "echo ONE; sleep 0.01; echo TWO; sleep 0.01; echo THREE"}, | ||
[&result](std::string const& line) { result += line + ' '; }); | ||
ASSERT_EQ(result, "ONE TWO THREE "); | ||
} | ||
|
||
TEST(Popen2Test, KillsSuccessfully) { | ||
bool nope = false; | ||
C5T_POPEN2( | ||
{"/usr/bin/bash", "-c", "sleep 10; echo NOPE"}, | ||
{"bash", "-c", "sleep 10; echo NOPE"}, | ||
[&nope](std::string const&) { nope = true; }, | ||
[](Popen2Runtime& run) { | ||
std::this_thread::sleep_for(std::chrono::milliseconds(5)); | ||
|
@@ -49,7 +49,7 @@ TEST(Popen2Test, KillsSuccessfully) { | |
TEST(Popen2Test, ReadsStdin) { | ||
std::string c; | ||
C5T_POPEN2( | ||
{"/usr/bin/bash", "-c", "read A; read B; echo $((A+B))"}, | ||
{"bash", "-c", "read A; read B; echo $((A+B))"}, | ||
[&c](std::string const& line) { c = line; }, | ||
[](Popen2Runtime& run) { run("1\n2\n"); }); | ||
ASSERT_EQ(c, "3"); | ||
|
@@ -58,7 +58,7 @@ TEST(Popen2Test, ReadsStdin) { | |
TEST(Popen2Test, ReadsStdinForever) { | ||
std::string result = "result:"; | ||
C5T_POPEN2( | ||
{"/usr/bin/bash", | ||
{"bash", | ||
"-c", | ||
"while true; do read A; read B; C=$((A+B)); if [ $C == '0' ]; then exit; fi; echo $C; done"}, | ||
[&result](std::string const& line) { result += ' ' + line; }, | ||
|
@@ -68,13 +68,13 @@ TEST(Popen2Test, ReadsStdinForever) { | |
|
||
TEST(Popen2Test, MultipleOutputLines) { | ||
std::string result = "result:"; | ||
C5T_POPEN2({"/usr/bin/bash", "-c", "seq 10"}, [&result](std::string const& line) { result += ' ' + line; }); | ||
C5T_POPEN2({"bash", "-c", "seq 10"}, [&result](std::string const& line) { result += ' ' + line; }); | ||
ASSERT_EQ(result, "result: 1 2 3 4 5 6 7 8 9 10"); | ||
} | ||
|
||
TEST(Popen2Test, MultipleOutputLinesWithMath) { | ||
std::string result = "result:"; | ||
C5T_POPEN2({"/usr/bin/bash", "-c", "for i in $(seq 3 7) ; do echo $((i * i)) ; done"}, | ||
C5T_POPEN2({"bash", "-c", "for i in $(seq 3 7) ; do echo $((i * i)) ; done"}, | ||
[&result](std::string const& line) { result += ' ' + line; }); | ||
ASSERT_EQ(result, "result: 9 16 25 36 49"); | ||
} | ||
|
@@ -88,7 +88,7 @@ TEST(Popen2Test, ReadsStdinAndCanBeKilled) { | |
current::WaitableAtomic<Ctx> ctx; | ||
std::thread t([&ctx]() { | ||
C5T_POPEN2( | ||
{"/usr/bin/bash", "-c", "while true; do read A; read B; C=$((A+B)); echo $C; done"}, | ||
{"bash", "-c", "while true; do read A; read B; C=$((A+B)); echo $C; done"}, | ||
[&ctx](std::string const& line) { | ||
ctx.MutableScopedAccessor()->sums.push_back(current::FromString<int>(line)); | ||
}, | ||
|
@@ -123,7 +123,7 @@ TEST(Popen2Test, Stderr) { | |
std::string text_stdout; | ||
std::string text_stderr; | ||
C5T_POPEN2( | ||
{"/usr/bin/bash", "-c", "echo out; echo err >/dev/stderr"}, | ||
{"bash", "-c", "echo out; echo err >/dev/stderr"}, | ||
[&text_stdout](std::string const& s) { text_stdout = s; }, | ||
[](Popen2Runtime&) {}, | ||
[&text_stderr](std::string const& s) { text_stderr = s; }); | ||
|
@@ -134,7 +134,7 @@ TEST(Popen2Test, Stderr) { | |
TEST(Popen2Test, Env) { | ||
std::string result; | ||
C5T_POPEN2( | ||
{"/usr/bin/bash", "-c", "echo $FOO"}, | ||
{"bash", "-c", "echo $FOO"}, | ||
[&result](std::string const& s) { result = s; }, | ||
[](Popen2Runtime&) {}, | ||
[](std::string const& unused_stderr) { static_cast<void>(unused_stderr); }, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Enabled by now-merged C5T/Current#1004, before this was silently OK on Linux, but failing on OS X.