Add a basic logcat thread

This commit is contained in:
blankie 2023-01-16 16:10:53 +07:00
parent 585c79c764
commit 2778fb039b
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
3 changed files with 58 additions and 0 deletions

39
logcat_thread.cpp Normal file
View File

@ -0,0 +1,39 @@
#include <unistd.h>
#include <sys/epoll.h>
#include "log.h"
#include "misc.h"
#include "logcat_thread.h"
#define EPOLL_MAX_EVENTS 10
LogcatThreadState::LogcatThreadState() {
this->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
if (this->epoll_fd == -1) {
throw make_system_error("epoll_create1()");
}
}
LogcatThreadState::~LogcatThreadState() {
if (close(this->epoll_fd)) {
log(std::string("Failed to close epoll file descriptor: ") + make_system_error("close()").what());
}
}
void run_logcat_thread(LogcatThreadState* state) {
struct epoll_event events[EPOLL_MAX_EVENTS];
// TODO break when run_main_loop is false
while (true) {
printf("(boop)\n");
int ready_fds = epoll_wait(state->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);
}
}
}

14
logcat_thread.h Normal file
View File

@ -0,0 +1,14 @@
#pragma once
struct LogcatThreadState {
// https://stackoverflow.com/a/2173764
LogcatThreadState(const LogcatThreadState&) = delete;
LogcatThreadState& operator=(const LogcatThreadState&) = delete;
LogcatThreadState();
~LogcatThreadState();
int epoll_fd;
};
void run_logcat_thread(LogcatThreadState* state);

View File

@ -1,4 +1,5 @@
#include <cstdio> #include <cstdio>
#include <thread>
#include <locale.h> #include <locale.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -15,6 +16,7 @@
#include "fonts.h" #include "fonts.h"
#include "config.h" #include "config.h"
#include "event_loop.h" #include "event_loop.h"
#include "logcat_thread.h"
int main(int, char**) { int main(int, char**) {
setlocale(LC_TIME, ""); setlocale(LC_TIME, "");
@ -129,6 +131,8 @@ int main(int, char**) {
// Main loop // Main loop
bool run_event_loop = true; bool run_event_loop = true;
float config_write_timer = 0.0f; float config_write_timer = 0.0f;
LogcatThreadState logcat_thread_state;
std::thread logcat_thread(run_logcat_thread, &logcat_thread_state);
while (run_event_loop) { while (run_event_loop) {
// Poll and handle events (inputs, window resize, etc.) // Poll and handle events (inputs, window resize, etc.)
@ -179,6 +183,7 @@ int main(int, char**) {
return 1; return 1;
} }
} }
logcat_thread.join();
return 0; return 0;
} }