logmeow/logcat_thread.h

47 lines
1.2 KiB
C++

#pragma once
#include <thread>
#include <variant>
#include "arb.h"
#include "logcat_entry.h"
typedef std::variant<LogEntry, LogcatEntry> LogcatThreadItem;
#define NEWLINE_BUF_SIZE 512 * 1024
class LogcatThread {
public:
// https://stackoverflow.com/a/2173764
LogcatThread(const LogcatThread&) = delete;
LogcatThread& operator=(const LogcatThread&) = delete;
LogcatThread();
~LogcatThread();
void request_stop();
void join();
AtomicRingBuffer<LogcatThreadItem> atomic_ring_buffer;
#ifndef NDEBUG
std::atomic_flag debug_log_request;
#endif
private:
void _put_if_not_stopped(LogcatThreadItem item);
void _handle_line(char* buf, size_t length, bool is_stdout);
void _run_epoll_round();
void _run(std::stop_token stoken);
int _epoll_fd = -1;
int _stdout_read_fd = -1;
int _stdout_write_fd = -1;
int _stderr_read_fd = -1;
int _stderr_write_fd = -1;
char _stdout_buf[NEWLINE_BUF_SIZE];
size_t _stdout_buf_used = 0;
char _stderr_buf[NEWLINE_BUF_SIZE];
size_t _stderr_buf_used = 0;
Buffer _current_buffer = Buffer::Unknown;
std::stop_source _stop_source;
std::thread _thread;
};