From 2778fb039bc39588979a25acd6561efa15355622 Mon Sep 17 00:00:00 2001 From: blankie Date: Mon, 16 Jan 2023 16:10:53 +0700 Subject: [PATCH] Add a basic logcat thread --- logcat_thread.cpp | 39 +++++++++++++++++++++++++++++++++++++++ logcat_thread.h | 14 ++++++++++++++ main.cpp | 5 +++++ 3 files changed, 58 insertions(+) create mode 100644 logcat_thread.cpp create mode 100644 logcat_thread.h diff --git a/logcat_thread.cpp b/logcat_thread.cpp new file mode 100644 index 0000000..c6b4b48 --- /dev/null +++ b/logcat_thread.cpp @@ -0,0 +1,39 @@ +#include +#include + +#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); + } + } +} diff --git a/logcat_thread.h b/logcat_thread.h new file mode 100644 index 0000000..84098f5 --- /dev/null +++ b/logcat_thread.h @@ -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); diff --git a/main.cpp b/main.cpp index 6b2ec33..26fb1f7 100644 --- a/main.cpp +++ b/main.cpp @@ -1,4 +1,5 @@ #include +#include #include #include @@ -15,6 +16,7 @@ #include "fonts.h" #include "config.h" #include "event_loop.h" +#include "logcat_thread.h" int main(int, char**) { setlocale(LC_TIME, ""); @@ -129,6 +131,8 @@ int main(int, char**) { // Main loop bool run_event_loop = true; float config_write_timer = 0.0f; + LogcatThreadState logcat_thread_state; + std::thread logcat_thread(run_logcat_thread, &logcat_thread_state); while (run_event_loop) { // Poll and handle events (inputs, window resize, etc.) @@ -179,6 +183,7 @@ int main(int, char**) { return 1; } } + logcat_thread.join(); return 0; }