Flecs: include/flecs/addons/log.h Source File
Description: A fast entity component system (ECS) for C & C++

| Flecs v4.1 A fast entity component system (ECS) for C & C++

Loading...
Searching...
No Matches
log.h
Go to the documentation of this file.
1
27
28#ifndef FLECS_LOG_H
29#define FLECS_LOG_H
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
35#ifdef FLECS_LOG
36
44
48
55FLECS_API
56 void ecs_deprecated_(
57 const char *file,
58 int32_t line,
59 const char *msg);
60
67FLECS_API
68 void ecs_log_push_(int32_t level);
69
76FLECS_API
77 void ecs_log_pop_(int32_t level);
78
86FLECS_API
87 bool ecs_should_log(int32_t level);
88
92
98FLECS_API
99 const char* ecs_strerror(
100 int32_t error_code);
101
102#else // FLECS_LOG
103
107
108#define ecs_deprecated_(file, line, msg)\
109 (void)file;\
110 (void)line;\
111 (void)msg
112
113#define ecs_log_push_(level)
114#define ecs_log_pop_(level)
115#define ecs_should_log(level) false
116
117#define ecs_strerror(error_code)\
118 (void)error_code
119
120#endif // FLECS_LOG
121
122
126
134FLECS_API
135 void ecs_print_(
136 int32_t level,
137 const char *file,
138 int32_t line,
139 const char *fmt,
140 ...);
141
150FLECS_API
151 void ecs_printv_(
152 int level,
153 const char *file,
154 int32_t line,
155 const char *fmt,
156 va_list args);
157
165FLECS_API
166 void ecs_log_(
167 int32_t level,
168 const char *file,
169 int32_t line,
170 const char *fmt,
171 ...);
172
181FLECS_API
182 void ecs_logv_(
183 int level,
184 const char *file,
185 int32_t line,
186 const char *fmt,
187 va_list args);
188
196FLECS_API
197 void ecs_abort_(
198 int32_t error_code,
199 const char *file,
200 int32_t line,
201 const char *fmt,
202 ...);
203
212FLECS_API
213 void ecs_assert_log_(
214 int32_t error_code,
215 const char *condition_str,
216 const char *file,
217 int32_t line,
218 const char *fmt,
219 ...);
220
228FLECS_API
229 void ecs_parser_error_(
230 const char *name,
231 const char *expr,
232 int64_t column,
233 const char *fmt,
234 ...);
235
244FLECS_API
245 void ecs_parser_errorv_(
246 const char *name,
247 const char *expr,
248 int64_t column,
249 const char *fmt,
250 va_list args);
251
259FLECS_API
260 void ecs_parser_warning_(
261 const char *name,
262 const char *expr,
263 int64_t column,
264 const char *fmt,
265 ...);
266
275FLECS_API
276 void ecs_parser_warningv_(
277 const char *name,
278 const char *expr,
279 int64_t column,
280 const char *fmt,
281 va_list args);
282
283
287
288#ifndef FLECS_LEGACY /* C89 doesn't support variadic macros */
289
291#define ecs_print(level, ...)\
292 ecs_print_(level, __FILE__, __LINE__, __VA_ARGS__)
293
295#define ecs_printv(level, fmt, args)\
296 ecs_printv_(level, __FILE__, __LINE__, fmt, args)
297
299#define ecs_log(level, ...)\
300 ecs_log_(level, __FILE__, __LINE__, __VA_ARGS__)
301
303#define ecs_logv(level, fmt, args)\
304 ecs_logv_(level, __FILE__, __LINE__, fmt, args)
305
307#define ecs_trace_(file, line, ...) ecs_log_(0, file, line, __VA_ARGS__)
309#define ecs_trace(...) ecs_trace_(__FILE__, __LINE__, __VA_ARGS__)
310
312#define ecs_warn_(file, line, ...) ecs_log_(-2, file, line, __VA_ARGS__)
314#define ecs_warn(...) ecs_warn_(__FILE__, __LINE__, __VA_ARGS__)
315
317#define ecs_err_(file, line, ...) ecs_log_(-3, file, line, __VA_ARGS__)
319#define ecs_err(...) ecs_err_(__FILE__, __LINE__, __VA_ARGS__)
320
322#define ecs_fatal_(file, line, ...) ecs_log_(-4, file, line, __VA_ARGS__)
324#define ecs_fatal(...) ecs_fatal_(__FILE__, __LINE__, __VA_ARGS__)
325
327#ifndef FLECS_NO_DEPRECATED_WARNINGS
329#define ecs_deprecated(...)\
330 ecs_deprecated_(__FILE__, __LINE__, __VA_ARGS__)
331#else
332#define ecs_deprecated(...)
333#endif // FLECS_NO_DEPRECATED_WARNINGS
334
335/* If no tracing verbosity is defined, pick default based on build config */
336#if !(defined(FLECS_LOG_0) || defined(FLECS_LOG_1) || defined(FLECS_LOG_2) || defined(FLECS_LOG_3))
337#if !defined(FLECS_NDEBUG)
338#define FLECS_LOG_3 /* Enable all tracing in debug mode. May slow things down */
339#else
340#define FLECS_LOG_0 /* Only enable infrequent tracing in release mode */
341#endif // !defined(FLECS_NDEBUG)
342#endif // !(defined(FLECS_LOG_0) || defined(FLECS_LOG_1) || defined(FLECS_LOG_2) || defined(FLECS_LOG_3))
343
344
345/* Define/undefine macros based on compiled-in tracing level. This can optimize
346 * out tracing statements from a build, which improves performance. */
347
348#if defined(FLECS_LOG_3) /* All debug tracing enabled */
350#define ecs_dbg_1(...) ecs_log(1, __VA_ARGS__);
352#define ecs_dbg_2(...) ecs_log(2, __VA_ARGS__);
354#define ecs_dbg_3(...) ecs_log(3, __VA_ARGS__);
355
357#define ecs_log_push_1() ecs_log_push_(1);
359#define ecs_log_push_2() ecs_log_push_(2);
361#define ecs_log_push_3() ecs_log_push_(3);
362
364#define ecs_log_pop_1() ecs_log_pop_(1);
366#define ecs_log_pop_2() ecs_log_pop_(2);
368#define ecs_log_pop_3() ecs_log_pop_(3);
369
371#define ecs_should_log_1() ecs_should_log(1)
373#define ecs_should_log_2() ecs_should_log(2)
375#define ecs_should_log_3() ecs_should_log(3)
376
377#define FLECS_LOG_2
378#define FLECS_LOG_1
379#define FLECS_LOG_0
380
381#elif defined(FLECS_LOG_2) /* Level 2 and below debug tracing enabled */
382#define ecs_dbg_1(...) ecs_log(1, __VA_ARGS__);
383#define ecs_dbg_2(...) ecs_log(2, __VA_ARGS__);
384#define ecs_dbg_3(...)
385
386#define ecs_log_push_1() ecs_log_push_(1);
387#define ecs_log_push_2() ecs_log_push_(2);
388#define ecs_log_push_3()
389
390#define ecs_log_pop_1() ecs_log_pop_(1);
391#define ecs_log_pop_2() ecs_log_pop_(2);
392#define ecs_log_pop_3()
393
394#define ecs_should_log_1() ecs_should_log(1)
395#define ecs_should_log_2() ecs_should_log(2)
396#define ecs_should_log_3() false
397
398#define FLECS_LOG_1
399#define FLECS_LOG_0
400
401#elif defined(FLECS_LOG_1) /* Level 1 debug tracing enabled */
402#define ecs_dbg_1(...) ecs_log(1, __VA_ARGS__);
403#define ecs_dbg_2(...)
404#define ecs_dbg_3(...)
405
406#define ecs_log_push_1() ecs_log_push_(1);
407#define ecs_log_push_2()
408#define ecs_log_push_3()
409
410#define ecs_log_pop_1() ecs_log_pop_(1);
411#define ecs_log_pop_2()
412#define ecs_log_pop_3()
413
414#define ecs_should_log_1() ecs_should_log(1)
415#define ecs_should_log_2() false
416#define ecs_should_log_3() false
417
418#define FLECS_LOG_0
419
420#elif defined(FLECS_LOG_0) /* No debug tracing enabled */
421#define ecs_dbg_1(...)
422#define ecs_dbg_2(...)
423#define ecs_dbg_3(...)
424
425#define ecs_log_push_1()
426#define ecs_log_push_2()
427#define ecs_log_push_3()
428
429#define ecs_log_pop_1()
430#define ecs_log_pop_2()
431#define ecs_log_pop_3()
432
433#define ecs_should_log_1() false
434#define ecs_should_log_2() false
435#define ecs_should_log_3() false
436
437#else /* No tracing enabled */
438#undef ecs_trace
439#define ecs_trace(...)
440#define ecs_dbg_1(...)
441#define ecs_dbg_2(...)
442#define ecs_dbg_3(...)
443
444#define ecs_log_push_1()
445#define ecs_log_push_2()
446#define ecs_log_push_3()
447
448#define ecs_log_pop_1()
449#define ecs_log_pop_2()
450#define ecs_log_pop_3()
451
452#endif // defined(FLECS_LOG_3)
453
455#define ecs_dbg ecs_dbg_1
456
458#define ecs_log_push() ecs_log_push_(0)
460#define ecs_log_pop() ecs_log_pop_(0)
461
464#define ecs_abort(error_code, ...)\
465 ecs_abort_(error_code, __FILE__, __LINE__, __VA_ARGS__);\
466 ecs_os_abort(); abort(); /* satisfy compiler/static analyzers */
467
470#if defined(FLECS_NDEBUG) && !defined(FLECS_KEEP_ASSERT)
471#define ecs_assert(condition, error_code, ...)
472#else
473#define ecs_assert(condition, error_code, ...)\
474 if (!(condition)) {\
475 ecs_assert_log_(error_code, #condition, __FILE__, __LINE__, __VA_ARGS__);\
476 ecs_os_abort();\
477 }\
478 assert(condition) /* satisfy compiler/static analyzers */
479#endif // FLECS_NDEBUG
480
483#ifndef FLECS_NDEBUG
484#define ecs_dbg_assert(condition, error_code, ...) ecs_assert(condition, error_code, __VA_ARGS__)
485#else
486#define ecs_dbg_assert(condition, error_code, ...)
487#endif
488
492#define ecs_always_assert(condition, error_code, ...)\
493 if (!(condition)) {\
494 ecs_assert_log_(error_code, #condition, __FILE__, __LINE__, __VA_ARGS__);\
495 ecs_os_abort();\
496 }\
497 assert(condition) /* satisfy compiler/static analyzers */
498
501#ifdef FLECS_SANITIZE
502#define ecs_san_assert(condition, error_code, ...) ecs_assert(condition, error_code, __VA_ARGS__)
503#else
504#define ecs_san_assert(condition, error_code, ...)
505#endif
506
507
509#define ecs_dummy_check\
510 if ((false)) {\
511 goto error;\
512 }
513
516#if defined(FLECS_NDEBUG) && !defined(FLECS_KEEP_ASSERT)
517#define ecs_check(condition, error_code, ...) ecs_dummy_check
518#else
519#ifdef FLECS_SOFT_ASSERT
520#define ecs_check(condition, error_code, ...)\
521 if (!(condition)) {\
522 ecs_assert_log_(error_code, #condition, __FILE__, __LINE__, __VA_ARGS__);\
523 goto error;\
524 }\
525 ecs_dummy_check
526#else // FLECS_SOFT_ASSERT
527#define ecs_check(condition, error_code, ...)\
528 if (!(condition)) {\
529 ecs_assert_log_(error_code, #condition, __FILE__, __LINE__, __VA_ARGS__);\
530 ecs_os_abort();\
531 }\
532 assert(condition); /* satisfy compiler/static analyzers */ \
533 ecs_dummy_check
534#endif
535#endif // FLECS_NDEBUG
536
539#if defined(FLECS_NDEBUG) && !defined(FLECS_KEEP_ASSERT)
540#define ecs_throw(error_code, ...) ecs_dummy_check
541#else
542#ifdef FLECS_SOFT_ASSERT
543#define ecs_throw(error_code, ...)\
544 ecs_abort_(error_code, __FILE__, __LINE__, __VA_ARGS__);\
545 goto error;
546#else
547#define ecs_throw(error_code, ...)\
548 ecs_abort(error_code, __VA_ARGS__);\
549 ecs_dummy_check
550#endif
551#endif // FLECS_NDEBUG
552
554#define ecs_parser_error(name, expr, column, ...)\
555 ecs_parser_error_(name, expr, column, __VA_ARGS__)
556
558#define ecs_parser_errorv(name, expr, column, fmt, args)\
559 ecs_parser_errorv_(name, expr, column, fmt, args)
560
562#define ecs_parser_warning(name, expr, column, ...)\
563 ecs_parser_warning_(name, expr, column, __VA_ARGS__)
564
566#define ecs_parser_warningv(name, expr, column, fmt, args)\
567 ecs_parser_warningv_(name, expr, column, fmt, args)
568
569#endif // FLECS_LEGACY
570
574
593FLECS_API
594 int ecs_log_set_level(
595 int level);
596
601FLECS_API
602 int ecs_log_get_level(void);
603
610FLECS_API
611 bool ecs_log_enable_colors(
612 bool enabled);
613
621FLECS_API
622 bool ecs_log_enable_timestamp(
623 bool enabled);
624
638FLECS_API
639 bool ecs_log_enable_timedelta(
640 bool enabled);
641
647FLECS_API
648 int ecs_log_last_error(void);
649
654FLECS_API
655 void ecs_log_start_capture(bool capture_try);
656
661FLECS_API
662 char* ecs_log_stop_capture(void);
663
667
669#define ECS_INVALID_OPERATION (1)
671#define ECS_INVALID_PARAMETER (2)
673#define ECS_CONSTRAINT_VIOLATED (3)
675#define ECS_OUT_OF_MEMORY (4)
677#define ECS_OUT_OF_RANGE (5)
679#define ECS_UNSUPPORTED (6)
681#define ECS_INTERNAL_ERROR (7)
683#define ECS_ALREADY_DEFINED (8)
685#define ECS_MISSING_OS_API (9)
687#define ECS_OPERATION_FAILED (10)
689#define ECS_INVALID_CONVERSION (11)
691#define ECS_CYCLE_DETECTED (13)
693#define ECS_LEAK_DETECTED (14)
695#define ECS_DOUBLE_FREE (15)
696
698#define ECS_INCONSISTENT_NAME (20)
700#define ECS_NAME_IN_USE (21)
702#define ECS_INVALID_COMPONENT_SIZE (23)
704#define ECS_INVALID_COMPONENT_ALIGNMENT (24)
706#define ECS_COMPONENT_NOT_REGISTERED (25)
708#define ECS_INCONSISTENT_COMPONENT_ID (26)
710#define ECS_INCONSISTENT_COMPONENT_ACTION (27)
712#define ECS_MODULE_UNDEFINED (28)
714#define ECS_MISSING_SYMBOL (29)
716#define ECS_ALREADY_IN_USE (30)
717
719#define ECS_ACCESS_VIOLATION (40)
721#define ECS_COLUMN_INDEX_OUT_OF_RANGE (41)
723#define ECS_COLUMN_IS_NOT_SHARED (42)
725#define ECS_COLUMN_IS_SHARED (43)
727#define ECS_COLUMN_TYPE_MISMATCH (45)
728
730#define ECS_INVALID_WHILE_READONLY (70)
732#define ECS_LOCKED_STORAGE (71)
734#define ECS_INVALID_FROM_WORKER (72)
735
736
740
742#define ECS_BLACK "\033[1;30m"
744#define ECS_RED "\033[0;31m"
746#define ECS_GREEN "\033[0;32m"
748#define ECS_YELLOW "\033[0;33m"
750#define ECS_BLUE "\033[0;34m"
752#define ECS_MAGENTA "\033[0;35m"
754#define ECS_CYAN "\033[0;36m"
756#define ECS_WHITE "\033[1;37m"
758#define ECS_GREY "\033[0;37m"
760#define ECS_NORMAL "\033[0;49m"
762#define ECS_BOLD "\033[1;49m"
763
764#ifdef __cplusplus
765}
766#endif
767
769
770#endif // FLECS_LOG_H
ecs_log_stop_capture
FLECS_API char * ecs_log_stop_capture(void)
Stop capturing log output.
ecs_log_last_error
FLECS_API int ecs_log_last_error(void)
Get last logged error code.
ecs_log_pop_
FLECS_API void ecs_log_pop_(int32_t level)
Decrease log stack.
ecs_parser_warningv_
FLECS_API void ecs_parser_warningv_(const char *name, const char *expr, int64_t column, const char *fmt, va_list args)
Log a parser warning (va_list).
ecs_abort_
FLECS_API void ecs_abort_(int32_t error_code, const char *file, int32_t line, const char *fmt,...)
Abort with error code.
ecs_log_start_capture
FLECS_API void ecs_log_start_capture(bool capture_try)
Start capturing log output.
ecs_should_log
FLECS_API bool ecs_should_log(int32_t level)
Should current level be logged.
ecs_printv_
FLECS_API void ecs_printv_(int level, const char *file, int32_t line, const char *fmt, va_list args)
Print at the provided log level (va_list).
ecs_log_
FLECS_API void ecs_log_(int32_t level, const char *file, int32_t line, const char *fmt,...)
Log at the provided level.
ecs_assert_log_
FLECS_API void ecs_assert_log_(int32_t error_code, const char *condition_str, const char *file, int32_t line, const char *fmt,...)
Log an assertion failure.
ecs_print_
FLECS_API void ecs_print_(int32_t level, const char *file, int32_t line, const char *fmt,...)
Print at the provided log level.
ecs_strerror
FLECS_API const char * ecs_strerror(int32_t error_code)
Get description for error code.
ecs_deprecated_
FLECS_API void ecs_deprecated_(const char *file, int32_t line, const char *msg)
Log message indicating an operation is deprecated.
ecs_log_push_
FLECS_API void ecs_log_push_(int32_t level)
Increase log stack.
ecs_parser_warning_
FLECS_API void ecs_parser_warning_(const char *name, const char *expr, int64_t column, const char *fmt,...)
Log a parser warning.
ecs_logv_
FLECS_API void ecs_logv_(int level, const char *file, int32_t line, const char *fmt, va_list args)
Log at the provided level (va_list).
ecs_log_enable_colors
FLECS_API bool ecs_log_enable_colors(bool enabled)
Enable/disable tracing with colors.
ecs_parser_error_
FLECS_API void ecs_parser_error_(const char *name, const char *expr, int64_t column, const char *fmt,...)
Log a parser error.
ecs_log_get_level
FLECS_API int ecs_log_get_level(void)
Get current log level.
ecs_parser_errorv_
FLECS_API void ecs_parser_errorv_(const char *name, const char *expr, int64_t column, const char *fmt, va_list args)
Log a parser error (va_list).
ecs_log_set_level
FLECS_API int ecs_log_set_level(int level)
Enable or disable log.
ecs_log_enable_timestamp
FLECS_API bool ecs_log_enable_timestamp(bool enabled)
Enable/disable logging timestamp.
ecs_log_enable_timedelta
FLECS_API bool ecs_log_enable_timedelta(bool enabled)
Enable/disable logging time since last log.