From bb94945fa702cd4e711902e55694d5563d865ce9 Mon Sep 17 00:00:00 2001 From: chatlanin Date: Fri, 3 Jan 2025 15:07:46 +0300 Subject: [PATCH] add exception --- src/hack/exception/exception.hpp | 54 ++++++++++++++++++++++++++++++++ src/hack/exception/title.hpp | 9 ++++++ 2 files changed, 63 insertions(+) create mode 100644 src/hack/exception/exception.hpp create mode 100644 src/hack/exception/title.hpp diff --git a/src/hack/exception/exception.hpp b/src/hack/exception/exception.hpp new file mode 100644 index 0000000..2ed6281 --- /dev/null +++ b/src/hack/exception/exception.hpp @@ -0,0 +1,54 @@ +#pragma once + +#include + +#include "hack/utils/color.hpp" +#include "hack/exception/title.hpp" + +namespace hack +{ + class exception + { + using LOCATION = std::experimental::source_location; + + public: + exception(const std::experimental::source_location loc = LOCATION::current()) : m_location { loc } {} + ~exception() = default; + + public: + void service(const std::string v) noexcept { m_service = v; } + void system_error(const std::exception& e) noexcept { m_system_error = e.what(); } + void title(const std::string v) noexcept { m_title = v; } + void description(const std::string v) noexcept { m_description = v; } + + void log() + { + std::cout << view::color::bold << view::color::red <<"["+m_service+"] " << view::color::reset + << m_location.file_name() << ":" + << view::color::italic << view::color::yellow << m_location.function_name() << "()" << view::color::reset + << view::color::bold << view::color::blue << "[" << m_location.line() << "]" << view::color::reset << ": " + << m_title << std::endl; + + if (!m_description.empty()) + std::cout << view::color::bold << view::color::red <<"["+m_service+"] " << view::color::reset + << m_location.file_name() << ":" + << view::color::italic << view::color::yellow << m_location.function_name() << "()" << view::color::reset + << view::color::bold << view::color::blue << "[" << m_location.line() << "]" << view::color::reset << ": " + << m_description << std::endl; + + if (!m_system_error.empty()) + std::cout << view::color::bold << view::color::red <<"["+m_service+"] " << view::color::reset + << m_location.file_name() << ":" + << view::color::italic << view::color::yellow << m_location.function_name() << "()" << view::color::reset + << view::color::bold << view::color::blue << "[" << m_location.line() << "]" << view::color::reset << ": " + << m_system_error << std::endl; + } + + private: + std::string m_service; + std::string m_system_error; + std::string m_title { exception_title::NO_VALID_DATA }; + std::string m_description; + std::experimental::source_location m_location; + }; +} diff --git a/src/hack/exception/title.hpp b/src/hack/exception/title.hpp new file mode 100644 index 0000000..aaa7d09 --- /dev/null +++ b/src/hack/exception/title.hpp @@ -0,0 +1,9 @@ +#pragma once + +#include + +namespace hack::exception_title +{ + const std::string NO_VALID_DATA { "no valid data" }; +} +