Flecs v4.0
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
50FLECS_API
52 const char *file,
53 int32_t line,
54 const char *msg);
55
62FLECS_API
63void ecs_log_push_(int32_t level);
64
71FLECS_API
72void ecs_log_pop_(int32_t level);
73
81FLECS_API
82bool ecs_should_log(int32_t level);
83
87
89FLECS_API
90const char* ecs_strerror(
91 int32_t error_code);
92
93#else // FLECS_LOG
94
98
99#define ecs_deprecated_(file, line, msg)\
100 (void)file;\
101 (void)line;\
102 (void)msg
103
104#define ecs_log_push_(level)
105#define ecs_log_pop_(level)
106#define ecs_should_log(level) false
107
108#define ecs_strerror(error_code)\
109 (void)error_code
110
111#endif // FLECS_LOG
112
113
117
118FLECS_API
119void ecs_print_(
120 int32_t level,
121 const char *file,
122 int32_t line,
123 const char *fmt,
124 ...);
125
126FLECS_API
127void ecs_printv_(
128 int level,
129 const char *file,
130 int32_t line,
131 const char *fmt,
132 va_list args);
133
134FLECS_API
135void ecs_log_(
136 int32_t level,
137 const char *file,
138 int32_t line,
139 const char *fmt,
140 ...);
141
142FLECS_API
143void ecs_logv_(
144 int level,
145 const char *file,
146 int32_t line,
147 const char *fmt,
148 va_list args);
149
150FLECS_API
151void ecs_abort_(
152 int32_t error_code,
153 const char *file,
154 int32_t line,
155 const char *fmt,
156 ...);
157
158FLECS_API
159void ecs_assert_log_(
160 int32_t error_code,
161 const char *condition_str,
162 const char *file,
163 int32_t line,
164 const char *fmt,
165 ...);
166
167FLECS_API
168void ecs_parser_error_(
169 const char *name,
170 const char *expr,
171 int64_t column,
172 const char *fmt,
173 ...);
174
175FLECS_API
176void ecs_parser_errorv_(
177 const char *name,
178 const char *expr,
179 int64_t column,
180 const char *fmt,
181 va_list args);
182
183FLECS_API
184void ecs_parser_warning_(
185 const char *name,
186 const char *expr,
187 int64_t column,
188 const char *fmt,
189 ...);
190
191FLECS_API
192void ecs_parser_warningv_(
193 const char *name,
194 const char *expr,
195 int64_t column,
196 const char *fmt,
197 va_list args);
198
199
203
204#ifndef FLECS_LEGACY /* C89 doesn't support variadic macros */
205
206/* Base logging function. Accepts a custom level */
207#define ecs_print(level, ...)\
208 ecs_print_(level, __FILE__, __LINE__, __VA_ARGS__)
209
210#define ecs_printv(level, fmt, args)\
211 ecs_printv_(level, __FILE__, __LINE__, fmt, args)
212
213#define ecs_log(level, ...)\
214 ecs_log_(level, __FILE__, __LINE__, __VA_ARGS__)
215
216#define ecs_logv(level, fmt, args)\
217 ecs_logv_(level, __FILE__, __LINE__, fmt, args)
218
219/* Tracing. Used for logging of infrequent events */
220#define ecs_trace_(file, line, ...) ecs_log_(0, file, line, __VA_ARGS__)
221#define ecs_trace(...) ecs_trace_(__FILE__, __LINE__, __VA_ARGS__)
222
223/* Warning. Used when an issue occurs, but operation is successful */
224#define ecs_warn_(file, line, ...) ecs_log_(-2, file, line, __VA_ARGS__)
225#define ecs_warn(...) ecs_warn_(__FILE__, __LINE__, __VA_ARGS__)
226
227/* Error. Used when an issue occurs, and operation failed. */
228#define ecs_err_(file, line, ...) ecs_log_(-3, file, line, __VA_ARGS__)
229#define ecs_err(...) ecs_err_(__FILE__, __LINE__, __VA_ARGS__)
230
231/* Fatal. Used when an issue occurs, and the application cannot continue. */
232#define ecs_fatal_(file, line, ...) ecs_log_(-4, file, line, __VA_ARGS__)
233#define ecs_fatal(...) ecs_fatal_(__FILE__, __LINE__, __VA_ARGS__)
234
235/* Optionally include warnings about using deprecated features */
236#ifndef FLECS_NO_DEPRECATED_WARNINGS
237#define ecs_deprecated(...)\
238 ecs_deprecated_(__FILE__, __LINE__, __VA_ARGS__)
239#else
240#define ecs_deprecated(...)
241#endif // FLECS_NO_DEPRECATED_WARNINGS
242
243/* If no tracing verbosity is defined, pick default based on build config */
244#if !(defined(FLECS_LOG_0) || defined(FLECS_LOG_1) || defined(FLECS_LOG_2) || defined(FLECS_LOG_3))
245#if !defined(FLECS_NDEBUG)
246#define FLECS_LOG_3 /* Enable all tracing in debug mode. May slow things down */
247#else
248#define FLECS_LOG_0 /* Only enable infrequent tracing in release mode */
249#endif // !defined(FLECS_NDEBUG)
250#endif // !(defined(FLECS_LOG_0) || defined(FLECS_LOG_1) || defined(FLECS_LOG_2) || defined(FLECS_LOG_3))
251
252
253/* Define/undefine macros based on compiled-in tracing level. This can optimize
254 * out tracing statements from a build, which improves performance. */
255
256#if defined(FLECS_LOG_3) /* All debug tracing enabled */
257#define ecs_dbg_1(...) ecs_log(1, __VA_ARGS__);
258#define ecs_dbg_2(...) ecs_log(2, __VA_ARGS__);
259#define ecs_dbg_3(...) ecs_log(3, __VA_ARGS__);
260
261#define ecs_log_push_1() ecs_log_push_(1);
262#define ecs_log_push_2() ecs_log_push_(2);
263#define ecs_log_push_3() ecs_log_push_(3);
264
265#define ecs_log_pop_1() ecs_log_pop_(1);
266#define ecs_log_pop_2() ecs_log_pop_(2);
267#define ecs_log_pop_3() ecs_log_pop_(3);
268
269#define ecs_should_log_1() ecs_should_log(1)
270#define ecs_should_log_2() ecs_should_log(2)
271#define ecs_should_log_3() ecs_should_log(3)
272
273#define FLECS_LOG_2
274#define FLECS_LOG_1
275#define FLECS_LOG_0
276
277#elif defined(FLECS_LOG_2) /* Level 2 and below debug tracing enabled */
278#define ecs_dbg_1(...) ecs_log(1, __VA_ARGS__);
279#define ecs_dbg_2(...) ecs_log(2, __VA_ARGS__);
280#define ecs_dbg_3(...)
281
282#define ecs_log_push_1() ecs_log_push_(1);
283#define ecs_log_push_2() ecs_log_push_(2);
284#define ecs_log_push_3()
285
286#define ecs_log_pop_1() ecs_log_pop_(1);
287#define ecs_log_pop_2() ecs_log_pop_(2);
288#define ecs_log_pop_3()
289
290#define ecs_should_log_1() ecs_should_log(1)
291#define ecs_should_log_2() ecs_should_log(2)
292#define ecs_should_log_3() false
293
294#define FLECS_LOG_1
295#define FLECS_LOG_0
296
297#elif defined(FLECS_LOG_1) /* Level 1 debug tracing enabled */
298#define ecs_dbg_1(...) ecs_log(1, __VA_ARGS__);
299#define ecs_dbg_2(...)
300#define ecs_dbg_3(...)
301
302#define ecs_log_push_1() ecs_log_push_(1);
303#define ecs_log_push_2()
304#define ecs_log_push_3()
305
306#define ecs_log_pop_1() ecs_log_pop_(1);
307#define ecs_log_pop_2()
308#define ecs_log_pop_3()
309
310#define ecs_should_log_1() ecs_should_log(1)
311#define ecs_should_log_2() false
312#define ecs_should_log_3() false
313
314#define FLECS_LOG_0
315
316#elif defined(FLECS_LOG_0) /* No debug tracing enabled */
317#define ecs_dbg_1(...)
318#define ecs_dbg_2(...)
319#define ecs_dbg_3(...)
320
321#define ecs_log_push_1()
322#define ecs_log_push_2()
323#define ecs_log_push_3()
324
325#define ecs_log_pop_1()
326#define ecs_log_pop_2()
327#define ecs_log_pop_3()
328
329#define ecs_should_log_1() false
330#define ecs_should_log_2() false
331#define ecs_should_log_3() false
332
333#else /* No tracing enabled */
334#undef ecs_trace
335#define ecs_trace(...)
336#define ecs_dbg_1(...)
337#define ecs_dbg_2(...)
338#define ecs_dbg_3(...)
339
340#define ecs_log_push_1()
341#define ecs_log_push_2()
342#define ecs_log_push_3()
343
344#define ecs_log_pop_1()
345#define ecs_log_pop_2()
346#define ecs_log_pop_3()
347
348#endif // defined(FLECS_LOG_3)
349
350/* Default debug tracing is at level 1 */
351#define ecs_dbg ecs_dbg_1
352
353/* Default level for push/pop is 0 */
354#define ecs_log_push() ecs_log_push_(0)
355#define ecs_log_pop() ecs_log_pop_(0)
356
359#define ecs_abort(error_code, ...)\
360 ecs_abort_(error_code, __FILE__, __LINE__, __VA_ARGS__);\
361 ecs_os_abort(); abort(); /* satisfy compiler/static analyzers */
362
365#if defined(FLECS_NDEBUG) && !defined(FLECS_KEEP_ASSERT)
366#define ecs_assert(condition, error_code, ...)
367#else
368#define ecs_assert(condition, error_code, ...)\
369 if (!(condition)) {\
370 ecs_assert_log_(error_code, #condition, __FILE__, __LINE__, __VA_ARGS__);\
371 ecs_os_abort();\
372 }\
373 assert(condition) /* satisfy compiler/static analyzers */
374#endif // FLECS_NDEBUG
375
376#define ecs_assert_var(var, error_code, ...)\
377 ecs_assert(var, error_code, __VA_ARGS__);\
378 (void)var
379
382#ifndef FLECS_NDEBUG
383#define ecs_dbg_assert(condition, error_code, ...) ecs_assert(condition, error_code, __VA_ARGS__)
384#else
385#define ecs_dbg_assert(condition, error_code, ...)
386#endif
387
390#ifdef FLECS_SANITIZE
391#define ecs_san_assert(condition, error_code, ...) ecs_assert(condition, error_code, __VA_ARGS__)
392#else
393#define ecs_san_assert(condition, error_code, ...)
394#endif
395
396
397/* Silence dead code/unused label warnings when compiling without checks. */
398#define ecs_dummy_check\
399 if ((false)) {\
400 goto error;\
401 }
402
405#if defined(FLECS_NDEBUG) && !defined(FLECS_KEEP_ASSERT)
406#define ecs_check(condition, error_code, ...) ecs_dummy_check
407#else
408#ifdef FLECS_SOFT_ASSERT
409#define ecs_check(condition, error_code, ...)\
410 if (!(condition)) {\
411 ecs_assert_log_(error_code, #condition, __FILE__, __LINE__, __VA_ARGS__);\
412 goto error;\
413 }
414#else // FLECS_SOFT_ASSERT
415#define ecs_check(condition, error_code, ...)\
416 ecs_assert(condition, error_code, __VA_ARGS__);\
417 ecs_dummy_check
418#endif
419#endif // FLECS_NDEBUG
420
423#if defined(FLECS_NDEBUG) && !defined(FLECS_KEEP_ASSERT)
424#define ecs_throw(error_code, ...) ecs_dummy_check
425#else
426#ifdef FLECS_SOFT_ASSERT
427#define ecs_throw(error_code, ...)\
428 ecs_abort_(error_code, __FILE__, __LINE__, __VA_ARGS__);\
429 goto error;
430#else
431#define ecs_throw(error_code, ...)\
432 ecs_abort(error_code, __VA_ARGS__);\
433 ecs_dummy_check
434#endif
435#endif // FLECS_NDEBUG
436
438#define ecs_parser_error(name, expr, column, ...)\
439 ecs_parser_error_(name, expr, column, __VA_ARGS__)
440
441#define ecs_parser_errorv(name, expr, column, fmt, args)\
442 ecs_parser_errorv_(name, expr, column, fmt, args)
443
444#define ecs_parser_warning(name, expr, column, ...)\
445 ecs_parser_warning_(name, expr, column, __VA_ARGS__)
446
447#define ecs_parser_warningv(name, expr, column, fmt, args)\
448 ecs_parser_warningv_(name, expr, column, fmt, args)
449
450#endif // FLECS_LEGACY
451
452
456
475FLECS_API
477 int level);
478
483FLECS_API
485
492FLECS_API
494 bool enabled);
495
503FLECS_API
505 bool enabled);
506
520FLECS_API
522 bool enabled);
523
529FLECS_API
531
532
536
537#define ECS_INVALID_OPERATION (1)
538#define ECS_INVALID_PARAMETER (2)
539#define ECS_CONSTRAINT_VIOLATED (3)
540#define ECS_OUT_OF_MEMORY (4)
541#define ECS_OUT_OF_RANGE (5)
542#define ECS_UNSUPPORTED (6)
543#define ECS_INTERNAL_ERROR (7)
544#define ECS_ALREADY_DEFINED (8)
545#define ECS_MISSING_OS_API (9)
546#define ECS_OPERATION_FAILED (10)
547#define ECS_INVALID_CONVERSION (11)
548#define ECS_ID_IN_USE (12)
549#define ECS_CYCLE_DETECTED (13)
550#define ECS_LEAK_DETECTED (14)
551#define ECS_DOUBLE_FREE (15)
552
553#define ECS_INCONSISTENT_NAME (20)
554#define ECS_NAME_IN_USE (21)
555#define ECS_NOT_A_COMPONENT (22)
556#define ECS_INVALID_COMPONENT_SIZE (23)
557#define ECS_INVALID_COMPONENT_ALIGNMENT (24)
558#define ECS_COMPONENT_NOT_REGISTERED (25)
559#define ECS_INCONSISTENT_COMPONENT_ID (26)
560#define ECS_INCONSISTENT_COMPONENT_ACTION (27)
561#define ECS_MODULE_UNDEFINED (28)
562#define ECS_MISSING_SYMBOL (29)
563#define ECS_ALREADY_IN_USE (30)
564
565#define ECS_ACCESS_VIOLATION (40)
566#define ECS_COLUMN_INDEX_OUT_OF_RANGE (41)
567#define ECS_COLUMN_IS_NOT_SHARED (42)
568#define ECS_COLUMN_IS_SHARED (43)
569#define ECS_COLUMN_TYPE_MISMATCH (45)
570
571#define ECS_INVALID_WHILE_READONLY (70)
572#define ECS_LOCKED_STORAGE (71)
573#define ECS_INVALID_FROM_WORKER (72)
574
575
579
580#define ECS_BLACK "\033[1;30m"
581#define ECS_RED "\033[0;31m"
582#define ECS_GREEN "\033[0;32m"
583#define ECS_YELLOW "\033[0;33m"
584#define ECS_BLUE "\033[0;34m"
585#define ECS_MAGENTA "\033[0;35m"
586#define ECS_CYAN "\033[0;36m"
587#define ECS_WHITE "\033[1;37m"
588#define ECS_GREY "\033[0;37m"
589#define ECS_NORMAL "\033[0;49m"
590#define ECS_BOLD "\033[1;49m"
591
592#ifdef __cplusplus
593}
594#endif
595
598#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_deprecated_(const char *file, int32_t line, const char *msg)
Log message indicating an operation is deprecated.
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.