diff options
Diffstat (limited to 'src/core/utils')
-rw-r--r-- | src/core/utils/GpgUtils.cpp | 37 | ||||
-rw-r--r-- | src/core/utils/LogUtils.cpp | 33 | ||||
-rw-r--r-- | src/core/utils/LogUtils.h | 31 | ||||
-rw-r--r-- | src/core/utils/MemoryUtils.h | 12 |
4 files changed, 97 insertions, 16 deletions
diff --git a/src/core/utils/GpgUtils.cpp b/src/core/utils/GpgUtils.cpp index 8588f117..ba8d8ba8 100644 --- a/src/core/utils/GpgUtils.cpp +++ b/src/core/utils/GpgUtils.cpp @@ -53,11 +53,26 @@ static inline auto Trim(std::string& s) -> std::string { return s; } +auto GetGpgmeErrorString(size_t buffer_size, gpgme_error_t err) -> std::string { + std::vector<char> buffer(buffer_size); + + gpgme_error_t ret = gpgme_strerror_r(err, buffer.data(), buffer.size()); + if (ret == ERANGE && buffer_size < 1024) { + return GetGpgmeErrorString(buffer_size * 2, err); + } + + return std::string(buffer.data()); +} + +auto GetGpgmeErrorString(gpgme_error_t err) -> std::string { + return GetGpgmeErrorString(64, err); +} + auto CheckGpgError(GpgError err) -> GpgError { if (gpg_err_code(err) != GPG_ERR_NO_ERROR) { SPDLOG_ERROR( "gpg operation failed [error code: {}], source: {} description: {}", - gpg_err_code(err), gpgme_strsource(err), gpgme_strerror(err)); + gpg_err_code(err), gpgme_strsource(err), GetGpgmeErrorString(err)); } return err; } @@ -65,27 +80,27 @@ auto CheckGpgError(GpgError err) -> GpgError { auto CheckGpgError2ErrCode(GpgError err, GpgError predict) -> GpgErrorCode { auto err_code = gpg_err_code(err); if (err_code != gpg_err_code(predict)) { - if (err_code == GPG_ERR_NO_ERROR) + if (err_code == GPG_ERR_NO_ERROR) { SPDLOG_WARN("[Warning {}] Source: {} description: {} predict: {}", - gpg_err_code(err), gpgme_strsource(err), gpgme_strerror(err), - gpgme_strerror(err)); - else + gpg_err_code(err), gpgme_strsource(err), + GetGpgmeErrorString(err), GetGpgmeErrorString(predict)); + } else { SPDLOG_ERROR("[Error {}] Source: {} description: {} predict: {}", - gpg_err_code(err), gpgme_strsource(err), gpgme_strerror(err), - gpgme_strerror(err)); + gpg_err_code(err), gpgme_strsource(err), + GetGpgmeErrorString(err), GetGpgmeErrorString(predict)); + } } return err_code; } auto DescribeGpgErrCode(GpgError err) -> GpgErrorDesc { - return {gpgme_strsource(err), gpgme_strerror(err)}; + return {gpgme_strsource(err), GetGpgmeErrorString(err)}; } auto CheckGpgError(GpgError err, const std::string& /*comment*/) -> GpgError { if (gpg_err_code(err) != GPG_ERR_NO_ERROR) { - SPDLOG_WARN("[Error {}] Source: {} description: {} predict: {}", - gpg_err_code(err), gpgme_strsource(err), gpgme_strerror(err), - gpgme_strerror(err)); + SPDLOG_WARN("[Error {}] Source: {} description: {}", gpg_err_code(err), + gpgme_strsource(err), GetGpgmeErrorString(err)); } return err; } diff --git a/src/core/utils/LogUtils.cpp b/src/core/utils/LogUtils.cpp new file mode 100644 index 00000000..d7d13579 --- /dev/null +++ b/src/core/utils/LogUtils.cpp @@ -0,0 +1,33 @@ +/** + * Copyright (C) 2021 Saturneric <[email protected]> + * + * This file is part of GpgFrontend. + * + * GpgFrontend is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GpgFrontend is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>. + * + * The initial version of the source code is inherited from + * the gpg4usb project, which is under GPL-3.0-or-later. + * + * All the source code of GpgFrontend was modified and released by + * Saturneric <[email protected]> starting on May 12, 2021. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#include "LogUtils.h" + +auto GetCoreLogger() -> std::shared_ptr<spdlog::logger> { + return spdlog::get("core"); +}
\ No newline at end of file diff --git a/src/core/utils/LogUtils.h b/src/core/utils/LogUtils.h new file mode 100644 index 00000000..a4d29dd8 --- /dev/null +++ b/src/core/utils/LogUtils.h @@ -0,0 +1,31 @@ +/** + * Copyright (C) 2021 Saturneric <[email protected]> + * + * This file is part of GpgFrontend. + * + * GpgFrontend is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GpgFrontend is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>. + * + * The initial version of the source code is inherited from + * the gpg4usb project, which is under GPL-3.0-or-later. + * + * All the source code of GpgFrontend was modified and released by + * Saturneric <[email protected]> starting on May 12, 2021. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#pragma once + +auto GPGFRONTEND_CORE_EXPORT GetCoreLogger() -> std::shared_ptr<spdlog::logger>;
\ No newline at end of file diff --git a/src/core/utils/MemoryUtils.h b/src/core/utils/MemoryUtils.h index 4c68e864..5f4283b2 100644 --- a/src/core/utils/MemoryUtils.h +++ b/src/core/utils/MemoryUtils.h @@ -30,7 +30,6 @@ #include "core/GpgFrontendCoreExport.h" #include "core/function/SecureMemoryAllocator.h" -#include "spdlog/spdlog.h" /* To avoid that a compiler optimizes certain memset calls away, these macros may be used instead. */ @@ -112,6 +111,7 @@ template <typename T, typename... Args> static auto SecureCreateObject(Args &&...args) -> T * { void *mem = SecureMemoryAllocator::Allocate(sizeof(T)); if (!mem) return nullptr; + SPDLOG_TRACE("alloc secure memnory success, type: {}, size: {}, addr: {}", typeid(T).name(), sizeof(T), mem); @@ -126,8 +126,6 @@ static auto SecureCreateObject(Args &&...args) -> T * { template <typename T> static void SecureDestroyObject(T *obj) { if (!obj) return; - - SPDLOG_TRACE("try to free object, obj: {}", static_cast<void *>(obj)); obj->~T(); SecureMemoryAllocator::Deallocate(obj); } @@ -137,8 +135,10 @@ static auto SecureCreateUniqueObject(Args &&...args) -> std::unique_ptr<T, SecureObjectDeleter<T>> { void *mem = SecureMemoryAllocator::Allocate(sizeof(T)); if (!mem) throw std::bad_alloc(); + SPDLOG_TRACE( - "alloc secure memnory success, unique ptr, type: {}, size: {}, addr: {}", + "alloc secure memnory success, unique ptr, " + "type: {}, size: {}, addr: {}", typeid(T).name(), sizeof(T), mem); try { @@ -154,8 +154,10 @@ template <typename T, typename... Args> auto SecureCreateSharedObject(Args &&...args) -> std::shared_ptr<T> { void *mem = SecureMemoryAllocator::Allocate(sizeof(T)); if (!mem) throw std::bad_alloc(); + SPDLOG_TRACE( - "alloc secure memnory success, shared ptr, type: {}, size: {}, addr: {}", + "alloc secure memnory success, shared ptr, " + "type: {}, size: {}, addr: {}", typeid(T).name(), sizeof(T), mem); try { |