]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/log.qh
General cleanup/optimize
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / log.qh
1 #ifndef LOG_H
2 #define LOG_H
3
4 #define _printferr(...) error(sprintf(__VA_ARGS__))
5 #define _printfbt(...) backtrace(sprintf(__VA_ARGS__))
6 #define printf(...) print(sprintf(__VA_ARGS__))
7 #define dprintf(...) dprint(sprintf(__VA_ARGS__))
8 #define _dprintf2(...) \
9         do \
10         { \
11                 if (autocvar_developer > 1) dprintf(__VA_ARGS__); \
12         } \
13         while (0)
14
15 #define assert(expr, ...) \
16         do \
17         { \
18                 if (!(expr)) LOG_WARNINGF(__VA_ARGS__); \
19         } \
20         while (0)
21
22 #define _LOG(f, level, s) f("[::"level "] ["__FILE__ ":%s:%.0f] %s", __FUNC__, __LINE__, s)
23
24 #define  LOG_FATAL(...) _LOG_FATAL(strcat("", __VA_ARGS__))
25 #define  LOG_FATALF(...) _LOG_FATAL(sprintf(__VA_ARGS__))
26 #define _LOG_FATAL(s) _LOG(_printferr, "FATAL", s)
27
28 #define  LOG_SEVERE(...) _LOG_SEVERE(strcat("", __VA_ARGS__))
29 #define  LOG_SEVEREF(...) _LOG_SEVERE(sprintf(__VA_ARGS__))
30 #define _LOG_SEVERE(s) _LOG(_printfbt, "SEVERE", s)
31
32 #define  LOG_WARNING(...) _LOG_WARNING(strcat("", __VA_ARGS__))
33 #define  LOG_WARNINGF(...) _LOG_WARNING(sprintf(__VA_ARGS__))
34 #define _LOG_WARNING(s) _LOG(printf, "WARNING", s)
35
36 #define  LOG_INFO(...) \
37         do \
38         { \
39                 if (autocvar_developer) _LOG_INFO(strcat("", __VA_ARGS__)); \
40                 else print(__VA_ARGS__); \
41         } \
42         while (0)
43 #define  LOG_INFOF(...) \
44         do \
45         { \
46                 if (autocvar_developer) _LOG_INFO(sprintf(__VA_ARGS__)); \
47                 else printf(__VA_ARGS__); \
48         } \
49         while (0)
50 #define _LOG_INFO(s) _LOG(printf, "INFO", s)
51
52 #define  LOG_TRACE(...) _LOG_TRACE(strcat("", __VA_ARGS__))
53 #define  LOG_TRACEF(...) _LOG_TRACE(sprintf(__VA_ARGS__))
54 #define _LOG_TRACE(s) _LOG(dprintf, "TRACE", s)
55
56 #define  LOG_DEBUG(...) _LOG_DEBUG(strcat("", __VA_ARGS__))
57 #define  LOG_DEBUGF(...) _LOG_DEBUG(sprintf(__VA_ARGS__))
58 #define _LOG_DEBUG(s) _LOG(_dprintf2, "DEBUG", s)
59
60 // TODO: this sucks, lets find a better way to do backtraces?
61 #ifdef SVQC
62         void builtin_remove(entity);
63         #define _backtrace() builtin_remove(NULL)
64 #else
65         void remove(entity);
66         #define _backtrace() remove(NULL)
67 #endif
68
69 noref int autocvar_developer;
70 noref bool autocvar_prvm_backtraceforwarnings;
71
72 #define backtrace(msg) \
73         do \
74         { \
75                 int dev = autocvar_developer; \
76                 bool war = autocvar_prvm_backtraceforwarnings; \
77                 cvar_set("developer", "1"); \
78                 cvar_set("prvm_backtraceforwarnings", "1"); \
79                 print("\n--- CUT HERE ---\n", msg, "\n"); \
80                 _backtrace(); \
81                 print("\n--- CUT UNTIL HERE ---\n"); \
82                 cvar_set("developer", ftos(dev)); \
83                 cvar_set("prvm_backtraceforwarnings", ftos(war)); \
84         } \
85         while (0)
86
87 #define ASSERT(expr) \
88         do \
89         { \
90                 if (!(expr)) LOG_FATAL("assertion failed: " #expr "\n"); \
91         } \
92         while (0)
93
94 #endif