LibGame v0.4.0
The LG Game Engine - Copyright (C) 2024-2026 ETMSoftware
Loading...
Searching...
No Matches
lg_error.h
1/*
2 * LibGame - Copyright (C) Emmanuel Thomas-Maurin 2011-2026
3 * All rights reserved
4 */
5
6#ifndef LG_ERROR_H
7#define LG_ERROR_H
8
9/* GL errors checking */
10#define CHECK_GL_E lg_check_gl_errors((int64_t)lg_log_time(), __FILE__, __LINE__, __func__)
11
12#define CHECK_GL_E_2 {if (lg_check_gl_errors((int64_t)lg_log_time(), __FILE__, __LINE__, __func__) != LG_OK) exit(-1);}
13
14/*
15 * Checking for NULL pointers v2
16 *
17 * Usage:
18 * mem = malloc2(64 * 1024);
19 * __chkp__(mem, "mem", NULL)
20 */
21#define __chkp__(pointer, string, ret_value) {if (pointer == NULL) {INFO_ERR("%s == NULL\n", string); return ret_value;}}
22
23#define __chkp2__(pointer, string, exit_1) {if (pointer == NULL) {INFO_ERR("%s == NULL\n", string); goto exit_1;}}
24
25#define __chkv__(x, string_x, y, string_y, exit_1) {if (x == y) {INFO_ERR("%s == %s\n", string_x, string_y); goto exit_1;}}
26
27/*
28 * Debugging stuff
29 *
30 * Usage:
31 * // Do sth
32 * NESF(__LINE__)
33 */
34#define NESF(line_num) INFO_ERR("No errors so far ... (line %d)\n", line_num)
35
36/* Where is this used ? */
37#define STH_WRONG_HERE \
38 { \
39 static int i = 0; \
40 if (i == 0) { \
41 INFO_OUT("VARO TÄSSÄ\n%s [%s: %d] %s(): jotain pitää korjata\n", UTF8_RW_ROUNDED_ARROW, __FILE__, __LINE__, __func__) \
42 i++; \
43 } \
44 }
45
46/* Debugging stuff (trivial) */
47#define DEBUG_ENTER_FUNC INFO_OUT("\n\nDEBUG **** %s() ****\n\n", __func__)
48#define DEBUG_ENTER_FUNC2(str) INFO_OUT("\n\nDEBUG **** %s() -> %s ****\n\n", __func__, str)
49
50/* Adding timestamps to error logs on Linux */
51/*
52 * TODO: it would be actually more correct to change:
53 * - TIME_FILE_LINE_FUNC_STR to TIME_FILE_LINE_FUNC
54 * and
55 * - TIME_FILE_LINE_FUNC_STR2 to TIME_FILE_LINE_FUNC_STR
56 * but:
57 * - There is already (uncorrectly) FILE_LINE_FUNC_STR in libetm.h
58 * which should be also changed
59 * - It's not working so far
60 */
61
62uint64_t lg_log_time();
63
64#ifndef ANDROID_V
65 #define TIME_FILE_LINE_FUNC_STR "%ld [%s: %d] %s(): ", (int64_t)lg_log_time(), __FILE__, __LINE__, __func__
66 #define INFO_ERR2(...) \
67 {\
68 fprintf(STD_ERR, TIME_FILE_LINE_FUNC_STR);\
69 fprintf(STD_ERR, __VA_ARGS__);\
70 fflush(STD_ERR);\
71 }
72#endif
73
74#define LG_ERR_CTX_FILE_MAXLEN 63
75#define LG_ERR_CTX_FUNC_MAXLEN 63
76#define LG_ERR_CTX_TXT_MAXLEN 1023
77#define LG_ERR_CTX_CODE_NA (-100000) /* Really not sure what to pick */
78#define LG_ERR_CTX_STR_MAXLEN 2048 /* Must contain all of the above - for snprintf() */
79#define LG_ERR_CTX_FORMAT "[%s: %d] %s(): %s (code = %d)"
80
81typedef struct {
82 char file[LG_ERR_CTX_FILE_MAXLEN + 1]; /* Not the full path */
83 unsigned int line;
84 char func[LG_ERR_CTX_FUNC_MAXLEN + 1];
85 char txt[LG_ERR_CTX_TXT_MAXLEN + 1];
86 int code;
88
89/* This is actually an almost duplicate of Libetm/error.h: struct ErrSt (code/str) */
90typedef struct {
91 int code;
92 const char *txt;
94
95enum {OOM_MALLOC2_TEST, OOM_MALLOC3_TEST, OOM_MALLOC3_TEST2, INVPTR_FREE2_TEST, INVPTR_FREE3_TEST, SEGFAULT_TEST, DIVBYZERO_TEST};
96
98
100
101void lg_set_error_context(const char *, unsigned int, const char *, const char *, int);
102
104
105const char *lg_get_full_error_context();
106
108
110
112
113#ifndef WIN32_V
114void sig_handler(int, siginfo_t *, void *);
115
116void set_sig_handler();
117
118extern void app_sig_handler(int, const char *);
119#endif
120
122
123void test_big_error(int);
124
125#endif /* LG_ERROR_H */
void set_sig_handler()
Definition lg_error.c:390
void lg_set_error_context(const char *file, unsigned int line, const char *func, const char *txt, int code)
Definition lg_error.c:226
const char * lg_get_full_error_context()
Definition lg_error.c:250
void lg_override_warning_timeout(int timeout)
Definition lg_error.c:198
uint64_t lg_log_time()
Definition lg_error.c:214
void lg_clear_error_context()
Definition lg_error.c:263
int lg_get_error_context_code()
Definition lg_error.c:240
void sig_handler(int sig_num, siginfo_t *sig_info, void *context)
Definition lg_error.c:363
void test_big_error(int test)
Definition lg_error.c:470
void lg_print_out_error_codes()
Definition lg_error.c:448
void lg_reset_warning_timeout()
Definition lg_error.c:206
void lg_restore_error_context(LG_ErrorContext *err_ctx)
Definition lg_error.c:287
void lg_save_error_context(LG_ErrorContext *err_ctx)
Definition lg_error.c:279
Definition lg_error.h:81
Definition lg_error.h:90