logmeow/logcat_thread.h

44 lines
1.0 KiB
C
Raw Normal View History

2023-01-16 09:10:53 +00:00
#pragma once
2023-01-17 13:56:56 +00:00
#include <thread>
2023-01-18 16:34:17 +00:00
#include <variant>
#include "arb.h"
2023-01-19 16:34:03 +00:00
#include "logcat_entry.h"
2023-01-18 16:34:17 +00:00
2023-01-19 16:34:03 +00:00
typedef std::variant<std::string, LogcatEntry> LogcatThreadItem;
2023-01-19 05:54:20 +00:00
#define NEWLINE_BUF_SIZE 512 * 1024
2023-01-17 13:56:56 +00:00
class LogcatThread {
public:
2023-01-16 09:10:53 +00:00
// https://stackoverflow.com/a/2173764
2023-01-17 13:56:56 +00:00
LogcatThread(const LogcatThread&) = delete;
LogcatThread& operator=(const LogcatThread&) = delete;
2023-01-16 09:10:53 +00:00
2023-01-17 16:22:12 +00:00
LogcatThread();
2023-01-17 13:56:56 +00:00
~LogcatThread();
2023-01-17 16:22:12 +00:00
void request_stop();
2023-01-17 13:56:56 +00:00
void join();
2023-01-16 09:10:53 +00:00
2023-01-18 16:34:17 +00:00
AtomicRingBuffer<LogcatThreadItem> atomic_ring_buffer;
#ifndef NDEBUG
std::atomic_flag debug_log_request;
#endif
2023-01-17 13:56:56 +00:00
private:
2023-01-19 16:34:03 +00:00
void _handle_line(char* buf, size_t length, bool is_stdout);
2023-01-17 16:22:12 +00:00
void _run(std::stop_token stoken);
2023-01-16 09:10:53 +00:00
2023-01-17 13:56:56 +00:00
int _epoll_fd = -1;
2023-01-17 14:44:17 +00:00
int _stdout_read_fd = -1;
int _stdout_write_fd = -1;
int _stderr_read_fd = -1;
int _stderr_write_fd = -1;
2023-01-19 05:54:20 +00:00
char _stdout_buf[NEWLINE_BUF_SIZE];
size_t _stdout_buf_used = 0;
char _stderr_buf[NEWLINE_BUF_SIZE];
size_t _stderr_buf_used = 0;
2023-01-17 16:22:12 +00:00
std::stop_source _stop_source;
2023-01-17 13:56:56 +00:00
std::thread _thread;
};