logmeow/logcat_thread.h

67 lines
1.7 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-29 08:21:22 +00:00
typedef std::variant<LogEntry, LogcatEntry> LogcatThreadItem;
2023-01-19 05:54:20 +00:00
#define NEWLINE_BUF_SIZE 512 * 1024
2023-01-17 13:56:56 +00:00
2023-02-02 12:50:28 +00:00
enum class LogcatProcessRequest {
None,
Start,
Stop,
};
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-02-02 12:50:28 +00:00
LogcatThread(const std::string* logcat_command);
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;
2023-02-02 12:50:28 +00:00
std::atomic<LogcatProcessRequest> logcat_process_request = LogcatProcessRequest::None;
std::atomic_flag logcat_process_running;
2023-01-18 16:34:17 +00:00
#ifndef NDEBUG
std::atomic_flag debug_log_request;
#endif
2023-01-17 13:56:56 +00:00
private:
2023-01-21 07:51:23 +00:00
void _put_if_not_stopped(LogcatThreadItem item);
void _try_log(std::string message);
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-02-02 12:50:28 +00:00
void _run_epoll_round();
void _try_reap(bool stop_requested);
bool _handle_stop_request();
bool _handle_start_request();
bool _run_process_round(LogcatProcessRequest request);
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-02-02 12:50:28 +00:00
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-02-02 12:50:28 +00:00
pid_t _logcat_pid = -1;
char _logcat_process_kill_attempts = 0;
const std::string* _logcat_command;
2023-01-20 14:41:06 +00:00
Buffer _current_buffer = Buffer::Unknown;
2023-01-17 16:22:12 +00:00
std::stop_source _stop_source;
2023-01-17 13:56:56 +00:00
std::thread _thread;
};