diff options
-rw-r--r-- | src/other_tools/utils/TARGETS | 10 | ||||
-rw-r--r-- | src/other_tools/utils/curl_easy_handle.cpp | 19 | ||||
-rw-r--r-- | src/other_tools/utils/curl_easy_handle.hpp | 9 |
3 files changed, 25 insertions, 13 deletions
diff --git a/src/other_tools/utils/TARGETS b/src/other_tools/utils/TARGETS index 35685ba7..65772ae8 100644 --- a/src/other_tools/utils/TARGETS +++ b/src/other_tools/utils/TARGETS @@ -11,11 +11,15 @@ , "name": ["curl_easy_handle"] , "hdrs": ["curl_easy_handle.hpp"] , "srcs": ["curl_easy_handle.cpp"] - , "deps": ["curl_context", ["@", "gsl", "", "gsl"]] + , "deps": + [ "curl_context" + , ["src/buildtool/logging", "log_level"] + , ["@", "gsl", "", "gsl"] + ] , "stage": ["src", "other_tools", "utils"] , "private-deps": - [ ["src/buildtool/logging", "logging"] - , ["src/buildtool/file_system", "file_system_manager"] + [ ["src/buildtool/file_system", "file_system_manager"] + , ["src/buildtool/logging", "logging"] , ["", "libcurl"] ] } diff --git a/src/other_tools/utils/curl_easy_handle.cpp b/src/other_tools/utils/curl_easy_handle.cpp index 6a98056e..cf833be9 100644 --- a/src/other_tools/utils/curl_easy_handle.cpp +++ b/src/other_tools/utils/curl_easy_handle.cpp @@ -54,14 +54,15 @@ auto read_stream_data(gsl::not_null<std::FILE*> const& stream) noexcept } // namespace -auto CurlEasyHandle::Create() noexcept -> std::shared_ptr<CurlEasyHandle> { - return Create(false, std::nullopt); +auto CurlEasyHandle::Create(LogLevel log_level) noexcept + -> std::shared_ptr<CurlEasyHandle> { + return Create(false, std::nullopt, log_level); } auto CurlEasyHandle::Create( bool no_ssl_verify, - std::optional<std::filesystem::path> const& ca_bundle) noexcept - -> std::shared_ptr<CurlEasyHandle> { + std::optional<std::filesystem::path> const& ca_bundle, + LogLevel log_level) noexcept -> std::shared_ptr<CurlEasyHandle> { try { auto curl = std::make_shared<CurlEasyHandle>(); auto* handle = curl_easy_init(); @@ -72,6 +73,8 @@ auto CurlEasyHandle::Create( // store CA info curl->no_ssl_verify_ = no_ssl_verify; curl->ca_bundle_ = ca_bundle; + // store log level + curl->log_level_ = log_level; return curl; } catch (std::exception const& ex) { Logger::Log(LogLevel::Error, @@ -157,7 +160,7 @@ auto CurlEasyHandle::DownloadToFile( // cleanup failed downloaded file, if created [[maybe_unused]] auto tmp_res = FileSystemManager::RemoveFile(file_path); - Logger::Log(LogLevel::Error, [&tmp_file]() { + Logger::Log(log_level_, [&tmp_file]() { return fmt::format("curl download to file failed:\n{}", read_stream_data(tmp_file)); }); @@ -173,7 +176,7 @@ auto CurlEasyHandle::DownloadToFile( std::fclose(tmp_file); return res; } catch (std::exception const& ex) { - Logger::Log(LogLevel::Error, [&ex, &tmp_file]() { + Logger::Log(log_level_, [&ex, &tmp_file]() { return fmt::format( "curl download to file failed with:\n{}\n" "while performing:\n{}", @@ -234,7 +237,7 @@ auto CurlEasyHandle::DownloadToString(std::string const& url) noexcept // check result if (res != CURLE_OK) { - Logger::Log(LogLevel::Error, [&tmp_file]() { + Logger::Log(log_level_, [&tmp_file]() { return fmt::format("curl download to string failed:\n{}", read_stream_data(tmp_file)); }); @@ -250,7 +253,7 @@ auto CurlEasyHandle::DownloadToString(std::string const& url) noexcept std::fclose(tmp_file); return content; } catch (std::exception const& ex) { - Logger::Log(LogLevel::Error, [&ex, &tmp_file]() { + Logger::Log(log_level_, [&ex, &tmp_file]() { return fmt::format( "curl download to string failed with:\n{}\n" "while performing:\n{}", diff --git a/src/other_tools/utils/curl_easy_handle.hpp b/src/other_tools/utils/curl_easy_handle.hpp index 470f6f2a..4c03d968 100644 --- a/src/other_tools/utils/curl_easy_handle.hpp +++ b/src/other_tools/utils/curl_easy_handle.hpp @@ -22,6 +22,7 @@ #include <string> #include "gsl/gsl" +#include "src/buildtool/logging/log_level.hpp" #include "src/other_tools/utils/curl_context.hpp" extern "C" { @@ -46,13 +47,15 @@ class CurlEasyHandle { auto operator=(CurlEasyHandle&& other) = delete; /// \brief Create a CurlEasyHandle object - [[nodiscard]] auto static Create() noexcept + [[nodiscard]] auto static Create( + LogLevel log_level = LogLevel::Error) noexcept -> std::shared_ptr<CurlEasyHandle>; /// \brief Create a CurlEasyHandle object with non-default CA info [[nodiscard]] auto static Create( bool no_ssl_verify, - std::optional<std::filesystem::path> const& ca_bundle) noexcept + std::optional<std::filesystem::path> const& ca_bundle, + LogLevel log_level = LogLevel::Error) noexcept -> std::shared_ptr<CurlEasyHandle>; /// \brief Download file from URL into given file_path. @@ -73,6 +76,8 @@ class CurlEasyHandle { std::unique_ptr<CURL, decltype(&curl_easy_closer)> handle_{ nullptr, curl_easy_closer}; + // allow also non-fatal logging of curl operations + LogLevel log_level_{}; bool no_ssl_verify_{false}; std::optional<std::filesystem::path> ca_bundle_{std::nullopt}; |