logmeow/logcat_thread.cpp

44 lines
1.2 KiB
C++

#include <unistd.h>
#include <sys/epoll.h>
#include "log.h"
#include "misc.h"
#include "logcat_thread.h"
#define EPOLL_MAX_EVENTS 10
LogcatThread::LogcatThread(bool* run_event_loop) {
this->_epoll_fd = epoll_create1(EPOLL_CLOEXEC);
if (this->_epoll_fd == -1) {
throw make_system_error("epoll_create1()");
}
this->_thread = std::thread(&LogcatThread::_run, this, run_event_loop);
}
LogcatThread::~LogcatThread() {
if (this->_epoll_fd != -1 && close(this->_epoll_fd)) {
log(std::string("Failed to close epoll file descriptor: ") + make_system_error("close()").what());
}
}
void LogcatThread::join() {
this->_thread.join();
}
void LogcatThread::_run(bool* run_event_loop) {
struct epoll_event events[EPOLL_MAX_EVENTS];
while (*run_event_loop) {
printf("(boop)\n");
int ready_fds = epoll_wait(this->_epoll_fd, events, EPOLL_MAX_EVENTS, 1000);
if (ready_fds == -1) {
printf("%s\n", format_log(make_system_error("epoll_wait()").what()).c_str());
break;
}
for (int i=0; i < ready_fds; i++) {
printf("ain't no way in hell do we get a ready fd (#%d) when we haven't set up anything\n", i);
}
}
}