Topaz 5.0
Topaz Game Engine
Loading...
Searching...
No Matches
debug.hpp
1#ifndef TOPAZ_DEBUG_HPP
2#define TOPAZ_DEBUG_HPP
3#include <source_location>
4#include <cstdio>
5#include <utility>
6#include <format>
7#include <string>
8
9namespace tz::detail
10{
11 template<typename... Args>
12 inline void error_internal(const char* preamble, std::format_string<Args...> fmt, const std::source_location& loc, Args&&... args)
13 {
14 std::fprintf(stderr, "%s", preamble);
15 std::fprintf(stderr, "%s", std::format(fmt, std::forward<Args>(args)...).c_str());
16
17 std::string diag_info = std::format("\n\tIn file {}({},{})\n\t>\t{}", loc.file_name(), loc.line(), loc.column(), loc.function_name());
18
19 std::fprintf(stderr, "%s", diag_info.c_str());
20
21 // debug break: int3
22 asm("int3");
23 }
24}
25
34#define tz_assert(cond, fmt, ...) if(!(cond)){tz::detail::error_internal("[Assertion Fail]: ", fmt, std::source_location::current(), __VA_ARGS__);}
35
43#define tz_error(fmt, ...) tz::detail::error_internal("[Error]: ", fmt, std::source_location::current(), __VA_ARGS__)
44
45
46#define tz_seterror(errcode, msg) set_last_error(std::format("({}): {}", error_code_name(errcode), msg))
47
66#define tz_must(fnret) [sl = std::source_location::current()](auto ret){if constexpr(std::is_same_v<std::decay_t<decltype(ret)>, tz::error_code>){if(ret != tz::error_code::success){tz::detail::error_internal("[Must Failure]: ", "error {} {}", sl, static_cast<int>(ret), tz::last_error());}} else{if(!ret.has_value()){tz::detail::error_internal("[Must Failure]: ", "error {} {}", sl, static_cast<int>(ret.error()), tz::last_error());} return ret.value();}}(fnret)
67
68
69#define UNERR(errcode, preamble, ...) {auto msg = std::format(preamble, __VA_ARGS__); tz_seterror(errcode, msg); return std::unexpected(errcode);}
70#define RETERR(errcode, preamble, ...){auto msg = std::format(preamble, __VA_ARGS__); tz_seterror(errcode, msg); return errcode;}
71
72#endif // TOPAZ_DEBUG_HPP