diff --git a/repertory/librepertory/src/comm/curl/curl_shared.cpp b/repertory/librepertory/src/comm/curl/curl_shared.cpp index a33d7d56..222614bb 100644 --- a/repertory/librepertory/src/comm/curl/curl_shared.cpp +++ b/repertory/librepertory/src/comm/curl/curl_shared.cpp @@ -20,6 +20,7 @@ SOFTWARE. */ #include "comm/curl/curl_shared.hpp" +#include "utils/error.hpp" namespace repertory { curl_shared::curl_sh_t curl_shared::cache_; @@ -34,12 +35,15 @@ void curl_shared::cleanup() { auto curl_shared::init() -> bool { auto res = curl_global_init(CURL_GLOBAL_ALL); if (res != 0) { + utils::error::handle_error(function_name, + "failed to initialize curl|result|" + + std::to_string(res)); return false; } auto *cache = curl_share_init(); if (cache == nullptr) { - curl_global_cleanup(); + utils::error::handle_error(function_name, "failed to create curl share"); return false; } cache_.reset(cache); diff --git a/repertory/librepertory/src/drives/fuse/fuse_base.cpp b/repertory/librepertory/src/drives/fuse/fuse_base.cpp index 47f61bc7..cd83932e 100644 --- a/repertory/librepertory/src/drives/fuse/fuse_base.cpp +++ b/repertory/librepertory/src/drives/fuse/fuse_base.cpp @@ -381,6 +381,7 @@ auto fuse_base::init_impl(struct fuse_conn_info *conn) -> void * { if (not console_enabled_ && not repertory::project_initialize()) { utils::error::raise_error(function_name, "failed to initialize repertory"); event_system::instance().raise(function_name); + repertory::project_cleanup(); } return this; diff --git a/repertory/librepertory/src/initialize.cpp b/repertory/librepertory/src/initialize.cpp index e96c5cb6..00051ab7 100644 --- a/repertory/librepertory/src/initialize.cpp +++ b/repertory/librepertory/src/initialize.cpp @@ -39,6 +39,7 @@ #include "spdlog/spdlog.h" #include "initialize.hpp" +#include "utils/error.hpp" #if defined(PROJECT_REQUIRE_ALPINE) && !defined(PROJECT_IS_MINGW) #include "utils/path.hpp" @@ -48,6 +49,11 @@ #include "comm/curl/curl_shared.hpp" #endif // defined(PROJECT_ENABLE_CURL) +namespace { +bool curl_initialized{false}; +bool sqlite3_initialized{false}; +} // namespace + namespace repertory { auto project_initialize() -> bool { #if defined(PROJECT_REQUIRE_ALPINE) && !defined(PROJECT_IS_MINGW) @@ -65,38 +71,39 @@ auto project_initialize() -> bool { #endif // defined(PROJECT_REQUIRE_ALPINE) && !defined (PROJECT_IS_MINGW) spdlog::drop_all(); - spdlog::flush_every(std::chrono::seconds(10)); + spdlog::flush_every(std::chrono::seconds(5)); spdlog::set_pattern("%Y-%m-%d|%T.%e|%^%l%$|%v"); #if defined(PROJECT_ENABLE_LIBSODIUM) - { - if (sodium_init() == -1) { - return false; - } + if (sodium_init() == -1) { + utils::error::handle_error(function_name, "failed to initialize sodium"); + return false; } #endif // defined(PROJECT_ENABLE_LIBSODIUM) #if defined(PROJECT_ENABLE_OPENSSL) - { - SSL_library_init(); - } + SSL_library_init(); #endif // defined(PROJECT_ENABLE_OPENSSL) #if defined(PROJECT_ENABLE_CURL) if (not curl_shared::init()) { return false; } + + curl_initialized = true; #endif // defined(PROJECT_ENABLE_CURL) #if defined(PROJECT_ENABLE_SQLITE) { auto res = sqlite3_initialize(); if (res != SQLITE_OK) { -#if defined(PROJECT_ENABLE_CURL) - curl_shared::cleanup(); -#endif // defined(PROJECT_ENABLE_CURL) + utils::error::handle_error(function_name, + "failed to initialize sqlite3|result|" + + std::to_string(res)); return false; } + + sqlite3_initialized = true; } #endif // defined(PROJECT_ENABLE_SQLITE) @@ -105,12 +112,17 @@ auto project_initialize() -> bool { void project_cleanup() { #if defined(PROJECT_ENABLE_CURL) - curl_shared::cleanup(); + if (curl_initialized) { + curl_shared::cleanup(); + } #endif // defined(PROJECT_ENABLE_CURL) #if defined(PROJECT_ENABLE_SQLITE) - sqlite3_shutdown(); + if (sqlite3_initialized) { + sqlite3_shutdown(); + } #endif // defined(PROJECT_ENABLE_SQLITE) + spdlog::shutdown(); } } // namespace repertory diff --git a/repertory/repertory/main.cpp b/repertory/repertory/main.cpp index d6b9878b..f6b4e5d3 100644 --- a/repertory/repertory/main.cpp +++ b/repertory/repertory/main.cpp @@ -38,6 +38,7 @@ auto main(int argc, char **argv) -> int { if (not repertory::project_initialize()) { std::cerr << "fatal: failed to initialize repertory" << std::endl; + repertory::project_cleanup(); return -1; } diff --git a/repertory/repertory_test/main.cpp b/repertory/repertory_test/main.cpp index 4b07986c..f7d6a633 100644 --- a/repertory/repertory_test/main.cpp +++ b/repertory/repertory_test/main.cpp @@ -25,23 +25,32 @@ #include "initialize.hpp" #include "test_common.hpp" +#include "utils/error.hpp" using namespace repertory; int PROJECT_TEST_RESULT{0}; auto main(int argc, char **argv) -> int { + REPERTORY_USES_FUNCTION_NAME(); + #if defined(PROJECT_ENABLE_BACKWARD_CPP) static backward::SignalHandling sh; #endif // defined(PROJECT_ENABLE_BACKWARD_CPP) if (not repertory::project_initialize()) { - std::cerr << "fatal: failed to initialize repertory" << std::endl; + repertory::project_cleanup(); return -1; } - ::testing::InitGoogleTest(&argc, argv); - PROJECT_TEST_RESULT = RUN_ALL_TESTS(); + try { + ::testing::InitGoogleTest(&argc, argv); + PROJECT_TEST_RESULT = RUN_ALL_TESTS(); + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } repertory::project_cleanup();