__restrict-ify codebase and add log wrapper for logcat thread
This commit is contained in:
		
							parent
							
								
									fd2652b16e
								
							
						
					
					
						commit
						bda12e0112
					
				|  | @ -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<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 = 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(); | ||||
| 
 | ||||
|  |  | |||
|  | @ -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); | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -140,8 +140,8 @@ private: | |||
| }; | ||||
| 
 | ||||
| typedef std::vector<std::pair<std::string, std::unique_ptr<Filter>>> 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); | ||||
|  |  | |||
|  | @ -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, | ||||
|         bool* p_open) { | ||||
|     ImGui::TextUnformatted("You can use regex for strings by prepending \"regex:\""); | ||||
|  |  | |||
|  | @ -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<LogcatEntry>& logcat_entries, std::vector<size_t>& filtered_logcat_entry_offsets, | ||||
|         bool* p_open); | ||||
|  |  | |||
|  | @ -166,42 +166,39 @@ 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)}; | ||||
| 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; | ||||
|     } | ||||
| 
 | ||||
|     std::optional<LogcatEntry> 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)); | ||||
|     } | ||||
|         std::optional<LogcatEntry> logcat_entry = try_parse_logcat_entry(buf, length, this->_current_buffer); | ||||
|         if (logcat_entry) { | ||||
|             this->_put_if_not_stopped(std::move(*logcat_entry)); | ||||
|             return; | ||||
|         } | ||||
|     std::optional<Buffer> 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)); | ||||
|         this->_try_log(std::string("Failed to parse logcat entry: ") + e.what()); | ||||
|     } | ||||
| 
 | ||||
|     try { | ||||
|         std::optional<Buffer> 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()); | ||||
|     } | ||||
| 
 | ||||
|     LogEntry log_entry = {time(nullptr), std::string("Cannot parse logcat stdout: ") + std::string(buf, length)}; | ||||
|     print_log(log_entry); | ||||
|     this->_put_if_not_stopped(std::move(log_entry)); | ||||
|     this->_try_log(std::string("Cannot parse logcat stdout: ") + std::string(buf, length)); | ||||
| } | ||||
| 
 | ||||
| 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); | ||||
|     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 | ||||
|  |  | |||
|  | @ -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(); | ||||
|  |  | |||
							
								
								
									
										2
									
								
								main.cpp
								
								
								
								
							
							
						
						
									
										2
									
								
								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; | ||||
|  |  | |||
|  | @ -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<LogcatEntry>& logcat_entries, std::vector<size_t>& filtered_logcat_entry_offsets, | ||||
|         bool* p_open) { | ||||
|     if (!ImGui::BeginWithCloseShortcut("Exclusions", p_open)) { | ||||
|  |  | |||
|  | @ -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<LogcatEntry>& logcat_entries, std::vector<size_t>& filtered_logcat_entry_offsets, | ||||
|         bool* p_open); | ||||
|  |  | |||
|  | @ -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<LogcatEntry>& logcat_entries, std::vector<size_t>& filtered_logcat_entry_offsets, | ||||
|         bool* p_open) { | ||||
|     if (!ImGui::BeginWithCloseShortcut("Filters", p_open)) { | ||||
|  |  | |||
|  | @ -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<LogcatEntry>& logcat_entries, std::vector<size_t>& filtered_logcat_entry_offsets, | ||||
|         bool* p_open); | ||||
|  |  | |||
|  | @ -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; | ||||
|  |  | |||
|  | @ -2,4 +2,4 @@ | |||
| 
 | ||||
| #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); | ||||
|  |  | |||
|  | @ -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, | ||||
|         std::vector<LogcatEntry>& logcat_entries, std::vector<size_t>& 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); | ||||
|  |  | |||
|  | @ -9,5 +9,5 @@ | |||
| 
 | ||||
| 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, | ||||
|         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); | ||||
|  |  | |||
|  | @ -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; | ||||
|  |  | |||
|  | @ -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); | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue