Flecs v3.2
A fast entity component system (ECS) for C & C++
Loading...
Searching...
No Matches
log.h
Go to the documentation of this file.
1
28#ifndef FLECS_LOG_H
29#define FLECS_LOG_H
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
35#ifdef FLECS_LOG
36
48
49FLECS_API
50void ecs_deprecated_(
51 const char *file,
52 int32_t line,
53 const char *msg);
54
61FLECS_API
62void ecs_log_push_(int32_t level);
63
70FLECS_API
71void ecs_log_pop_(int32_t level);
72
80FLECS_API
81bool ecs_should_log(int32_t level);
82
86
88FLECS_API
89const char* ecs_strerror(
90 int32_t error_code);
91
92#else // FLECS_LOG
93
97
98#define ecs_deprecated_(file, line, msg)\
99 (void)file;\
100 (void)line;\
101 (void)msg
102
103#define ecs_log_push_(level)
104#define ecs_log_pop_(level)
105#define ecs_should_log(level) false
106
107#define ecs_strerror(error_code)\
108 (void)error_code
109
110#endif // FLECS_LOG
111
112
116
117FLECS_API
118void ecs_print_(
119 int32_t level,
120 const char *file,
121 int32_t line,
122 const char *fmt,
123 ...);
124
125FLECS_API
126void ecs_printv_(
127 int level,
128 const char *file,
129 int32_t line,
130 const char *fmt,
131 va_list args);
132
133FLECS_API
134void ecs_log_(
135 int32_t level,
136 const char *file,
137 int32_t line,
138 const char *fmt,
139 ...);
140
141FLECS_API
142void ecs_logv_(
143 int level,
144 const char *file,
145 int32_t line,
146 const char *fmt,
147 va_list args);
148
149FLECS_API
150void ecs_abort_(
151 int32_t error_code,
152 const char *file,
153 int32_t line,
154 const char *fmt,
155 ...);
156
157FLECS_API
158void ecs_assert_log_(
159 int32_t error_code,
160 const char *condition_str,
161 const char *file,
162 int32_t line,
163 const char *fmt,
164 ...);
165
166FLECS_API
167void ecs_parser_error_(
168 const char *name,
169 const char *expr,
170 int64_t column,
171 const char *fmt,
172 ...);
173
174FLECS_API
175void ecs_parser_errorv_(
176 const char *name,
177 const char *expr,
178 int64_t column,
179 const char *fmt,
180 va_list args);
181
182
186
187#ifndef FLECS_LEGACY /* C89 doesn't support variadic macros */
188
189/* Base logging function. Accepts a custom level */
190#define ecs_print(level, ...)\
191 ecs_print_(level, __FILE__, __LINE__, __VA_ARGS__)
192
193#define ecs_printv(level, fmt, args)\
194 ecs_printv_(level, __FILE__, __LINE__, fmt, args)
195
196#define ecs_log(level, ...)\
197 ecs_log_(level, __FILE__, __LINE__, __VA_ARGS__)
198
199#define ecs_logv(level, fmt, args)\
200 ecs_logv_(level, __FILE__, __LINE__, fmt, args)
201
202/* Tracing. Used for logging of infrequent events */
203#define ecs_trace_(file, line, ...) ecs_log_(0, file, line, __VA_ARGS__)
204#define ecs_trace(...) ecs_trace_(__FILE__, __LINE__, __VA_ARGS__)
205
206/* Warning. Used when an issue occurs, but operation is successful */
207#define ecs_warn_(file, line, ...) ecs_log_(-2, file, line, __VA_ARGS__)
208#define ecs_warn(...) ecs_warn_(__FILE__, __LINE__, __VA_ARGS__)
209
210/* Error. Used when an issue occurs, and operation failed. */
211#define ecs_err_(file, line, ...) ecs_log_(-3, file, line, __VA_ARGS__)
212#define ecs_err(...) ecs_err_(__FILE__, __LINE__, __VA_ARGS__)
213
214/* Fatal. Used when an issue occurs, and the application cannot continue. */
215#define ecs_fatal_(file, line, ...) ecs_log_(-4, file, line, __VA_ARGS__)
216#define ecs_fatal(...) ecs_fatal_(__FILE__, __LINE__, __VA_ARGS__)
217
218/* Optionally include warnings about using deprecated features */
219#ifndef FLECS_NO_DEPRECATED_WARNINGS
220#define ecs_deprecated(...)\
221 ecs_deprecated_(__FILE__, __LINE__, __VA_ARGS__)
222#else
223#define ecs_deprecated(...)
224#endif // FLECS_NO_DEPRECATED_WARNINGS
225
226/* If no tracing verbosity is defined, pick default based on build config */
227#if !(defined(FLECS_LOG_0) || defined(FLECS_LOG_1) || defined(FLECS_LOG_2) || defined(FLECS_LOG_3))
228#if !defined(FLECS_NDEBUG)
229#define FLECS_LOG_3 /* Enable all tracing in debug mode. May slow things down */
230#else
231#define FLECS_LOG_0 /* Only enable infrequent tracing in release mode */
232#endif // !defined(FLECS_NDEBUG)
233#endif // !(defined(FLECS_LOG_0) || defined(FLECS_LOG_1) || defined(FLECS_LOG_2) || defined(FLECS_LOG_3))
234
235
236/* Define/undefine macros based on compiled-in tracing level. This can optimize
237 * out tracing statements from a build, which improves performance. */
238
239#if defined(FLECS_LOG_3) /* All debug tracing enabled */
240#define ecs_dbg_1(...) ecs_log(1, __VA_ARGS__);
241#define ecs_dbg_2(...) ecs_log(2, __VA_ARGS__);
242#define ecs_dbg_3(...) ecs_log(3, __VA_ARGS__);
243
244#define ecs_log_push_1() ecs_log_push_(1);
245#define ecs_log_push_2() ecs_log_push_(2);
246#define ecs_log_push_3() ecs_log_push_(3);
247
248#define ecs_log_pop_1() ecs_log_pop_(1);
249#define ecs_log_pop_2() ecs_log_pop_(2);
250#define ecs_log_pop_3() ecs_log_pop_(3);
251
252#define ecs_should_log_1() ecs_should_log(1)
253#define ecs_should_log_2() ecs_should_log(2)
254#define ecs_should_log_3() ecs_should_log(3)
255
256#define FLECS_LOG_2
257#define FLECS_LOG_1
258#define FLECS_LOG_0
259
260#elif defined(FLECS_LOG_2) /* Level 2 and below debug tracing enabled */
261#define ecs_dbg_1(...) ecs_log(1, __VA_ARGS__);
262#define ecs_dbg_2(...) ecs_log(2, __VA_ARGS__);
263#define ecs_dbg_3(...)
264
265#define ecs_log_push_1() ecs_log_push_(1);
266#define ecs_log_push_2() ecs_log_push_(2);
267#define ecs_log_push_3()
268
269#define ecs_log_pop_1() ecs_log_pop_(1);
270#define ecs_log_pop_2() ecs_log_pop_(2);
271#define ecs_log_pop_3()
272
273#define ecs_should_log_1() ecs_should_log(1)
274#define ecs_should_log_2() ecs_should_log(2)
275#define ecs_should_log_3() false
276
277#define FLECS_LOG_1
278#define FLECS_LOG_0
279
280#elif defined(FLECS_LOG_1) /* Level 1 debug tracing enabled */
281#define ecs_dbg_1(...) ecs_log(1, __VA_ARGS__);
282#define ecs_dbg_2(...)
283#define ecs_dbg_3(...)
284
285#define ecs_log_push_1() ecs_log_push_(1);
286#define ecs_log_push_2()
287#define ecs_log_push_3()
288
289#define ecs_log_pop_1() ecs_log_pop_(1);
290#define ecs_log_pop_2()
291#define ecs_log_pop_3()
292
293#define ecs_should_log_1() ecs_should_log(1)
294#define ecs_should_log_2() false
295#define ecs_should_log_3() false
296
297#define FLECS_LOG_0
298
299#elif defined(FLECS_LOG_0) /* No debug tracing enabled */
300#define ecs_dbg_1(...)
301#define ecs_dbg_2(...)
302#define ecs_dbg_3(...)
303
304#define ecs_log_push_1()
305#define ecs_log_push_2()
306#define ecs_log_push_3()
307
308#define ecs_log_pop_1()
309#define ecs_log_pop_2()
310#define ecs_log_pop_3()
311
312#define ecs_should_log_1() false
313#define ecs_should_log_2() false
314#define ecs_should_log_3() false
315
316#else /* No tracing enabled */
317#undef ecs_trace
318#define ecs_trace(...)
319#define ecs_dbg_1(...)
320#define ecs_dbg_2(...)
321#define ecs_dbg_3(...)
322
323#define ecs_log_push_1()
324#define ecs_log_push_2()
325#define ecs_log_push_3()
326
327#define ecs_log_pop_1()
328#define ecs_log_pop_2()
329#define ecs_log_pop_3()
330
331#endif // defined(FLECS_LOG_3)
332
333/* Default debug tracing is at level 1 */
334#define ecs_dbg ecs_dbg_1
335
336/* Default level for push/pop is 0 */
337#define ecs_log_push() ecs_log_push_(0)
338#define ecs_log_pop() ecs_log_pop_(0)
339
342#define ecs_abort(error_code, ...)\
343 ecs_abort_(error_code, __FILE__, __LINE__, __VA_ARGS__);\
344 ecs_os_abort(); abort(); /* satisfy compiler/static analyzers */
345
348#if defined(FLECS_NDEBUG) && !defined(FLECS_KEEP_ASSERT)
349#define ecs_assert(condition, error_code, ...)
350#else
351#define ecs_assert(condition, error_code, ...)\
352 if (!(condition)) {\
353 ecs_assert_log_(error_code, #condition, __FILE__, __LINE__, __VA_ARGS__);\
354 ecs_os_abort();\
355 }\
356 assert(condition) /* satisfy compiler/static analyzers */
357#endif // FLECS_NDEBUG
358
359#define ecs_assert_var(var, error_code, ...)\
360 ecs_assert(var, error_code, __VA_ARGS__);\
361 (void)var
362
365#ifndef FLECS_NDEBUG
366#define ecs_dbg_assert(condition, error_code, ...) ecs_assert(condition, error_code, __VA_ARGS__)
367#else
368#define ecs_dbg_assert(condition, error_code, ...)
369#endif
370
373#ifdef FLECS_SANITIZE
374#define ecs_san_assert(condition, error_code, ...) ecs_assert(condition, error_code, __VA_ARGS__)
375#else
376#define ecs_san_assert(condition, error_code, ...)
377#endif
378
379
380/* Silence dead code/unused label warnings when compiling without checks. */
381#define ecs_dummy_check\
382 if ((false)) {\
383 goto error;\
384 }
385
388#if defined(FLECS_NDEBUG) && !defined(FLECS_KEEP_ASSERT)
389#define ecs_check(condition, error_code, ...) ecs_dummy_check
390#else
391#ifdef FLECS_SOFT_ASSERT
392#define ecs_check(condition, error_code, ...)\
393 if (!(condition)) {\
394 ecs_assert_log_(error_code, #condition, __FILE__, __LINE__, __VA_ARGS__);\
395 goto error;\
396 }
397#else // FLECS_SOFT_ASSERT
398#define ecs_check(condition, error_code, ...)\
399 ecs_assert(condition, error_code, __VA_ARGS__);\
400 ecs_dummy_check
401#endif
402#endif // FLECS_NDEBUG
403
406#if defined(FLECS_NDEBUG) && !defined(FLECS_KEEP_ASSERT)
407#define ecs_throw(error_code, ...) ecs_dummy_check
408#else
409#ifdef FLECS_SOFT_ASSERT
410#define ecs_throw(error_code, ...)\
411 ecs_abort_(error_code, __FILE__, __LINE__, __VA_ARGS__);\
412 goto error;
413#else
414#define ecs_throw(error_code, ...)\
415 ecs_abort(error_code, __VA_ARGS__);\
416 ecs_dummy_check
417#endif
418#endif // FLECS_NDEBUG
419
421#define ecs_parser_error(name, expr, column, ...)\
422 ecs_parser_error_(name, expr, column, __VA_ARGS__)
423
424#define ecs_parser_errorv(name, expr, column, fmt, args)\
425 ecs_parser_errorv_(name, expr, column, fmt, args)
426
427#endif // FLECS_LEGACY
428
429
433
452FLECS_API
454 int level);
455
460FLECS_API
462
469FLECS_API
471 bool enabled);
472
480FLECS_API
482 bool enabled);
483
497FLECS_API
499 bool enabled);
500
506FLECS_API
508
509
513
514#define ECS_INVALID_OPERATION (1)
515#define ECS_INVALID_PARAMETER (2)
516#define ECS_CONSTRAINT_VIOLATED (3)
517#define ECS_OUT_OF_MEMORY (4)
518#define ECS_OUT_OF_RANGE (5)
519#define ECS_UNSUPPORTED (6)
520#define ECS_INTERNAL_ERROR (7)
521#define ECS_ALREADY_DEFINED (8)
522#define ECS_MISSING_OS_API (9)
523#define ECS_OPERATION_FAILED (10)
524#define ECS_INVALID_CONVERSION (11)
525#define ECS_ID_IN_USE (12)
526#define ECS_CYCLE_DETECTED (13)
527#define ECS_LEAK_DETECTED (14)
528#define ECS_DOUBLE_FREE (15)
529
530#define ECS_INCONSISTENT_NAME (20)
531#define ECS_NAME_IN_USE (21)
532#define ECS_NOT_A_COMPONENT (22)
533#define ECS_INVALID_COMPONENT_SIZE (23)
534#define ECS_INVALID_COMPONENT_ALIGNMENT (24)
535#define ECS_COMPONENT_NOT_REGISTERED (25)
536#define ECS_INCONSISTENT_COMPONENT_ID (26)
537#define ECS_INCONSISTENT_COMPONENT_ACTION (27)
538#define ECS_MODULE_UNDEFINED (28)
539#define ECS_MISSING_SYMBOL (29)
540#define ECS_ALREADY_IN_USE (30)
541
542#define ECS_ACCESS_VIOLATION (40)
543#define ECS_COLUMN_INDEX_OUT_OF_RANGE (41)
544#define ECS_COLUMN_IS_NOT_SHARED (42)
545#define ECS_COLUMN_IS_SHARED (43)
546#define ECS_COLUMN_TYPE_MISMATCH (45)
547
548#define ECS_INVALID_WHILE_READONLY (70)
549#define ECS_LOCKED_STORAGE (71)
550#define ECS_INVALID_FROM_WORKER (72)
551
552
556
557#define ECS_BLACK "\033[1;30m"
558#define ECS_RED "\033[0;31m"
559#define ECS_GREEN "\033[0;32m"
560#define ECS_YELLOW "\033[0;33m"
561#define ECS_BLUE "\033[0;34m"
562#define ECS_MAGENTA "\033[0;35m"
563#define ECS_CYAN "\033[0;36m"
564#define ECS_WHITE "\033[1;37m"
565#define ECS_GREY "\033[0;37m"
566#define ECS_NORMAL "\033[0;49m"
567#define ECS_BOLD "\033[1;49m"
568
569#ifdef __cplusplus
570}
571#endif
572
575#endif // FLECS_LOG_H
FLECS_API int ecs_log_last_error(void)
Get last logged error code.
FLECS_API void ecs_log_pop_(int32_t level)
Decrease log stack.
FLECS_API bool ecs_should_log(int32_t level)
Should current level be logged.
FLECS_API const char * ecs_strerror(int32_t error_code)
Get description for error code.
FLECS_API void ecs_log_push_(int32_t level)
Increase log stack.
FLECS_API bool ecs_log_enable_colors(bool enabled)
Enable/disable tracing with colors.
FLECS_API int ecs_log_get_level(void)
Get current log level.
FLECS_API int ecs_log_set_level(int level)
Enable or disable log.
FLECS_API bool ecs_log_enable_timestamp(bool enabled)
Enable/disable logging timestamp.
FLECS_API bool ecs_log_enable_timedelta(bool enabled)
Enable/disable logging time since last log.