logmeow/logcat_thread.cpp

45 lines
1.2 KiB
C++
Raw Normal View History

2023-01-16 09:10:53 +00:00
#include <unistd.h>
#include <sys/epoll.h>
#include "log.h"
#include "misc.h"
#include "logcat_thread.h"
#define EPOLL_MAX_EVENTS 10
2023-01-17 13:56:56 +00:00
LogcatThread::LogcatThread() {
this->_epoll_fd = epoll_create1(EPOLL_CLOEXEC);
if (this->_epoll_fd == -1) {
2023-01-16 09:10:53 +00:00
throw make_system_error("epoll_create1()");
}
2023-01-17 13:56:56 +00:00
this->_thread = std::thread(&LogcatThread::_run, this);
2023-01-16 09:10:53 +00:00
}
2023-01-17 13:56:56 +00:00
LogcatThread::~LogcatThread() {
if (this->_epoll_fd != -1 && close(this->_epoll_fd)) {
2023-01-16 09:10:53 +00:00
log(std::string("Failed to close epoll file descriptor: ") + make_system_error("close()").what());
}
}
2023-01-17 13:56:56 +00:00
void LogcatThread::join() {
this->_thread.join();
}
void LogcatThread::_run() {
2023-01-16 09:10:53 +00:00
struct epoll_event events[EPOLL_MAX_EVENTS];
// TODO break when run_main_loop is false
while (true) {
printf("(boop)\n");
2023-01-17 13:56:56 +00:00
int ready_fds = epoll_wait(this->_epoll_fd, events, EPOLL_MAX_EVENTS, 1000);
2023-01-16 09:10:53 +00:00
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);
}
}
}