From bda12e01120cd9503f181e8e21e8d581f106128b Mon Sep 17 00:00:00 2001 From: blankie Date: Sun, 5 Feb 2023 22:57:46 +0700 Subject: [PATCH] __restrict-ify codebase and add log wrapper for logcat thread --- config.cpp | 5 +- filters.cpp | 4 +- filters.h | 4 +- fragments/filters.cpp | 2 +- fragments/filters.h | 2 +- logcat_thread.cpp | 111 +++++++++++++++++------------------------ logcat_thread.h | 1 + main.cpp | 2 +- windows/exclusions.cpp | 2 +- windows/exclusions.h | 2 +- windows/filters.cpp | 2 +- windows/filters.h | 2 +- windows/logs.cpp | 2 +- windows/logs.h | 2 +- windows/main.cpp | 4 +- windows/main.h | 4 +- windows/settings.cpp | 2 +- windows/settings.h | 2 +- 18 files changed, 67 insertions(+), 88 deletions(-) diff --git a/config.cpp b/config.cpp index 10446f2..c10d454 100644 --- a/config.cpp +++ b/config.cpp @@ -8,7 +8,7 @@ #include "misc.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); if (!file && !(ignore_enoent && errno == ENOENT)) { 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::unique_ptr 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 = json_config.dump(); + std::string str_config = nlohmann::json(config).dump(); fwrite(str_config.data(), sizeof(char), str_config.size(), config_file.get()); config_file.reset(); diff --git a/filters.cpp b/filters.cpp index d8b378f..f5428e4 100644 --- a/filters.cpp +++ b/filters.cpp @@ -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.reserve(other.size()); @@ -288,7 +288,7 @@ static bool matches(const LogcatEntry& entry, const Filters& filters, bool retur 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); } diff --git a/filters.h b/filters.h index b097afb..3172e16 100644 --- a/filters.h +++ b/filters.h @@ -140,8 +140,8 @@ private: }; typedef std::vector>> Filters; -void copy_filters(Filters& filters, const Filters& other); -bool matches(const LogcatEntry& entry, const Filters& filters, const Filters& exclusions); +void copy_filters(Filters& __restrict filters, const Filters& __restrict other); +bool matches(const LogcatEntry& entry, const Filters& __restrict filters, const Filters& __restrict exclusions); void from_json(const nlohmann::json& j, FilterKey& key); void to_json(nlohmann::json& j, const FilterKey& key); diff --git a/fragments/filters.cpp b/fragments/filters.cpp index b08e036..4c4a621 100644 --- a/fragments/filters.cpp +++ b/fragments/filters.cpp @@ -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& logcat_entries, std::vector& filtered_logcat_entry_offsets, bool* p_open) { ImGui::TextUnformatted("You can use regex for strings by prepending \"regex:\""); diff --git a/fragments/filters.h b/fragments/filters.h index 728ec55..9c1c211 100644 --- a/fragments/filters.h +++ b/fragments/filters.h @@ -4,6 +4,6 @@ #include "../config.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& logcat_entries, std::vector& filtered_logcat_entry_offsets, bool* p_open); diff --git a/logcat_thread.cpp b/logcat_thread.cpp index 4f64c6d..2fe2666 100644 --- a/logcat_thread.cpp +++ b/logcat_thread.cpp @@ -166,52 +166,48 @@ void LogcatThread::_put_if_not_stopped(LogcatThreadItem item) { } } -void LogcatThread::_handle_line(char* buf, size_t length, bool is_stdout) { - if (!is_stdout) { - LogEntry log_entry = {time(nullptr), std::string("Received from logcat stderr: ") + std::string(buf, length)}; - print_log(log_entry); - this->_put_if_not_stopped(std::move(log_entry)); - return; - } - - std::optional logcat_entry; - try { - 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) { - this->_put_if_not_stopped(std::move(*logcat_entry)); - return; - } - std::optional new_buffer; - try { - new_buffer = try_parse_buffer(buf, length); - } catch (const std::exception& e) { - LogEntry log_entry = {time(nullptr), std::string("Failed to parse buffer line: ") + e.what()}; - print_log(log_entry); - this->_put_if_not_stopped(std::move(log_entry)); - } - if (new_buffer) { - this->_current_buffer = *new_buffer; - return; - } - - LogEntry log_entry = {time(nullptr), std::string("Cannot parse logcat stdout: ") + std::string(buf, length)}; +void LogcatThread::_try_log(std::string message) { + LogEntry log_entry = {time(nullptr), std::move(message)}; print_log(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; + } + + try { + std::optional logcat_entry = try_parse_logcat_entry(buf, length, this->_current_buffer); + if (logcat_entry) { + this->_put_if_not_stopped(std::move(*logcat_entry)); + return; + } + } catch (const std::exception& e) { + this->_try_log(std::string("Failed to parse logcat entry: ") + e.what()); + } + + try { + std::optional new_buffer = try_parse_buffer(buf, length); + if (new_buffer) { + this->_current_buffer = *new_buffer; + return; + } + } catch (const std::exception& e) { + this->_try_log(std::string("Failed to parse buffer line: ") + e.what()); + } + + this->_try_log(std::string("Cannot parse logcat stdout: ") + std::string(buf, length)); +} + void LogcatThread::_run_epoll_round() { struct epoll_event events[EPOLL_MAX_EVENTS]; int ready_fds = epoll_wait(this->_epoll_fd, events, EPOLL_MAX_EVENTS, 1000); if (ready_fds == -1) { - LogEntry log_entry = {time(nullptr), std::string("epoll_wait(): ") + strerror(errno)}; - print_log(log_entry); - this->_put_if_not_stopped(std::move(log_entry)); + int errsv = errno; // just in case if std::string overrides it + this->_try_log(std::string("epoll_wait(): ") + strerror(errsv)); return; } @@ -223,12 +219,7 @@ void LogcatThread::_run_epoll_round() { try { handle_fd(events[i].data.fd, buf, used, &LogcatThread::_handle_line, this, is_stdout); } catch (const std::exception& e) { - std::string message = "Failed to handle std"; - 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)); + this->_try_log(std::string("Failed to handle std") + (is_stdout ? "out: " : "err: ") + e.what()); } } } @@ -240,9 +231,7 @@ void LogcatThread::_try_reap(bool has_request) { try { throw_system_error("waitpid()"); } catch (const std::exception& e) { - LogEntry log_entry = {time(nullptr), e.what()}; - print_log(log_entry); - this->_put_if_not_stopped(std::move(log_entry)); + this->_try_log(e.what()); } return; } else if (res != this->_logcat_pid) { @@ -258,20 +247,14 @@ void LogcatThread::_try_reap(bool has_request) { if (WIFEXITED(wstatus)) { if (WEXITSTATUS(wstatus) && !has_request) { - LogEntry log_entry = {time(nullptr), std::string("Logcat exited with ") + std::to_string(WEXITSTATUS(wstatus))}; - print_log(log_entry); - this->_put_if_not_stopped(std::move(log_entry)); + this->_try_log(std::string("Logcat exited with ") + std::to_string(WEXITSTATUS(wstatus))); } } else if (WIFSIGNALED(wstatus)) { if (!has_request) { - LogEntry log_entry = {time(nullptr), std::string("Logcat exited with -") + std::to_string(WTERMSIG(wstatus))}; - print_log(log_entry); - this->_put_if_not_stopped(std::move(log_entry)); + this->_try_log(std::string("Logcat exited with -") + std::to_string(WTERMSIG(wstatus))); } } else { - LogEntry log_entry = {time(nullptr), std::string("Logcat disappeared (wstatus=") + std::to_string(wstatus) + ')'}; - print_log(log_entry); - this->_put_if_not_stopped(std::move(log_entry)); + this->_try_log(std::string("Logcat disappeared (wstatus=") + std::to_string(wstatus) + ')'); } } @@ -287,9 +270,7 @@ bool LogcatThread::_handle_stop_request() { try { throw_system_error("kill()"); } catch (const std::exception& e) { - LogEntry log_entry = {time(nullptr), e.what()}; - print_log(log_entry); - this->_put_if_not_stopped(std::move(log_entry)); + this->_try_log(e.what()); } return true; } @@ -321,18 +302,17 @@ bool LogcatThread::_handle_start_request() { } }; - std::string logcat_command = !this->_logcat_command->empty() - ? *this->_logcat_command - : default_logcat_command; + std::string logcat_command = *this->_logcat_command; + if (logcat_command.empty()) { + logcat_command = default_logcat_command; + } this->_logcat_pid = fork(); if (this->_logcat_pid == -1) { try { throw_system_error("fork()"); } catch (const std::exception& e) { - LogEntry log_entry = {time(nullptr), e.what()}; - print_log(log_entry); - this->_put_if_not_stopped(std::move(log_entry)); + this->_try_log(e.what()); } return true; } 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) { #ifndef NDEBUG if (this->debug_log_request.test()) { - LogEntry log_entry = {time(nullptr), "A log entry from the logcat thread :D"}; - this->_put_if_not_stopped(std::move(log_entry)); + this->_try_log("A log entry from the logcat thread :D"); this->debug_log_request.clear(); } #endif diff --git a/logcat_thread.h b/logcat_thread.h index 4636a32..0059f06 100644 --- a/logcat_thread.h +++ b/logcat_thread.h @@ -35,6 +35,7 @@ public: private: 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 _run(std::stop_token stoken); void _run_epoll_round(); diff --git a/main.cpp b/main.cpp index aecfa2c..071b2e9 100644 --- a/main.cpp +++ b/main.cpp @@ -20,7 +20,7 @@ #include "event_loop.h" #include "logcat_thread.h" -int main(int, char**) { +int main() { setlocale(LC_TIME, ""); Config config; diff --git a/windows/exclusions.cpp b/windows/exclusions.cpp index 338da6f..b6c0157 100644 --- a/windows/exclusions.cpp +++ b/windows/exclusions.cpp @@ -6,7 +6,7 @@ #include "../config.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& logcat_entries, std::vector& filtered_logcat_entry_offsets, bool* p_open) { if (!ImGui::BeginWithCloseShortcut("Exclusions", p_open)) { diff --git a/windows/exclusions.h b/windows/exclusions.h index cb97c43..59d33b6 100644 --- a/windows/exclusions.h +++ b/windows/exclusions.h @@ -5,6 +5,6 @@ #include "../logcat_entry.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& logcat_entries, std::vector& filtered_logcat_entry_offsets, bool* p_open); diff --git a/windows/filters.cpp b/windows/filters.cpp index 3b9ced7..4d75888 100644 --- a/windows/filters.cpp +++ b/windows/filters.cpp @@ -6,7 +6,7 @@ #include "../config.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& logcat_entries, std::vector& filtered_logcat_entry_offsets, bool* p_open) { if (!ImGui::BeginWithCloseShortcut("Filters", p_open)) { diff --git a/windows/filters.h b/windows/filters.h index 823597c..28f6227 100644 --- a/windows/filters.h +++ b/windows/filters.h @@ -5,6 +5,6 @@ #include "../logcat_entry.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& logcat_entries, std::vector& filtered_logcat_entry_offsets, bool* p_open); diff --git a/windows/logs.cpp b/windows/logs.cpp index d743962..ba4d2da 100644 --- a/windows/logs.cpp +++ b/windows/logs.cpp @@ -43,7 +43,7 @@ static inline void render_table(ImFont* monospace_font, bool* autoscrolling) { 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)) { ImGui::End(); return; diff --git a/windows/logs.h b/windows/logs.h index 5b05350..dde1776 100644 --- a/windows/logs.h +++ b/windows/logs.h @@ -2,4 +2,4 @@ #include -void logs_window(ImFont* monospace_font, bool* autoscrolling, bool* p_open); +void logs_window(ImFont* monospace_font, bool* __restrict autoscrolling, bool* __restrict p_open); diff --git a/windows/main.cpp b/windows/main.cpp index 314b5d5..97da8e4 100644 --- a/windows/main.cpp +++ b/windows/main.cpp @@ -56,8 +56,8 @@ static inline void render_table(ImFont* monospace_font, std::vector void main_window(bool latest_log_entries_read, ImFont* monospace_font, LogcatThread& logcat_thread, std::vector& logcat_entries, std::vector& filtered_logcat_entry_offsets, - const Config& active_config, Config& inactive_config, - bool* show_settings_window, bool* show_filters_window, bool* show_exclusions_window, bool* show_logs_window) { + const Config& __restrict active_config, Config& __restrict inactive_config, + 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::SetNextWindowSize(ImGui::GetMainViewport()->WorkSize); diff --git a/windows/main.h b/windows/main.h index 25b0dc9..6fcd2c9 100644 --- a/windows/main.h +++ b/windows/main.h @@ -9,5 +9,5 @@ void main_window(bool latest_log_entries_read, ImFont* monospace_font, LogcatThread& logcat_thread, std::vector& logcat_entries, std::vector& filtered_logcat_entry_offsets, - const Config& active_config, Config& inactive_config, - bool* show_settings_window, bool* show_filters_window, bool* show_exclusions_window, bool* show_logs_window); + const Config& __restrict active_config, Config& __restrict inactive_config, + bool* __restrict show_settings_window, bool* __restrict show_filters_window, bool* __restrict show_exclusions_window, bool* __restrict show_logs_window); diff --git a/windows/settings.cpp b/windows/settings.cpp index d5a6c52..58b51cd 100644 --- a/windows/settings.cpp +++ b/windows/settings.cpp @@ -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)) { ImGui::End(); return; diff --git a/windows/settings.h b/windows/settings.h index 557575a..078afe4 100644 --- a/windows/settings.h +++ b/windows/settings.h @@ -2,4 +2,4 @@ #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);