Nuclide
Software Development Kit for id Tech
global.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2016-2024 Vera Visions LLC.
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
13 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
14 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15*/
16
17var bool autocvar_g_logTimestamps = false;
18
19typedef enum
20{
26
27#define LOGLEVEL_DEFAULT LOGLEVEL_WARNINGS
29
30#define printf(...) print(sprintf(__VA_ARGS__))
31
32#ifdef DOXYGEN
34#define enumflags enum
35#endif
36
37void
38_NSLog(string msg)
39{
41 print(sprintf("^9%f ^7%s\n", time, msg));
42 else
43 print(sprintf("^7%s\n", msg));
44}
45
46void
47_NSError(string functionName, string msg)
48{
50 print(sprintf("^9%f ^1%s^7: %s\n", time, functionName, msg));
51 else
52 print(sprintf("^1%s^7: %s\n", functionName, msg));
53}
54
55void
56_NSWarning(string functionName, string msg)
57{
59 print(sprintf("^9%f ^3%s^7: %s\n", time, functionName, msg));
60 else
61 print(sprintf("^3%s^7: %s\n", functionName, msg));
62}
63
64void
65_NSAssert(bool condition, string function, string descr)
66{
67 if (!condition) {
68 print(strcat("^1Assertion failed in ", function, ", reason: ", descr, "\n"));
69#ifndef MENU
70 breakpoint();
71#endif
72 }
73}
74
79#define NSLog(...) if (autocvar_g_logLevel >= LOGLEVEL_DEBUG) _NSLog(sprintf(__VA_ARGS__))
80
85#define NSError(...) if (autocvar_g_logLevel >= LOGLEVEL_ERRORS) _NSError(__FUNC__, sprintf(__VA_ARGS__))
86
91#define NSWarning(...) if (autocvar_g_logLevel >= LOGLEVEL_WARNINGS) _NSWarning(__FUNC__, sprintf(__VA_ARGS__))
92
99#define NSAssert(condition, ...) if (autocvar_g_logLevel >= LOGLEVEL_ERRORS) _NSAssert(condition, __FUNC__, sprintf(__VA_ARGS__))
100
102{
110
111const vector g_vec_null = [0.0f, 0.0f, 0.0f];
112
113/* the console needs some attention too. */
114var float g_initTime;
115
116void
117InitPrint(string functionName)
118{
119 int chars = 51i;
120 int charsLeft;
121 int charExtra;
122 string sideLeft = "";
123 string sideRight = "";
124
125 if (functionName == __NULL__) {
126 NSLog("---------------------------------------------------");
127 return;
128 }
129
130 /* word and padding */
131 chars = chars - (int)strlen(functionName) - 2i;
132 charsLeft = chars / 2i;
133 charExtra = chars % 2i;
134
135 for (int i = 0i; i < charsLeft; i++)
136 sideLeft = strcat(sideLeft,"-");
137
138 for (int i = 0i; i < (charsLeft + charExtra); i++) {
139 sideRight = strcat(sideRight,"-");
140 }
141
142 NSLog( "%s %s %s", sideLeft, functionName, sideRight);
143}
144
145var string g_lastInitFunc;
146void
147_InitStart(string functionName)
148{
149 if (g_initTime != 0)
150 error(sprintf("Called InitStart() without InitEnd()ing %s!", g_lastInitFunc));
151
152 InitPrint(functionName);
153 g_lastInitFunc = functionName;
154 g_initTime = gettime(1);
155}
156
157#define InitStart() _InitStart(__FUNC__)
158
159void
161{
162 float endTime = gettime(1);
163 NSLog("loaded in %.1f seconds", (endTime - g_initTime));
164 NSLog("---------------------------------------------------");
165 g_initTime = 0;
166}
167
168#define InitEnd() _InitEnd()
169
171#define entity_def(x, ...) const string x[] = { __VA_ARGS__ }
172
174#define thread(x) if (fork()) { x; abort(); }
const vector g_vec_null
Definition: global.h:111
var float g_initTime
Definition: global.h:114
var string g_lastInitFunc
Definition: global.h:145
#define LOGLEVEL_DEFAULT
Definition: global.h:27
void _InitStart(string functionName)
Definition: global.h:147
void _NSWarning(string functionName, string msg)
Definition: global.h:56
logLevel_t
Definition: global.h:20
@ LOGLEVEL_NONE
Definition: global.h:21
@ LOGLEVEL_ERRORS
Definition: global.h:22
@ LOGLEVEL_WARNINGS
Definition: global.h:23
@ LOGLEVEL_DEBUG
Definition: global.h:24
void _NSLog(string msg)
Definition: global.h:38
void _NSError(string functionName, string msg)
Definition: global.h:47
var bool autocvar_g_logTimestamps
Definition: global.h:17
searchFlags_t
Definition: global.h:102
@ SEARCH_MULTISEARCH
When set, separating search queries with : will allow for multiple queries in one string.
Definition: global.h:107
@ SEARCH_FORCESEARCH
Search a given game directory even if it's not mounted.
Definition: global.h:106
@ SEARCH_NAMESORT
Sort the results alphabetically (slower)
Definition: global.h:108
@ SEARCH_FULLPACKAGE
Package names include the game directory as a prefix.
Definition: global.h:104
@ SEARCH_INSENSITIVE
Attempt to do a case-insensitive search (slower)
Definition: global.h:103
@ SEARCH_ALLOWDUPES
Do not attempt to remove duplicate results (so you can search through multiple archives)
Definition: global.h:105
var logLevel_t autocvar_g_logLevel
Definition: global.h:28
void _NSAssert(bool condition, string function, string descr)
Definition: global.h:65
void InitPrint(string functionName)
Definition: global.h:117
#define NSLog(...)
Logs an message, with timestamp.
Definition: global.h:79
void _InitEnd(void)
Definition: global.h:160
#define enumflags
Doxygen doesn't know what enumflags (aka bitfields) are, used as e.g.
Definition: global.h:34