UraniumCompute 0.1.0
A GPU accelerated parallel task scheduler
Logger.h
1#pragma once
2#include <UnCompute/Base/CompilerTraits.h>
3#include <UnCompute/Base/ResultCode.h>
4
5#if UN_DEBUG
6# define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG
7
9# define UN_Fail() \
10 do \
11 { \
12 UN_DebugBreak(); \
13 } \
14 while (false)
15#else
16# define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO
17
19# define UN_Fail() \
20 do \
21 { \
22 assert(false && "Fatal error"); \
23 } \
24 while (false)
25#endif
26
27#include <spdlog/spdlog.h>
28#include <string_view>
29
30template<>
31struct fmt::formatter<UN::ResultCode> : fmt::formatter<std::string_view>
32{
33 template<typename FormatContext>
34 auto format(const UN::ResultCode& result, FormatContext& ctx) const -> decltype(ctx.out())
35 {
36 return fmt::format_to(ctx.out(), "{}", UN::ResultToString(result));
37 }
38};
39
45#define UNLOG_Debug(...) SPDLOG_DEBUG(__VA_ARGS__)
46#define UNLOG_Info(...) SPDLOG_INFO(__VA_ARGS__)
47#define UNLOG_Warning(...) SPDLOG_WARN(__VA_ARGS__)
48#define UNLOG_Error(...) SPDLOG_ERROR(__VA_ARGS__)
50
52#define UN_Verify(expr, ...) \
53 do \
54 { \
55 if (!(expr)) \
56 { \
57 UNLOG_Error(__VA_ARGS__); \
58 UN_Fail(); \
59 } \
60 } \
61 while (false)
62
66#define UN_VerifyResultFatal(expr, ...) \
67 do \
68 { \
69 if (auto resultCode = (expr); !::UN::Succeeded(resultCode)) \
70 { \
71 UN_Verify(false, "[Result was {}]: {}", resultCode, ::fmt::format(__VA_ARGS__)); \
72 } \
73 } \
74 while (false)
75
77#define UN_VerifyWarning(expr, ...) \
78 do \
79 { \
80 if (!(expr)) \
81 { \
82 UNLOG_Warning(__VA_ARGS__); \
83 } \
84 } \
85 while (false)
86
88#define UN_VerifyError(expr, ...) \
89 do \
90 { \
91 if (!(expr)) \
92 { \
93 UNLOG_Error(__VA_ARGS__); \
94 } \
95 } \
96 while (false)
97
101#define UN_VerifyResult(expr, ...) \
102 do \
103 { \
104 if (auto resultCode = (expr); !::UN::Succeeded(resultCode)) \
105 { \
106 UN_VerifyError(false, "[Result was {}]: {}", resultCode, ::fmt::format(__VA_ARGS__)); \
107 } \
108 } \
109 while (false)
110
111#if UN_DEBUG || defined DOXYGEN
113# define UN_Assert(expr, ...) UN_Verify(expr, __VA_ARGS__)
115# define UN_Warning(expr, ...) UN_VerifyWarning(expr, __VA_ARGS__)
117# define UN_Error(expr, ...) UN_VerifyError(expr, __VA_ARGS__)
118#else
119# define UN_Assert(expr, ...) UN_UNUSED(expr)
120# define UN_Warning(expr, ...) UN_UNUSED(expr)
121# define UN_Error(expr, ...) UN_UNUSED(expr)
122#endif
123
124namespace UN
125{
127 inline void InitializeLogger()
128 {
129 spdlog::set_level(spdlog::level::debug);
130 }
131} // namespace UN