nCine 2025.12.r529-30f7d03
A cross-platform 2D game engine
Loading...
Searching...
No Matches
common_macros.h
1#ifndef NCINE_COMMON_MACROS
2#define NCINE_COMMON_MACROS
3
4#include <cstdlib> // for abort()
5#include "ServiceLocator.h"
6
7#ifdef NCINE_DEBUG
8 #define NCINE_ASSERT_BREAK
9#endif
10
11#ifdef __GNUC__
12 #define FUNCTION __PRETTY_FUNCTION__
13#elif _MSC_VER
14 #define FUNCTION __FUNCTION__
15#else
16 #define FUNCTION __func__
17#endif
18
19#define LOGV_X(fmt, ...) ncine::theServiceLocator().logger().write(ncine::ILogger::LogLevel::VERBOSE, static_cast<const char *>("%s -> " fmt), FUNCTION, ##__VA_ARGS__)
20#define LOGD_X(fmt, ...) ncine::theServiceLocator().logger().write(ncine::ILogger::LogLevel::DEBUG, static_cast<const char *>("%s -> " fmt), FUNCTION, ##__VA_ARGS__)
21#define LOGI_X(fmt, ...) ncine::theServiceLocator().logger().write(ncine::ILogger::LogLevel::INFO, static_cast<const char *>("%s -> " fmt), FUNCTION, ##__VA_ARGS__)
22#define LOGW_X(fmt, ...) ncine::theServiceLocator().logger().write(ncine::ILogger::LogLevel::WARN, static_cast<const char *>("%s -> " fmt), FUNCTION, ##__VA_ARGS__)
23#define LOGE_X(fmt, ...) ncine::theServiceLocator().logger().write(ncine::ILogger::LogLevel::ERROR, static_cast<const char *>("%s -> " fmt), FUNCTION, ##__VA_ARGS__)
24#define LOGF_X(fmt, ...) ncine::theServiceLocator().logger().write(ncine::ILogger::LogLevel::FATAL, static_cast<const char *>("%s -> " fmt), FUNCTION, ##__VA_ARGS__)
25#define LOG_X(logLevel, fmt, ...) ncine::theServiceLocator().logger().write(logLevel, static_cast<const char *>("%s -> " fmt), FUNCTION, ##__VA_ARGS__)
26
27#define LOGV(fmt) ncine::theServiceLocator().logger().write(ncine::ILogger::LogLevel::VERBOSE, static_cast<const char *>("%s -> " fmt), FUNCTION)
28#define LOGD(fmt) ncine::theServiceLocator().logger().write(ncine::ILogger::LogLevel::DEBUG, static_cast<const char *>("%s -> " fmt), FUNCTION)
29#define LOGI(fmt) ncine::theServiceLocator().logger().write(ncine::ILogger::LogLevel::INFO, static_cast<const char *>("%s -> " fmt), FUNCTION)
30#define LOGW(fmt) ncine::theServiceLocator().logger().write(ncine::ILogger::LogLevel::WARN, static_cast<const char *>("%s -> " fmt), FUNCTION)
31#define LOGE(fmt) ncine::theServiceLocator().logger().write(ncine::ILogger::LogLevel::ERROR, static_cast<const char *>("%s -> " fmt), FUNCTION)
32#define LOGF(fmt) ncine::theServiceLocator().logger().write(ncine::ILogger::LogLevel::FATAL, static_cast<const char *>("%s -> " fmt), FUNCTION)
33#define LOG(logLevel, fmt) ncine::theServiceLocator().logger().write(logLevel, static_cast<const char *>("%s -> " fmt), FUNCTION)
34
35#ifdef NCINE_ASSERT_BREAK
36 #ifdef _MSC_VER
37 #define BREAK() __debugbreak()
38 #else
39 #ifndef __has_builtin
40 #define __has_builtin(x) 0
41 #endif
42
43 #if __has_builtin(__builtin_trap)
44 #define BREAK() __builtin_trap()
45 #else
46 #define BREAK() ::abort()
47 #endif
48 #endif
49#else
50 #define BREAK() ::exit(EXIT_FAILURE)
51#endif
52
53// Fatal assert macros
54#define FATAL_ASSERT_MSG_X(x, fmt, ...) \
55 do \
56 { \
57 if (!(x)) \
58 { \
59 LOGF_X(fmt, ##__VA_ARGS__); \
60 BREAK(); \
61 } \
62 } while (false)
63
64#define FATAL_ASSERT_MSG(x, fmt) \
65 do \
66 { \
67 if (!(x)) \
68 { \
69 LOGF(fmt); \
70 BREAK(); \
71 } \
72 } while (false)
73
74#define FATAL_ASSERT(x) \
75 do \
76 { \
77 if (!(x)) \
78 { \
79 LOGF("FATAL_ASSERT(" #x ")"); \
80 BREAK(); \
81 } \
82 } while (false)
83
84#define FATAL_ASSERT_NOLOG(x) \
85 do \
86 { \
87 if (!(x)) \
88 { \
89 BREAK(); \
90 } \
91 } while (false)
92
93// Fatal macros
94#define FATAL_MSG_X(fmt, ...) \
95 do \
96 { \
97 LOGF_X(fmt, ##__VA_ARGS__); \
98 BREAK(); \
99 } while (false)
100
101#define FATAL_MSG(fmt) \
102 do \
103 { \
104 LOGF(fmt); \
105 BREAK(); \
106 } while (false)
107
108#define FATAL() \
109 do \
110 { \
111 BREAK(); \
112 } while (false)
113
114// Non-fatal assert macros
115#ifdef NCINE_ASSERT_BREAK
116 #define ASSERT_MSG_X(x, fmt, ...) \
117 do \
118 { \
119 if (!(x)) \
120 { \
121 LOGE_X(fmt, ##__VA_ARGS__); \
122 BREAK(); \
123 } \
124 } while (false)
125
126 #define ASSERT_MSG(x, fmt) \
127 do \
128 { \
129 if (!(x)) \
130 { \
131 LOGE(fmt); \
132 BREAK(); \
133 } \
134 } while (false)
135
136 #define ASSERT(x) \
137 do \
138 { \
139 if (!(x)) \
140 { \
141 LOGE("ASSERT(" #x ")"); \
142 BREAK(); \
143 } \
144 } while (false)
145
146 #define ASSERT_NOLOG(x) \
147 do \
148 { \
149 if (!(x)) \
150 { \
151 BREAK(); \
152 } \
153 } while (false)
154#else
155 #define ASSERT_MSG_X(x, fmt, ...) \
156 do \
157 { \
158 (void)sizeof(x); \
159 } while (false)
160
161 #define ASSERT_MSG(x, fmt) \
162 do \
163 { \
164 (void)sizeof(x); \
165 } while (false)
166
167 #define ASSERT(x) \
168 do \
169 { \
170 (void)sizeof(x); \
171 } while (false)
172
173 #define ASSERT_NOLOG(x) \
174 do \
175 { \
176 (void)sizeof(x); \
177 } while (false)
178#endif
179
180#endif