add exception
This commit is contained in:
parent
4fc81c4c04
commit
bb94945fa7
54
src/hack/exception/exception.hpp
Normal file
54
src/hack/exception/exception.hpp
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <experimental/source_location>
|
||||||
|
|
||||||
|
#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;
|
||||||
|
};
|
||||||
|
}
|
9
src/hack/exception/title.hpp
Normal file
9
src/hack/exception/title.hpp
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace hack::exception_title
|
||||||
|
{
|
||||||
|
const std::string NO_VALID_DATA { "no valid data" };
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user