5 void print_assertfailed_severe(string expr);
6 void print_assertfailed_fatal(string expr);
8 #define assert(expr, ...) _assert(print_assertfailed_severe, expr, __VA_ARGS__)
9 #define devassert(...) MACRO_BEGIN if (autocvar_developer > 0) assert(__VA_ARGS__); MACRO_END
11 #define assert_once(expr, ...) \
16 assert(expr, __VA_ARGS__); \
20 #define devassert_once(...) MACRO_BEGIN if (autocvar_developer > 0) assert_once(__VA_ARGS__); MACRO_END
22 #define demand(expr, ...) _assert(print_assertfailed_fatal, expr, __VA_ARGS__)
23 #define devdemand(...) MACRO_BEGIN if (autocvar_developer > 0) demand(__VA_ARGS__); MACRO_END
25 #define _assert(f, expr, then) \
34 #define ASSERT_LESS(name, var, const) noref int name[(const - var + 1)];
38 string(string, string...) strcat1n = #53;
40 string(string, string...) strcat1n = #115;
43 // would be nice if __FUNC__ could be concatenated at compile time
45 // less work, bigger binary
46 #define __SOURCELOC__ (sprintf("^7%s^9(^9"__FILE__"^7:^9"STR(__LINE__)"^7)", __FUNC__))
48 #define __SOURCELOC__ (sprintf("^7%s^9(^9%s^7:^9%s^7)", __FUNC__, __FILE__, STR(__LINE__)))
51 #define _LOG_HEADER(level) "^9[::^7"PROGNAME"^9::"level"^9] ", __SOURCELOC__
52 #define _LOG(f, level, s) \
54 f(strcat1n(_LOG_HEADER(level), "\n^7", s, "\n")); \
57 #define LOG_FATAL(...) _LOG_FATAL(strcat1n(__VA_ARGS__))
58 #define LOG_FATALF(...) _LOG_FATAL(sprintf(__VA_ARGS__))
59 #define _LOG_FATAL(s) _LOG(error, "^1FATAL", s)
61 #define LOG_SEVERE(...) _LOG_SEVERE(strcat1n(__VA_ARGS__))
62 #define LOG_SEVEREF(...) _LOG_SEVERE(sprintf(__VA_ARGS__))
63 #define _LOG_SEVERE(s) _LOG(backtrace, "^1SEVERE", s)
65 #define LOG_WARN(...) _LOG_WARN(strcat1n(__VA_ARGS__))
66 #define LOG_WARNF(...) _LOG_WARN(sprintf(__VA_ARGS__))
67 #define _LOG_WARN(s) _LOG(print, "^3WARNING", s)
69 #define LOG_INFO(...) _LOG_INFO(strcat1n(__VA_ARGS__))
70 #define LOG_INFOF(...) _LOG_INFO(sprintf(__VA_ARGS__))
71 #define _LOG_INFO(s) \
73 dprint(_LOG_HEADER("^5INFO")); \
76 /* TODO: unconditionally add a newline when possible */ \
77 if (str2chr(__s, strlen(__s) - 1) != '\n') { print("\n"); } \
80 #define LOG_TRACE(...) _LOG_TRACE(strcat1n(__VA_ARGS__))
81 #define LOG_TRACEF(...) _LOG_TRACE(sprintf(__VA_ARGS__))
82 #define _LOG_TRACE(s) _LOG(dprint, "^6TRACE", s)
84 #define LOG_DEBUG(...) _LOG_DEBUG(strcat1n(__VA_ARGS__))
85 #define LOG_DEBUGF(...) _LOG_DEBUG(sprintf(__VA_ARGS__))
86 #define _LOG_DEBUG(s) _LOG(dprint2, "^2DEBUG", s)
88 #define dprint2(msg) \
90 if (autocvar_developer > 1) dprint(msg); \
93 // simpler version for help messages
94 #define LOG_HELP(...) _LOG_HELP(strcat1n(__VA_ARGS__))
95 #define LOG_HELPF(...) _LOG_HELP(sprintf(__VA_ARGS__))
96 #define _LOG_HELP(s) print("^7", s, "\n")
98 // TODO: this sucks, lets find a better way to do backtraces?
99 #define _backtrace() builtin_remove(NULL)
101 noref int autocvar_developer;
102 noref bool autocvar_prvm_backtraceforwarnings;
104 #define backtrace(msg) \
106 int dev = autocvar_developer; \
107 bool war = autocvar_prvm_backtraceforwarnings; \
108 cvar_set("developer", "1"); \
109 cvar_set("prvm_backtraceforwarnings", "1"); \
110 print("\n--- CUT HERE ---\n", msg); \
112 print("\n--- CUT UNTIL HERE ---\n"); \
113 cvar_set("developer", ftos(dev)); \
114 cvar_set("prvm_backtraceforwarnings", ftos(war)); \
117 void print_assertfailed_severe(string expr)
119 // this is inside a function to avoid expanding it on compilation everytime
120 LOG_SEVERE("assertion failed: `", expr, "`\n");
123 void print_assertfailed_fatal(string expr)
125 // this is inside a function to avoid expanding it on compilation everytime
126 LOG_FATAL("assertion failed: `", expr, "`\n");