nCine 2025.06.r503-ff15d8d
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// Fatal macros
85#define FATAL_MSG_X(fmt, ...) \
86 do \
87 { \
88 LOGF_X(fmt, ##__VA_ARGS__); \
89 BREAK(); \
90 } while (false)
91
92#define FATAL_MSG(fmt) \
93 do \
94 { \
95 LOGF(fmt); \
96 BREAK(); \
97 } while (false)
98
99#define FATAL() \
100 do \
101 { \
102 BREAK(); \
103 } while (false)
104
105// Non-fatal assert macros
106#ifdef NCINE_ASSERT_BREAK
107 #define ASSERT_MSG_X(x, fmt, ...) \
108 do \
109 { \
110 if (!(x)) \
111 { \
112 LOGE_X(fmt, ##__VA_ARGS__); \
113 BREAK(); \
114 } \
115 } while (false)
116
117 #define ASSERT_MSG(x, fmt) \
118 do \
119 { \
120 if (!(x)) \
121 { \
122 LOGE(fmt); \
123 BREAK(); \
124 } \
125 } while (false)
126
127 #define ASSERT(x) \
128 do \
129 { \
130 if (!(x)) \
131 { \
132 LOGE("ASSERT(" #x ")"); \
133 BREAK(); \
134 } \
135 } while (false)
136#else
137 #define ASSERT_MSG_X(x, fmt, ...) \
138 do \
139 { \
140 (void)sizeof(x); \
141 } while (false)
142 #define ASSERT_MSG(x, fmt) \
143 do \
144 { \
145 (void)sizeof(x); \
146 } while (false)
147 #define ASSERT(x) \
148 do \
149 { \
150 (void)sizeof(x); \
151 } while (false)
152#endif
153
154#endif