__restrict-ify codebase and add log wrapper for logcat thread

This commit is contained in:
blankie 2023-02-05 22:57:46 +07:00
parent fd2652b16e
commit bda12e0112
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
18 changed files with 67 additions and 88 deletions

View File

@ -8,7 +8,7 @@
#include "misc.h" #include "misc.h"
#include "config.h" #include "config.h"
static FILE* fopen_or_raise(const char* path, const char* mode, bool ignore_enoent) { static FILE* fopen_or_raise(const char* __restrict path, const char* __restrict mode, bool ignore_enoent) {
FILE* file = fopen(path, mode); FILE* file = fopen(path, mode);
if (!file && !(ignore_enoent && errno == ENOENT)) { if (!file && !(ignore_enoent && errno == ENOENT)) {
throw_system_error(std::string("fopen(") + quote(path) + ')'); throw_system_error(std::string("fopen(") + quote(path) + ')');
@ -99,8 +99,7 @@ void write_config(const Config& config) {
std::string tmp_config_file_path = config_file_path + ".tmp"; std::string tmp_config_file_path = config_file_path + ".tmp";
std::unique_ptr<FILE, decltype(&fclose_and_log)> config_file(fopen_or_raise(tmp_config_file_path.c_str(), "w", false), fclose_and_log); std::unique_ptr<FILE, decltype(&fclose_and_log)> config_file(fopen_or_raise(tmp_config_file_path.c_str(), "w", false), fclose_and_log);
nlohmann::json json_config = config; std::string str_config = nlohmann::json(config).dump();
std::string str_config = json_config.dump();
fwrite(str_config.data(), sizeof(char), str_config.size(), config_file.get()); fwrite(str_config.data(), sizeof(char), str_config.size(), config_file.get());
config_file.reset(); config_file.reset();

View File

@ -265,7 +265,7 @@ bool GroupFilter::match(const LogcatEntry& entry) const {
} }
void copy_filters(Filters& filters, const Filters& other) { void copy_filters(Filters& __restrict filters, const Filters& __restrict other) {
filters.clear(); filters.clear();
filters.reserve(other.size()); filters.reserve(other.size());
@ -288,7 +288,7 @@ static bool matches(const LogcatEntry& entry, const Filters& filters, bool retur
return return_true_if_empty; return return_true_if_empty;
} }
bool matches(const LogcatEntry& entry, const Filters& filters, const Filters& exclusions) { bool matches(const LogcatEntry& entry, const Filters& __restrict filters, const Filters& __restrict exclusions) {
return !matches(entry, exclusions, false) && matches(entry, filters, true); return !matches(entry, exclusions, false) && matches(entry, filters, true);
} }

View File

@ -140,8 +140,8 @@ private:
}; };
typedef std::vector<std::pair<std::string, std::unique_ptr<Filter>>> Filters; typedef std::vector<std::pair<std::string, std::unique_ptr<Filter>>> Filters;
void copy_filters(Filters& filters, const Filters& other); void copy_filters(Filters& __restrict filters, const Filters& __restrict other);
bool matches(const LogcatEntry& entry, const Filters& filters, const Filters& exclusions); bool matches(const LogcatEntry& entry, const Filters& __restrict filters, const Filters& __restrict exclusions);
void from_json(const nlohmann::json& j, FilterKey& key); void from_json(const nlohmann::json& j, FilterKey& key);
void to_json(nlohmann::json& j, const FilterKey& key); void to_json(nlohmann::json& j, const FilterKey& key);

View File

@ -220,7 +220,7 @@ static void try_write_config(const Config& config) {
} }
} }
void filters_fragment(Config& active_config, Filters& active_filters, Filters& inactive_filters, void filters_fragment(Config& active_config, Filters& __restrict active_filters, Filters& __restrict inactive_filters,
const std::vector<LogcatEntry>& logcat_entries, std::vector<size_t>& filtered_logcat_entry_offsets, const std::vector<LogcatEntry>& logcat_entries, std::vector<size_t>& filtered_logcat_entry_offsets,
bool* p_open) { bool* p_open) {
ImGui::TextUnformatted("You can use regex for strings by prepending \"regex:\""); ImGui::TextUnformatted("You can use regex for strings by prepending \"regex:\"");

View File

@ -4,6 +4,6 @@
#include "../config.h" #include "../config.h"
#include "../filters.h" #include "../filters.h"
void filters_fragment(Config& active_config, Filters& active_filters, Filters& inactive_filters, void filters_fragment(Config& active_config, Filters& __restrict active_filters, Filters& __restrict inactive_filters,
const std::vector<LogcatEntry>& logcat_entries, std::vector<size_t>& filtered_logcat_entry_offsets, const std::vector<LogcatEntry>& logcat_entries, std::vector<size_t>& filtered_logcat_entry_offsets,
bool* p_open); bool* p_open);

View File

@ -166,42 +166,39 @@ void LogcatThread::_put_if_not_stopped(LogcatThreadItem item) {
} }
} }
void LogcatThread::_handle_line(char* buf, size_t length, bool is_stdout) { void LogcatThread::_try_log(std::string message) {
if (!is_stdout) { LogEntry log_entry = {time(nullptr), std::move(message)};
LogEntry log_entry = {time(nullptr), std::string("Received from logcat stderr: ") + std::string(buf, length)};
print_log(log_entry); print_log(log_entry);
this->_put_if_not_stopped(std::move(log_entry)); this->_put_if_not_stopped(std::move(log_entry));
}
void LogcatThread::_handle_line(char* buf, size_t length, bool is_stdout) {
if (!is_stdout) {
this->_try_log(std::string("Received from logcat stderr: ") + std::string(buf, length));
return; return;
} }
std::optional<LogcatEntry> logcat_entry;
try { try {
logcat_entry = try_parse_logcat_entry(buf, length, this->_current_buffer); std::optional<LogcatEntry> logcat_entry = try_parse_logcat_entry(buf, length, this->_current_buffer);
} catch (const std::exception& e) {
LogEntry log_entry = {time(nullptr), std::string("Failed to parse logcat entry: ") + e.what()};
print_log(log_entry);
this->_put_if_not_stopped(std::move(log_entry));
}
if (logcat_entry) { if (logcat_entry) {
this->_put_if_not_stopped(std::move(*logcat_entry)); this->_put_if_not_stopped(std::move(*logcat_entry));
return; return;
} }
std::optional<Buffer> new_buffer;
try {
new_buffer = try_parse_buffer(buf, length);
} catch (const std::exception& e) { } catch (const std::exception& e) {
LogEntry log_entry = {time(nullptr), std::string("Failed to parse buffer line: ") + e.what()}; this->_try_log(std::string("Failed to parse logcat entry: ") + e.what());
print_log(log_entry);
this->_put_if_not_stopped(std::move(log_entry));
} }
try {
std::optional<Buffer> new_buffer = try_parse_buffer(buf, length);
if (new_buffer) { if (new_buffer) {
this->_current_buffer = *new_buffer; this->_current_buffer = *new_buffer;
return; return;
} }
} catch (const std::exception& e) {
this->_try_log(std::string("Failed to parse buffer line: ") + e.what());
}
LogEntry log_entry = {time(nullptr), std::string("Cannot parse logcat stdout: ") + std::string(buf, length)}; this->_try_log(std::string("Cannot parse logcat stdout: ") + std::string(buf, length));
print_log(log_entry);
this->_put_if_not_stopped(std::move(log_entry));
} }
void LogcatThread::_run_epoll_round() { void LogcatThread::_run_epoll_round() {
@ -209,9 +206,8 @@ void LogcatThread::_run_epoll_round() {
int ready_fds = epoll_wait(this->_epoll_fd, events, EPOLL_MAX_EVENTS, 1000); int ready_fds = epoll_wait(this->_epoll_fd, events, EPOLL_MAX_EVENTS, 1000);
if (ready_fds == -1) { if (ready_fds == -1) {
LogEntry log_entry = {time(nullptr), std::string("epoll_wait(): ") + strerror(errno)}; int errsv = errno; // just in case if std::string overrides it
print_log(log_entry); this->_try_log(std::string("epoll_wait(): ") + strerror(errsv));
this->_put_if_not_stopped(std::move(log_entry));
return; return;
} }
@ -223,12 +219,7 @@ void LogcatThread::_run_epoll_round() {
try { try {
handle_fd(events[i].data.fd, buf, used, &LogcatThread::_handle_line, this, is_stdout); handle_fd(events[i].data.fd, buf, used, &LogcatThread::_handle_line, this, is_stdout);
} catch (const std::exception& e) { } catch (const std::exception& e) {
std::string message = "Failed to handle std"; this->_try_log(std::string("Failed to handle std") + (is_stdout ? "out: " : "err: ") + e.what());
message += is_stdout ? "out: " : "err: ";
message += e.what();
LogEntry log_entry = {time(nullptr), std::move(message)};
print_log(log_entry);
this->_put_if_not_stopped(std::move(log_entry));
} }
} }
} }
@ -240,9 +231,7 @@ void LogcatThread::_try_reap(bool has_request) {
try { try {
throw_system_error("waitpid()"); throw_system_error("waitpid()");
} catch (const std::exception& e) { } catch (const std::exception& e) {
LogEntry log_entry = {time(nullptr), e.what()}; this->_try_log(e.what());
print_log(log_entry);
this->_put_if_not_stopped(std::move(log_entry));
} }
return; return;
} else if (res != this->_logcat_pid) { } else if (res != this->_logcat_pid) {
@ -258,20 +247,14 @@ void LogcatThread::_try_reap(bool has_request) {
if (WIFEXITED(wstatus)) { if (WIFEXITED(wstatus)) {
if (WEXITSTATUS(wstatus) && !has_request) { if (WEXITSTATUS(wstatus) && !has_request) {
LogEntry log_entry = {time(nullptr), std::string("Logcat exited with ") + std::to_string(WEXITSTATUS(wstatus))}; this->_try_log(std::string("Logcat exited with ") + std::to_string(WEXITSTATUS(wstatus)));
print_log(log_entry);
this->_put_if_not_stopped(std::move(log_entry));
} }
} else if (WIFSIGNALED(wstatus)) { } else if (WIFSIGNALED(wstatus)) {
if (!has_request) { if (!has_request) {
LogEntry log_entry = {time(nullptr), std::string("Logcat exited with -") + std::to_string(WTERMSIG(wstatus))}; this->_try_log(std::string("Logcat exited with -") + std::to_string(WTERMSIG(wstatus)));
print_log(log_entry);
this->_put_if_not_stopped(std::move(log_entry));
} }
} else { } else {
LogEntry log_entry = {time(nullptr), std::string("Logcat disappeared (wstatus=") + std::to_string(wstatus) + ')'}; this->_try_log(std::string("Logcat disappeared (wstatus=") + std::to_string(wstatus) + ')');
print_log(log_entry);
this->_put_if_not_stopped(std::move(log_entry));
} }
} }
@ -287,9 +270,7 @@ bool LogcatThread::_handle_stop_request() {
try { try {
throw_system_error("kill()"); throw_system_error("kill()");
} catch (const std::exception& e) { } catch (const std::exception& e) {
LogEntry log_entry = {time(nullptr), e.what()}; this->_try_log(e.what());
print_log(log_entry);
this->_put_if_not_stopped(std::move(log_entry));
} }
return true; return true;
} }
@ -321,18 +302,17 @@ bool LogcatThread::_handle_start_request() {
} }
}; };
std::string logcat_command = !this->_logcat_command->empty() std::string logcat_command = *this->_logcat_command;
? *this->_logcat_command if (logcat_command.empty()) {
: default_logcat_command; logcat_command = default_logcat_command;
}
this->_logcat_pid = fork(); this->_logcat_pid = fork();
if (this->_logcat_pid == -1) { if (this->_logcat_pid == -1) {
try { try {
throw_system_error("fork()"); throw_system_error("fork()");
} catch (const std::exception& e) { } catch (const std::exception& e) {
LogEntry log_entry = {time(nullptr), e.what()}; this->_try_log(e.what());
print_log(log_entry);
this->_put_if_not_stopped(std::move(log_entry));
} }
return true; return true;
} else if (this->_logcat_pid == 0) { } else if (this->_logcat_pid == 0) {
@ -374,8 +354,7 @@ void LogcatThread::_run(std::stop_token stoken) {
while (!stoken.stop_requested() || this->_logcat_pid != -1) { while (!stoken.stop_requested() || this->_logcat_pid != -1) {
#ifndef NDEBUG #ifndef NDEBUG
if (this->debug_log_request.test()) { if (this->debug_log_request.test()) {
LogEntry log_entry = {time(nullptr), "A log entry from the logcat thread :D"}; this->_try_log("A log entry from the logcat thread :D");
this->_put_if_not_stopped(std::move(log_entry));
this->debug_log_request.clear(); this->debug_log_request.clear();
} }
#endif #endif

View File

@ -35,6 +35,7 @@ public:
private: private:
void _put_if_not_stopped(LogcatThreadItem item); void _put_if_not_stopped(LogcatThreadItem item);
void _try_log(std::string message);
void _handle_line(char* buf, size_t length, bool is_stdout); void _handle_line(char* buf, size_t length, bool is_stdout);
void _run(std::stop_token stoken); void _run(std::stop_token stoken);
void _run_epoll_round(); void _run_epoll_round();

View File

@ -20,7 +20,7 @@
#include "event_loop.h" #include "event_loop.h"
#include "logcat_thread.h" #include "logcat_thread.h"
int main(int, char**) { int main() {
setlocale(LC_TIME, ""); setlocale(LC_TIME, "");
Config config; Config config;

View File

@ -6,7 +6,7 @@
#include "../config.h" #include "../config.h"
#include "exclusions.h" #include "exclusions.h"
void exclusions_window(Config& active_config, Config& inactive_config, void exclusions_window(Config& __restrict active_config, Config& __restrict inactive_config,
const std::vector<LogcatEntry>& logcat_entries, std::vector<size_t>& filtered_logcat_entry_offsets, const std::vector<LogcatEntry>& logcat_entries, std::vector<size_t>& filtered_logcat_entry_offsets,
bool* p_open) { bool* p_open) {
if (!ImGui::BeginWithCloseShortcut("Exclusions", p_open)) { if (!ImGui::BeginWithCloseShortcut("Exclusions", p_open)) {

View File

@ -5,6 +5,6 @@
#include "../logcat_entry.h" #include "../logcat_entry.h"
#include "../config.h" #include "../config.h"
void exclusions_window(Config& active_config, Config& inactive_config, void exclusions_window(Config& __restrict active_config, Config& __restrict inactive_config,
const std::vector<LogcatEntry>& logcat_entries, std::vector<size_t>& filtered_logcat_entry_offsets, const std::vector<LogcatEntry>& logcat_entries, std::vector<size_t>& filtered_logcat_entry_offsets,
bool* p_open); bool* p_open);

View File

@ -6,7 +6,7 @@
#include "../config.h" #include "../config.h"
#include "filters.h" #include "filters.h"
void filters_window(Config& active_config, Config& inactive_config, void filters_window(Config& __restrict active_config, Config& __restrict inactive_config,
const std::vector<LogcatEntry>& logcat_entries, std::vector<size_t>& filtered_logcat_entry_offsets, const std::vector<LogcatEntry>& logcat_entries, std::vector<size_t>& filtered_logcat_entry_offsets,
bool* p_open) { bool* p_open) {
if (!ImGui::BeginWithCloseShortcut("Filters", p_open)) { if (!ImGui::BeginWithCloseShortcut("Filters", p_open)) {

View File

@ -5,6 +5,6 @@
#include "../logcat_entry.h" #include "../logcat_entry.h"
#include "../config.h" #include "../config.h"
void filters_window(Config& active_config, Config& inactive_config, void filters_window(Config& __restrict active_config, Config& __restrict inactive_config,
const std::vector<LogcatEntry>& logcat_entries, std::vector<size_t>& filtered_logcat_entry_offsets, const std::vector<LogcatEntry>& logcat_entries, std::vector<size_t>& filtered_logcat_entry_offsets,
bool* p_open); bool* p_open);

View File

@ -43,7 +43,7 @@ static inline void render_table(ImFont* monospace_font, bool* autoscrolling) {
ImGui::EndTable(); ImGui::EndTable();
} }
void logs_window(ImFont* monospace_font, bool* autoscrolling, bool* p_open) { void logs_window(ImFont* monospace_font, bool* __restrict autoscrolling, bool* __restrict p_open) {
if (!ImGui::BeginWithCloseShortcut("LogMeow Logs", p_open)) { if (!ImGui::BeginWithCloseShortcut("LogMeow Logs", p_open)) {
ImGui::End(); ImGui::End();
return; return;

View File

@ -2,4 +2,4 @@
#include <imgui.h> #include <imgui.h>
void logs_window(ImFont* monospace_font, bool* autoscrolling, bool* p_open); void logs_window(ImFont* monospace_font, bool* __restrict autoscrolling, bool* __restrict p_open);

View File

@ -56,8 +56,8 @@ static inline void render_table(ImFont* monospace_font, std::vector<LogcatEntry>
void main_window(bool latest_log_entries_read, ImFont* monospace_font, LogcatThread& logcat_thread, void main_window(bool latest_log_entries_read, ImFont* monospace_font, LogcatThread& logcat_thread,
std::vector<LogcatEntry>& logcat_entries, std::vector<size_t>& filtered_logcat_entry_offsets, std::vector<LogcatEntry>& logcat_entries, std::vector<size_t>& filtered_logcat_entry_offsets,
const Config& active_config, Config& inactive_config, const Config& __restrict active_config, Config& __restrict inactive_config,
bool* show_settings_window, bool* show_filters_window, bool* show_exclusions_window, bool* show_logs_window) { bool* __restrict show_settings_window, bool* __restrict show_filters_window, bool* __restrict show_exclusions_window, bool* __restrict show_logs_window) {
ImGui::SetNextWindowPos(ImGui::GetMainViewport()->WorkPos); ImGui::SetNextWindowPos(ImGui::GetMainViewport()->WorkPos);
ImGui::SetNextWindowSize(ImGui::GetMainViewport()->WorkSize); ImGui::SetNextWindowSize(ImGui::GetMainViewport()->WorkSize);

View File

@ -9,5 +9,5 @@
void main_window(bool latest_log_entries_read, ImFont* monospace_font, LogcatThread& logcat_thread, void main_window(bool latest_log_entries_read, ImFont* monospace_font, LogcatThread& logcat_thread,
std::vector<LogcatEntry>& logcat_entries, std::vector<size_t>& filtered_logcat_entry_offsets, std::vector<LogcatEntry>& logcat_entries, std::vector<size_t>& filtered_logcat_entry_offsets,
const Config& active_config, Config& inactive_config, const Config& __restrict active_config, Config& __restrict inactive_config,
bool* show_settings_window, bool* show_filters_window, bool* show_exclusions_window, bool* show_logs_window); bool* __restrict show_settings_window, bool* __restrict show_filters_window, bool* __restrict show_exclusions_window, bool* __restrict show_logs_window);

View File

@ -13,7 +13,7 @@ static void try_write_config(const Config& config) {
} }
} }
void settings_window(Config& active_config, Config& inactive_config, bool* p_open) { void settings_window(Config& __restrict active_config, Config& __restrict inactive_config, bool* p_open) {
if (!ImGui::BeginWithCloseShortcut("Settings", p_open)) { if (!ImGui::BeginWithCloseShortcut("Settings", p_open)) {
ImGui::End(); ImGui::End();
return; return;

View File

@ -2,4 +2,4 @@
#include "../config.h" #include "../config.h"
void settings_window(Config& active_config, Config& inactive_config, bool* p_open); void settings_window(Config& __restrict active_config, Config& __restrict inactive_config, bool* p_open);