Xonotic
log.qh
Go to the documentation of this file.
1 #pragma once
2 
3 #include "progname.qh"
4 
5 void print_assertfailed_severe(string expr);
6 void print_assertfailed_fatal(string expr);
7 
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
10 
11 #define assert_once(expr, ...) \
12  MACRO_BEGIN \
13  static bool __once; \
14  if (!__once) \
15  { \
16  assert(expr, __VA_ARGS__); \
17  __once = true; \
18  } \
19  MACRO_END
20 #define devassert_once(...) MACRO_BEGIN if (autocvar_developer > 0) assert_once(__VA_ARGS__); MACRO_END
21 
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
24 
25 #define _assert(f, expr, then) \
26  MACRO_BEGIN \
27  if (!(expr)) \
28  { \
29  f(#expr); \
30  then; \
31  } \
32  MACRO_END
33 
34 #define ASSERT_LESS(name, var, const) noref int name[(const - var + 1)];
35 
36 
37 #if defined(MENUQC)
38 string(string, string...) strcat1n = #53;
39 #else
40 string(string, string...) strcat1n = #115;
41 #endif
42 
43 // would be nice if __FUNC__ could be concatenated at compile time
44 #if 0
45  // less work, bigger binary
46  #define __SOURCELOC__ (sprintf("^7%s^9(^9"__FILE__"^7:^9"STR(__LINE__)"^7)", __FUNC__))
47 #else
48  #define __SOURCELOC__ (sprintf("^7%s^9(^9%s^7:^9%s^7)", __FUNC__, __FILE__, STR(__LINE__)))
49 #endif
50 
51 #define _LOG_HEADER(level) "^9[::^7"PROGNAME"^9::"level"^9] ", __SOURCELOC__
52 #define _LOG(f, level, s) \
53  MACRO_BEGIN \
54  if (autocvar_developer > 0) f(strcat1n(_LOG_HEADER(level), "\n")); \
55  f(strcat1n("^7", s, "\n")); \
56  MACRO_END
57 
58 #define LOG_FATAL(...) _LOG_FATAL(strcat1n(__VA_ARGS__))
59 #define LOG_FATALF(...) _LOG_FATAL(sprintf(__VA_ARGS__))
60 #define _LOG_FATAL(s) _LOG(error, "^1FATAL", s)
61 
62 #define LOG_SEVERE(...) _LOG_SEVERE(strcat1n(__VA_ARGS__))
63 #define LOG_SEVEREF(...) _LOG_SEVERE(sprintf(__VA_ARGS__))
64 #define _LOG_SEVERE(s) _LOG(backtrace, "^1SEVERE", s)
65 
66 #define LOG_WARN(...) _LOG_WARN(strcat1n(__VA_ARGS__))
67 #define LOG_WARNF(...) _LOG_WARN(sprintf(__VA_ARGS__))
68 #define _LOG_WARN(s) _LOG(print, "^3WARNING", s)
69 
70 #define LOG_INFO(...) _LOG_INFO(strcat1n(__VA_ARGS__))
71 #define LOG_INFOF(...) _LOG_INFO(sprintf(__VA_ARGS__))
72 #define _LOG_INFO(s) \
73  MACRO_BEGIN \
74  if (autocvar_developer > 1) dprint(strcat1n(_LOG_HEADER("^5INFO"), "\n")); \
75  string __s = s; \
76  print("^7", __s); \
77  /* TODO: unconditionally add a newline when possible */ \
78  if (str2chr(__s, strlen(__s) - 1) != '\n') { print("\n"); } \
79  MACRO_END
80 
81 #define LOG_TRACE(...) _LOG_TRACE(strcat1n(__VA_ARGS__))
82 #define LOG_TRACEF(...) _LOG_TRACE(sprintf(__VA_ARGS__))
83 #define _LOG_TRACE(s) _LOG(dprint, "^6TRACE", s)
84 
85 #define LOG_DEBUG(...) _LOG_DEBUG(strcat1n(__VA_ARGS__))
86 #define LOG_DEBUGF(...) _LOG_DEBUG(sprintf(__VA_ARGS__))
87 #define _LOG_DEBUG(s) _LOG(dprint2, "^2DEBUG", s)
88 
89 #define dprint2(msg) \
90  MACRO_BEGIN \
91  if (autocvar_developer > 1) dprint(msg); \
92  MACRO_END
93 
94 // simpler version for help messages
95 #define LOG_HELP(...) _LOG_HELP(strcat1n(__VA_ARGS__))
96 #define LOG_HELPF(...) _LOG_HELP(sprintf(__VA_ARGS__))
97 #define _LOG_HELP(s) print("^7", s, "\n")
98 
99 // TODO: this sucks, lets find a better way to do backtraces?
100 #define _backtrace() builtin_remove(NULL)
101 
104 
105 #define backtrace(msg) \
106  MACRO_BEGIN \
107  int dev = autocvar_developer; \
108  bool war = autocvar_prvm_backtraceforwarnings; \
109  cvar_set("developer", "1"); \
110  cvar_set("prvm_backtraceforwarnings", "1"); \
111  print("\n--- CUT HERE ---\n", msg); \
112  _backtrace(); \
113  print("\n--- CUT UNTIL HERE ---\n"); \
114  cvar_set("developer", ftos(dev)); \
115  cvar_set("prvm_backtraceforwarnings", ftos(war)); \
116  MACRO_END
117 
118 void print_assertfailed_severe(string expr)
119 {
120  // this is inside a function to avoid expanding it on compilation everytime
121  LOG_SEVERE("assertion failed: `", expr, "`\n");
122 }
123 
124 void print_assertfailed_fatal(string expr)
125 {
126  // this is inside a function to avoid expanding it on compilation everytime
127  LOG_FATAL("assertion failed: `", expr, "`\n");
128 }
void print_assertfailed_severe(string expr)
Definition: log.qh:118
void print_assertfailed_fatal(string expr)
Definition: log.qh:124
string(string, string...) strcat1n
#define LOG_SEVERE(...)
Definition: log.qh:62
noref int autocvar_developer
Definition: log.qh:102
noref bool autocvar_prvm_backtraceforwarnings
Definition: log.qh:103
#define LOG_FATAL(...)
Definition: log.qh:58