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
9long long lg_log_time();
10
11/* Adding timestamps to error logs on Linux */
12/*
13 * TODO: it would be actually more correct to change:
14 * - TIME_FILE_LINE_FUNC_STR to TIME_FILE_LINE_FUNC
15 * and
16 * - TIME_FILE_LINE_FUNC_STR2 to TIME_FILE_LINE_FUNC_STR
17 * but:
18 * - There is already (uncorrectly) FILE_LINE_FUNC_STR in libetm.h
19 * which should be also changed
20 * - It's not working so far
21 */
22#ifndef ANDROID_V
23 #define TIME_FILE_LINE_FUNC_STR "%lld [%s: %d] %s(): ", lg_log_time(), __FILE__ , __LINE__, __func__
24 // STH WRONG HERE - #define TIME_FILE_LINE_FUNC_STR2 "[%lld %s: %d] %s(): %s", lg_log_time(), __FILE__ , __LINE__, __func__
25 #define INFO_ERR2(...) \
26 {\
27 fprintf(STD_ERR, TIME_FILE_LINE_FUNC_STR);\
28 fprintf(STD_ERR, __VA_ARGS__);\
29 fflush(STD_ERR);\
30 }
31#endif
32
33#define LG_ERR_CTX_FILE_MAXLEN 63
34#define LG_ERR_CTX_FUNC_MAXLEN 63
35#define LG_ERR_CTX_TXT_MAXLEN 1023
36#define LG_ERR_CTX_CODE_NA (-100000) /* Really not sure what to pick */
37#define LG_ERR_CTX_STR_MAXLEN 2048 /* Must contain all of the above - for snprintf() */
38#define LG_ERR_CTX_FORMAT "[%s: %d] %s(): %s (code = %d)"
39
40typedef struct {
41 char file[LG_ERR_CTX_FILE_MAXLEN + 1]; /* Not the full path */
42 unsigned int line;
43 char func[LG_ERR_CTX_FUNC_MAXLEN + 1];
44 char txt[LG_ERR_CTX_TXT_MAXLEN + 1];
45 int code;
47
48enum {OOM_MALLOC2_TEST, OOM_MALLOC3_TEST, OOM_MALLOC3_TEST2, INVPTR_FREE2_TEST, INVPTR_FREE3_TEST, SEGFAULT_TEST, DIVBYZERO_TEST};
49
51
53
54void lg_set_error_context(const char *, unsigned int, const char *, const char *, int);
55
57
58const char *lg_get_full_error_context();
59
61
63
65
66/*
67 * Wrappers for these mem alloc funcs
68 */
69void *malloc2_plus(size_t, const char *, unsigned int, const char *); /* size , __FILE__, __LINE__, __func__ */
70#define malloc3(...) malloc2_plus(__VA_ARGS__, __FILE__, __LINE__, __func__)
71
72/* Nothing done here apart from a #define -> need to implement this properly */
73#define realloc3 realloc2
74
75#define calloc3 calloc2
76
77void free2_plus(void *, const char *, unsigned int, const char *); /* mem , __FILE__, __LINE__, __func__ */
78/* Prev version
79#define free3(...) free2_plus(__VA_ARGS__, __FILE__, __LINE__, __func__)*/
80/* New version - to be tested, seems OK so far ... */
81#define free2_ultimate(...) free2_plus(__VA_ARGS__, __FILE__, __LINE__, __func__) /* Thats's silly */
82#define free3(mem_block) {free2_ultimate(mem_block); mem_block = NULL;}
83
84void free_v2(void **);
85
86/* DO THIS MAKE SENSE ???? - to be tested */
87#define l_str_free2(string) {l_str_free(string); string = NULL;}
88
89/*
90 * Wrappers for these file ops funcs
91 */
92/* === BUGGY SO NOT USED SO FAR === */
93int mkdir_plus(const char *, mode_t, const char *, unsigned int, const char *); /* path, mode, __FILE__, __LINE__, __func__ */
94//#define mkdir_2(...) mkdir_plus(__VA_ARGS__, __FILE__, __LINE__, __func__)
95#define mkdir_2(...) mkdir(__VA_ARGS__)
96
97/* === BUGGY SO NOT USED SO FAR === */
98FILE *fopen_plus(const char *, const char *, const char *, unsigned int, const char *); /* path, mode, __FILE__, __LINE__, __func__ */
99//#define fopen_2(...) fopen_plus(__VA_ARGS__, __FILE__, __LINE__, __func__)
100#define fopen_2(...) fopen(__VA_ARGS__)
101/*
102void *SDL_LoadFile_2(pathname, &size);
103size_t SDL_RWread_2(SDL_RWops *, void *, size_t, size_t);
104size_t SDL_RWwrite_2(SDL_RWops *, void *, size_t, size_t);
105int SDL_RWclose_2(SDL_RWops *);
106SDL_RWops* SDL_RWFromFile_2(const char *, const char *);
107*/
108
109#ifndef WIN32_V
110void sig_handler(int, siginfo_t *, void *);
111
112void set_sig_handler();
113
114extern void app_sig_handler(int, const char *);
115#endif
116
118
119void test_big_error(int);
120
121#endif /* LG_ERROR_H */
void free_v2(void **mem)
Definition lg_error.c:308
void set_sig_handler()
Definition lg_error.c:423
void lg_set_error_context(const char *file, unsigned int line, const char *func, const char *txt, int code)
Definition lg_error.c:147
const char * lg_get_full_error_context()
Definition lg_error.c:171
FILE * fopen_plus(const char *path, const char *mode, const char *file, unsigned int line, const char *func)
Definition lg_error.c:342
void lg_override_warning_timeout(int timeout)
Definition lg_error.c:119
void free2_plus(void *mem, const char *file, unsigned int line, const char *func)
Definition lg_error.c:280
void lg_clear_error_context()
Definition lg_error.c:184
void * malloc2_plus(size_t size, const char *file, unsigned int line, const char *func)
Definition lg_error.c:257
int lg_get_error_context_code()
Definition lg_error.c:161
void sig_handler(int sig_num, siginfo_t *sig_info, void *context)
Definition lg_error.c:396
void test_big_error(int test)
Definition lg_error.c:501
void lg_print_out_error_codes()
Definition lg_error.c:479
int mkdir_plus(const char *path, mode_t mode, const char *file, unsigned int line, const char *func)
Definition lg_error.c:323
long long lg_log_time()
Definition lg_error.c:135
void lg_reset_warning_timeout()
Definition lg_error.c:127
void lg_restore_error_context(LG_ErrorContext *err_ctx)
Definition lg_error.c:208
void lg_save_error_context(LG_ErrorContext *err_ctx)
Definition lg_error.c:200
Definition lg_error.h:40