v2.0.4-rc (#37)
	
		
			
	
		
	
	
		
	
		
			All checks were successful
		
		
	
	
		
			
				
	
				BlockStorage/repertory/pipeline/head This commit looks good
				
			
		
		
	
	
				
					
				
			
		
			All checks were successful
		
		
	
	BlockStorage/repertory/pipeline/head This commit looks good
				
			# Changelog ## v2.0.4-rc ### BREAKING CHANGES * `renterd` v2.0.0+ is now required. Prior versions will fail to mount. ### Issues * \#35 [bug] Low frequency check is set to '0' instead of 1 hour by default * \#36 [bug] Max cache size bytes is set to '0' by default ### Changes from v2.0.3-rc * Added Sia API version check prior to mounting * Added back `-cv` (check version) CLI option * Continue documentation updates * Fixed setting `ApiAuth` via `set_value_by_name` * Fixed setting `HostConfig.ApiUser` via `set_value_by_name` * Fixed setting `HostConfig.Path` via `set_value_by_name` * Fixed setting `HostConfig.Protocol` via `set_value_by_name` * Improved ring buffer read-ahead * Integrated `renterd` version 2.0.0 * Prefer using local cache file when opening files * Refactored `app_config` unit tests * Refactored polling to be more accurate on scheduling tasks Reviewed-on: #37
This commit is contained in:
		| @@ -22,9 +22,6 @@ | ||||
| #include "utils/error.hpp" | ||||
|  | ||||
| namespace repertory::utils::error { | ||||
| std::atomic<const i_exception_handler *> exception_handler{ | ||||
|     &default_exception_handler}; | ||||
|  | ||||
| auto create_error_message(std::vector<std::string_view> items) -> std::string { | ||||
|   std::stringstream stream{}; | ||||
|   for (std::size_t idx = 0U; idx < items.size(); ++idx) { | ||||
| @@ -38,13 +35,30 @@ auto create_error_message(std::vector<std::string_view> items) -> std::string { | ||||
|   return stream.str(); | ||||
| } | ||||
|  | ||||
| auto create_error_message(std::string_view function_name, | ||||
|                           std::vector<std::string_view> items) -> std::string { | ||||
|   items.insert(items.begin(), function_name); | ||||
|   return create_error_message(items); | ||||
| } | ||||
|  | ||||
| auto create_exception(std::string_view function_name, | ||||
|                       std::vector<std::string_view> items) | ||||
|     -> std::runtime_error { | ||||
|   items.insert(items.begin(), function_name); | ||||
|   return std::runtime_error(create_error_message(items)); | ||||
|   return std::runtime_error(create_error_message(function_name, items)); | ||||
| } | ||||
|  | ||||
| #if defined(PROJECT_ENABLE_V2_ERRORS) | ||||
| void handle_debug(std::string_view function_name, std::string_view msg) { | ||||
|   const i_exception_handler *handler{exception_handler}; | ||||
|   if (handler != nullptr) { | ||||
|     handler->handle_debug(function_name, msg); | ||||
|     return; | ||||
|   } | ||||
|  | ||||
|   default_exception_handler.handle_debug(function_name, msg); | ||||
| } | ||||
| #endif // defined(PROJECT_ENABLE_V2_ERRORS) | ||||
|  | ||||
| void handle_error(std::string_view function_name, std::string_view msg) { | ||||
|   const i_exception_handler *handler{exception_handler}; | ||||
|   if (handler != nullptr) { | ||||
| @@ -76,6 +90,38 @@ void handle_exception(std::string_view function_name, | ||||
|   default_exception_handler.handle_exception(function_name, ex); | ||||
| } | ||||
|  | ||||
| #if defined(PROJECT_ENABLE_V2_ERRORS) | ||||
| void handle_info(std::string_view function_name, std::string_view msg) { | ||||
|   const i_exception_handler *handler{exception_handler}; | ||||
|   if (handler != nullptr) { | ||||
|     handler->handle_info(function_name, msg); | ||||
|     return; | ||||
|   } | ||||
|  | ||||
|   default_exception_handler.handle_info(function_name, msg); | ||||
| } | ||||
|  | ||||
| void handle_trace(std::string_view function_name, std::string_view msg) { | ||||
|   const i_exception_handler *handler{exception_handler}; | ||||
|   if (handler != nullptr) { | ||||
|     handler->handle_trace(function_name, msg); | ||||
|     return; | ||||
|   } | ||||
|  | ||||
|   default_exception_handler.handle_trace(function_name, msg); | ||||
| } | ||||
|  | ||||
| void handle_warn(std::string_view function_name, std::string_view msg) { | ||||
|   const i_exception_handler *handler{exception_handler}; | ||||
|   if (handler != nullptr) { | ||||
|     handler->handle_warn(function_name, msg); | ||||
|     return; | ||||
|   } | ||||
|  | ||||
|   default_exception_handler.handle_warn(function_name, msg); | ||||
| } | ||||
| #endif // defined(PROJECT_ENABLE_V2_ERRORS) | ||||
|  | ||||
| void set_exception_handler(const i_exception_handler *handler) { | ||||
|   exception_handler = handler; | ||||
| } | ||||
|   | ||||
							
								
								
									
										241
									
								
								support/src/utils/error_handler.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										241
									
								
								support/src/utils/error_handler.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,241 @@ | ||||
| /* | ||||
|   Copyright <2018-2025> <scott.e.graves@protonmail.com> | ||||
|  | ||||
|   Permission is hereby granted, free of charge, to any person obtaining a copy | ||||
|   of this software and associated documentation files (the "Software"), to deal | ||||
|   in the Software without restriction, including without limitation the rights | ||||
|   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||
|   copies of the Software, and to permit persons to whom the Software is | ||||
|   furnished to do so, subject to the following conditions: | ||||
|  | ||||
|   The above copyright notice and this permission notice shall be included in all | ||||
|   copies or substantial portions of the Software. | ||||
|  | ||||
|   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||
|   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||
|   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||
|   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||
|   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||
|   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||||
|   SOFTWARE. | ||||
| */ | ||||
| #include "utils/config.hpp" | ||||
| #include "utils/error.hpp" | ||||
|  | ||||
| namespace repertory::utils::error { | ||||
| std::atomic<const i_exception_handler *> exception_handler{ | ||||
|     &default_exception_handler}; | ||||
|  | ||||
| #if defined(PROJECT_ENABLE_V2_ERRORS) | ||||
| void iostream_exception_handler::handle_debug(std::string_view function_name, | ||||
|                                               std::string_view msg) const { | ||||
|   std::cout << create_error_message({ | ||||
|                    "debug", | ||||
|                    function_name, | ||||
|                    msg, | ||||
|                }) | ||||
|             << std::endl; | ||||
| } | ||||
| #endif // defined(PROJECT_ENABLE_V2_ERRORS) | ||||
|  | ||||
| void iostream_exception_handler::handle_error(std::string_view function_name, | ||||
|                                               std::string_view msg) const { | ||||
|   std::cerr << create_error_message({ | ||||
|                    "error", | ||||
|                    function_name, | ||||
|                    msg, | ||||
|                }) | ||||
|             << std::endl; | ||||
| } | ||||
|  | ||||
| void iostream_exception_handler::handle_exception( | ||||
|     std::string_view function_name) const { | ||||
|   std::cerr << create_error_message({ | ||||
|                    "error", | ||||
|                    function_name, | ||||
|                    "exception", | ||||
|                    "unknown", | ||||
|                }) | ||||
|             << std::endl; | ||||
| } | ||||
|  | ||||
| void iostream_exception_handler::handle_exception( | ||||
|     std::string_view function_name, const std::exception &ex) const { | ||||
|   std::cerr << create_error_message({ | ||||
|                    "error", | ||||
|                    function_name, | ||||
|                    "exception", | ||||
|                    (ex.what() == nullptr ? "unknown" : ex.what()), | ||||
|                }) | ||||
|             << std::endl; | ||||
| } | ||||
|  | ||||
| #if defined(PROJECT_ENABLE_V2_ERRORS) | ||||
| void iostream_exception_handler::handle_info(std::string_view function_name, | ||||
|                                              std::string_view msg) const { | ||||
|   std::cout << create_error_message({ | ||||
|                    "info", | ||||
|                    function_name, | ||||
|                    msg, | ||||
|                }) | ||||
|             << std::endl; | ||||
| } | ||||
|  | ||||
| void iostream_exception_handler::handle_trace(std::string_view function_name, | ||||
|                                               std::string_view msg) const { | ||||
|   std::cout << create_error_message({ | ||||
|                    "trace", | ||||
|                    function_name, | ||||
|                    msg, | ||||
|                }) | ||||
|             << std::endl; | ||||
| } | ||||
|  | ||||
| void iostream_exception_handler::handle_warn(std::string_view function_name, | ||||
|                                              std::string_view msg) const { | ||||
|   std::cout << create_error_message({ | ||||
|                    "warn", | ||||
|                    function_name, | ||||
|                    msg, | ||||
|                }) | ||||
|             << std::endl; | ||||
| } | ||||
| #endif // defined(PROJECT_ENABLE_V2_ERRORS) | ||||
|  | ||||
| #if defined(PROJECT_ENABLE_SPDLOG) && defined(PROJECT_ENABLE_V2_ERRORS) | ||||
| void spdlog_exception_handler::handle_debug(std::string_view function_name, | ||||
|                                             std::string_view msg) const { | ||||
|   auto console = spdlog::get("console"); | ||||
|   if (console) { | ||||
|     console->debug(utils::error::create_error_message(function_name, {msg})); | ||||
|   } else { | ||||
|     fallback.handle_debug(function_name, msg); | ||||
|   } | ||||
|  | ||||
|   auto file = spdlog::get("file"); | ||||
|   if (not file) { | ||||
|     return; | ||||
|   } | ||||
|  | ||||
|   file->debug(utils::error::create_error_message(function_name, {msg})); | ||||
| } | ||||
|  | ||||
| void spdlog_exception_handler::handle_error(std::string_view function_name, | ||||
|                                             std::string_view msg) const { | ||||
|   auto console = spdlog::get("console"); | ||||
|   if (console) { | ||||
|     console->error(utils::error::create_error_message(function_name, {msg})); | ||||
|   } else { | ||||
|     fallback.handle_error(function_name, msg); | ||||
|   } | ||||
|  | ||||
|   auto file = spdlog::get("file"); | ||||
|   if (not file) { | ||||
|     return; | ||||
|   } | ||||
|  | ||||
|   file->error(utils::error::create_error_message(function_name, {msg})); | ||||
| } | ||||
|  | ||||
| void spdlog_exception_handler::handle_exception( | ||||
|     std::string_view function_name) const { | ||||
|   auto console = spdlog::get("console"); | ||||
|   if (console) { | ||||
|     console->error(utils::error::create_error_message(function_name, | ||||
|                                                       { | ||||
|                                                           "exception", | ||||
|                                                           "unknown exception", | ||||
|                                                       })); | ||||
|   } else { | ||||
|     fallback.handle_exception(function_name); | ||||
|   } | ||||
|  | ||||
|   auto file = spdlog::get("file"); | ||||
|   if (not file) { | ||||
|     return; | ||||
|   } | ||||
|  | ||||
|   file->error( | ||||
|       utils::error::create_error_message(function_name, { | ||||
|                                                             "exception", | ||||
|                                                             "unknown exception", | ||||
|                                                         })); | ||||
| } | ||||
|  | ||||
| void spdlog_exception_handler::handle_exception( | ||||
|     std::string_view function_name, const std::exception &ex) const { | ||||
|   auto console = spdlog::get("console"); | ||||
|   if (console) { | ||||
|     console->error(utils::error::create_error_message( | ||||
|         function_name, { | ||||
|                            "exception", | ||||
|                            (ex.what() == nullptr ? "unknown" : ex.what()), | ||||
|                        })); | ||||
|   } else { | ||||
|     fallback.handle_exception(function_name, ex); | ||||
|   } | ||||
|  | ||||
|   auto file = spdlog::get("file"); | ||||
|   if (not file) { | ||||
|     return; | ||||
|   } | ||||
|  | ||||
|   file->error(utils::error::create_error_message( | ||||
|       function_name, { | ||||
|                          "exception", | ||||
|                          (ex.what() == nullptr ? "unknown" : ex.what()), | ||||
|                      })); | ||||
| } | ||||
|  | ||||
| void spdlog_exception_handler::handle_info(std::string_view function_name, | ||||
|                                            std::string_view msg) const { | ||||
|   auto console = spdlog::get("console"); | ||||
|   if (console) { | ||||
|     console->info(utils::error::create_error_message(function_name, {msg})); | ||||
|   } else { | ||||
|     fallback.handle_info(function_name, msg); | ||||
|   } | ||||
|  | ||||
|   auto file = spdlog::get("file"); | ||||
|   if (not file) { | ||||
|     return; | ||||
|   } | ||||
|  | ||||
|   file->info(utils::error::create_error_message(function_name, {msg})); | ||||
| } | ||||
|  | ||||
| void spdlog_exception_handler::handle_trace(std::string_view function_name, | ||||
|                                             std::string_view msg) const { | ||||
|   auto console = spdlog::get("console"); | ||||
|   if (console) { | ||||
|     console->trace(utils::error::create_error_message(function_name, {msg})); | ||||
|   } else { | ||||
|     fallback.handle_trace(function_name, msg); | ||||
|   } | ||||
|  | ||||
|   auto file = spdlog::get("file"); | ||||
|   if (not file) { | ||||
|     return; | ||||
|   } | ||||
|  | ||||
|   file->trace(utils::error::create_error_message(function_name, {msg})); | ||||
| } | ||||
|  | ||||
| void spdlog_exception_handler::handle_warn(std::string_view function_name, | ||||
|                                            std::string_view msg) const { | ||||
|   auto console = spdlog::get("console"); | ||||
|   if (console) { | ||||
|     console->warn(utils::error::create_error_message(function_name, {msg})); | ||||
|   } else { | ||||
|     fallback.handle_warn(function_name, msg); | ||||
|   } | ||||
|  | ||||
|   auto file = spdlog::get("file"); | ||||
|   if (not file) { | ||||
|     return; | ||||
|   } | ||||
|  | ||||
|   file->warn(utils::error::create_error_message(function_name, {msg})); | ||||
| } | ||||
| #endif // defined(PROJECT_ENABLE_SPDLOG) && defined(PROJECT_ENABLE_V2_ERRORS) | ||||
| } // namespace repertory::utils::error | ||||
| @@ -41,8 +41,16 @@ auto change_to_process_directory() -> bool { | ||||
|  | ||||
|     ::GetModuleFileNameA(nullptr, file_name.data(), | ||||
|                          static_cast<DWORD>(file_name.size() - 1U)); | ||||
|     auto path = utils::path::strip_to_file_name(file_name.c_str()); | ||||
|     ::SetCurrentDirectoryA(path.c_str()); | ||||
|     auto path = utils::path::get_parent_path(file_name.c_str()); | ||||
|     auto res = ::SetCurrentDirectoryA(path.c_str()) != 0; | ||||
|     if (not res) { | ||||
|       throw utils::error::create_exception( | ||||
|           function_name, { | ||||
|                              "failed to set current directory", | ||||
|                              std::to_string(utils::get_last_error_code()), | ||||
|                              path, | ||||
|                          }); | ||||
|     } | ||||
| #else // !defined(_WIN32) | ||||
|     std::string path; | ||||
|     path.resize(PATH_MAX + 1); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user