From 35ee4d3ff453cf41390100cc781c78cfa0e42ecb Mon Sep 17 00:00:00 2001 From: blankie Date: Sat, 21 Jan 2023 14:55:09 +0700 Subject: [PATCH] Seperate epoll into its own function --- logcat_thread.cpp | 54 +++++++++++++++++++++++++---------------------- logcat_thread.h | 1 + 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/logcat_thread.cpp b/logcat_thread.cpp index d3fa7bb..67a9f5e 100644 --- a/logcat_thread.cpp +++ b/logcat_thread.cpp @@ -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(); } } diff --git a/logcat_thread.h b/logcat_thread.h index 2b8d66b..25fc3ad 100644 --- a/logcat_thread.h +++ b/logcat_thread.h @@ -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;