mal-packet-weaver
C++20 packet serialization/deserialization library.
Loading...
Searching...
No Matches
debug_with_source_location.hpp
Go to the documentation of this file.
1#pragma once
2#include "../debug.hpp"
3
10namespace mal_toolkit
11{
18 inline std::string CurrentSourceLocation(std::source_location location = std::source_location::current())
19 {
20 std::stringstream ss;
21 ss << "[" << location.file_name() << "] ";
22 ss << location.function_name() << "(line " << location.line() << ", column " << location.column() << ") ";
23 return ss.str();
24 }
53 inline void Assert(bool value, std::string_view message = "Assert failed",
54 std::source_location location = std::source_location::current())
55 {
56 if constexpr (!MAL_TOOLKIT_ASSERT_ENABLED)
57 {
58 return;
59 }
60
61 if (value) [[likely]]
62 {
63 return;
64 }
65
66 if constexpr (MAL_TOOLKIT_ASSERT_LOGS)
67 {
68 spdlog::critical(CurrentSourceLocation(location) + std::basic_string(message));
69 }
70 if constexpr (MAL_TOOLKIT_FORCE_ASSERT)
71 {
72 assert(value);
73 }
74 else
75 {
76 if constexpr (MAL_TOOLKIT_ASSERT_ABORTS)
77 {
78 std::abort();
79 }
80 else
81 {
82 throw std::runtime_error(CurrentSourceLocation(location) + std::basic_string(message));
83 }
84 }
85 }
86
116 inline void AlwaysAssert(bool value, std::string_view message = "Assert failed",
117 std::source_location location = std::source_location::current())
118 {
120 {
121 return;
122 }
123
124 if (value) [[likely]]
125 {
126 return;
127 }
128
129 if constexpr (MAL_TOOLKIT_ASSERT_LOGS)
130 {
131 spdlog::critical(CurrentSourceLocation(location) + std::basic_string(message));
132 }
133
134 if constexpr (MAL_TOOLKIT_FORCE_ASSERT)
135 {
136 assert(value);
137 }
138 else
139 {
140 if constexpr (MAL_TOOLKIT_ASSERT_ABORTS)
141 {
142 std::abort();
143 }
144 else
145 {
146 throw std::runtime_error(CurrentSourceLocation(location) + std::basic_string(message));
147 }
148 }
149 }
150} // namespace mal_toolkit
Contains debugging-related macros and utilities for assisting with debugging tasks.
#define MAL_TOOLKIT_ASSERT_ABORTS
Controls whether assertions should result in program abortion.
Definition debug.hpp:35
#define MAL_TOOLKIT_FORCE_ASSERT
Definition debug.hpp:93
#define MAL_TOOLKIT_ASSERT_ENABLED
Definition debug.hpp:75
#define MAL_TOOLKIT_ASSERT_LOGS
Indicates whether assertion logs are enabled (1) or disabled (0).
Definition debug.hpp:18
#define MAL_TOOLKIT_ALWAYS_ASSERT_ENABLED
Definition debug.hpp:78
Contains a collection of tools and utilities provided by the MAL Toolkit library.
Definition backoffs.hpp:7
void Assert(bool value, std::string_view message="Assert failed", std::source_location location=std::source_location::current())
Asserts a condition with customizable behavior based on debug mode.
std::string CurrentSourceLocation()
Returns an empty string representing the current source location.
void AlwaysAssert(bool value, std::string_view message="Assert failed", std::source_location location=std::source_location::current())
Always asserts a condition with customizable behavior.