Seperate epoll into its own function

This commit is contained in:
blankie 2023-01-21 14:55:09 +07:00
parent 6cca794fee
commit 35ee4d3ff4
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
2 changed files with 30 additions and 25 deletions

View File

@ -204,9 +204,36 @@ void LogcatThread::_handle_line(char* buf, size_t length, bool is_stdout) {
this->_put_if_not_stopped(std::move(log_entry));
}
void LogcatThread::_run(std::stop_token stoken) {
void LogcatThread::_run_epoll_round() {
struct epoll_event events[EPOLL_MAX_EVENTS];
int ready_fds = epoll_wait(this->_epoll_fd, events, EPOLL_MAX_EVENTS, 1000);
if (ready_fds == -1) {
std::string log_entry = format_log(std::string("epoll_wait(): ") + strerror(errno));
printf("%s\n", log_entry.c_str());
this->_put_if_not_stopped(std::move(log_entry));
return;
}
for (int i=0; i < ready_fds; i++) {
const bool is_stdout = events[i].data.fd == this->_stdout_read_fd;
char* buf = is_stdout ? this->_stdout_buf : this->_stderr_buf;
size_t* used = is_stdout ? &this->_stdout_buf_used : &this->_stderr_buf_used;
try {
handle_fd(events[i].data.fd, buf, used, &LogcatThread::_handle_line, this, is_stdout);
} catch (const std::exception& e) {
std::string log_entry = "Failed to handle std";
log_entry += is_stdout ? "out: " : "err: ";
log_entry += e.what();
log_entry = format_log(std::move(log_entry));
printf("%s\n", log_entry.c_str());
this->_put_if_not_stopped(std::move(log_entry));
}
}
}
void LogcatThread::_run(std::stop_token stoken) {
while (!stoken.stop_requested()) {
#ifndef NDEBUG
if (this->debug_log_request.test()) {
@ -215,29 +242,6 @@ void LogcatThread::_run(std::stop_token stoken) {
}
#endif
int ready_fds = epoll_wait(this->_epoll_fd, events, EPOLL_MAX_EVENTS, 1000);
if (ready_fds == -1) {
std::string log_entry = format_log(std::string("epoll_wait(): ") + strerror(errno));
printf("%s\n", log_entry.c_str());
this->_put_if_not_stopped(std::move(log_entry));
break;
}
for (int i=0; i < ready_fds; i++) {
const bool is_stdout = events[i].data.fd == this->_stdout_read_fd;
char* buf = is_stdout ? this->_stdout_buf : this->_stderr_buf;
size_t* used = is_stdout ? &this->_stdout_buf_used : &this->_stderr_buf_used;
try {
handle_fd(events[i].data.fd, buf, used, &LogcatThread::_handle_line, this, is_stdout);
} catch (const std::exception& e) {
std::string log_entry = "Failed to handle std";
log_entry += is_stdout ? "out: " : "err: ";
log_entry += e.what();
log_entry = format_log(std::move(log_entry));
printf("%s\n", log_entry.c_str());
this->_put_if_not_stopped(std::move(log_entry));
}
}
this->_run_epoll_round();
}
}

View File

@ -28,6 +28,7 @@ public:
private:
void _put_if_not_stopped(LogcatThreadItem item);
void _handle_line(char* buf, size_t length, bool is_stdout);
void _run_epoll_round();
void _run(std::stop_token stoken);
int _epoll_fd = -1;