From ee6c576c5f3b9807c60c49c0a7d65b1dfd1a2a78 Mon Sep 17 00:00:00 2001 From: Tobias Frisch <tfrisch@uni-koblenz.de> Date: Wed, 13 Jul 2022 15:48:05 +0200 Subject: [PATCH] Added support for automatic exception logging Signed-off-by: Tobias Frisch <tfrisch@uni-koblenz.de> --- include/vkcv/FeatureManager.hpp | 2 +- include/vkcv/Logger.hpp | 36 +++++++++++++++++++++++++++++++++ src/vkcv/Context.cpp | 6 +++--- src/vkcv/FeatureManager.cpp | 2 +- 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/include/vkcv/FeatureManager.hpp b/include/vkcv/FeatureManager.hpp index 5ab424b2..0ed0d440 100644 --- a/include/vkcv/FeatureManager.hpp +++ b/include/vkcv/FeatureManager.hpp @@ -421,7 +421,7 @@ namespace vkcv { if (!checkSupport(features, required)) { if (required) { - throw std::runtime_error("Required feature is not supported!"); + vkcv_log_throw_error("Required feature is not supported!"); } return false; diff --git a/include/vkcv/Logger.hpp b/include/vkcv/Logger.hpp index 9d4fa86f..a1805651 100644 --- a/include/vkcv/Logger.hpp +++ b/include/vkcv/Logger.hpp @@ -6,6 +6,7 @@ */ #include <cstdio> +#include <exception> namespace vkcv { @@ -113,4 +114,39 @@ namespace vkcv { #define vkcv_log(level, ...) {} #endif +/** + * @brief Macro-function to log the message of any error + * or an exception. + * + * @param[in] error Error or exception + */ +#define vkcv_log_error(error) { \ + vkcv_log(LogLevel::ERROR, "%s", (error).what()); \ +} + +/** + * @brief Macro-function to throw and log any error or + * an exception. + * + * @param[in] error Error or exception + */ +#define vkcv_log_throw(error) { \ + try { \ + throw error; \ + } catch (const std::exception& e) { \ + vkcv_log_error(e); \ + throw; \ + } \ +} + +/** + * @brief Macro-function to throw and log an error + * with its custom message. + * + * @param[in] message Error message + */ +#define vkcv_log_throw_error(message) { \ + vkcv_log_throw(std::runtime_error(message)); \ +} + } diff --git a/src/vkcv/Context.cpp b/src/vkcv/Context.cpp index f0520f05..0f5a07c4 100644 --- a/src/vkcv/Context.cpp +++ b/src/vkcv/Context.cpp @@ -352,7 +352,7 @@ namespace vkcv }; if (!checkSupport(supportedLayers, validationLayers)) { - throw std::runtime_error("Validation layers requested but not available!"); + vkcv_log_throw_error("Validation layers requested but not available!"); } #endif @@ -377,7 +377,7 @@ namespace vkcv requiredExtensions.insert(requiredExtensions.end(), instanceExtensions.begin(), instanceExtensions.end()); if (!checkSupport(supportedExtensions, requiredExtensions)) { - throw std::runtime_error("The requested instance extensions are not supported!"); + vkcv_log_throw_error("The requested instance extensions are not supported!"); } const vk::ApplicationInfo applicationInfo( @@ -408,7 +408,7 @@ namespace vkcv vk::PhysicalDevice physicalDevice; if (!pickPhysicalDevice(instance, physicalDevice)) { - throw std::runtime_error("Picking suitable GPU as physical device failed!"); + vkcv_log_throw_error("Picking suitable GPU as physical device failed!"); } FeatureManager featureManager (physicalDevice); diff --git a/src/vkcv/FeatureManager.cpp b/src/vkcv/FeatureManager.cpp index 8c8c16a5..502b0797 100644 --- a/src/vkcv/FeatureManager.cpp +++ b/src/vkcv/FeatureManager.cpp @@ -557,7 +557,7 @@ m_physicalDevice.getFeatures2(&query) delete[] clone; if (required) { - throw std::runtime_error("Required extension is not supported!"); + vkcv_log_throw_error("Required extension is not supported!"); } return false; -- GitLab