LibGame  v0.4.0
The LG Game Engine - Copyright (C) 2024 ETMSoftware
lg_error.h
1 /*
2  * LibGame - Copyright (C) Emmanuel Thomas-Maurin 2011-2024
3  * All rights reserved
4  */
5 
6 #ifndef LG_ERROR_H
7 #define LG_ERROR_H
8 
9 long 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 
40 typedef 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 
48 enum {OOM_MALLOC2_TEST, OOM_MALLOC3_TEST, INVPTR_FREE2_TEST, INVPTR_FREE3_TEST, SEGFAULT_TEST, DIVBYZERO_TEST};
49 
50 void lg_set_error_context(const char *, unsigned int, const char *, const char *, int);
51 
53 
54 const char *lg_get_full_error_context();
55 
57 
59 
61 
62 /*
63  * Wrappers for these mem alloc funcs
64  */
65 void *malloc2_plus(size_t, const char *, unsigned int, const char *); /* size , __FILE__, __LINE__, __func__ */
66 #define malloc3(...) malloc2_plus(__VA_ARGS__, __FILE__, __LINE__, __func__)
67 
68 void free2_plus(void *, const char *, unsigned int, const char *); /* mem , __FILE__, __LINE__, __func__ */
69 /* Prev version
70 #define free3(...) free2_plus(__VA_ARGS__, __FILE__, __LINE__, __func__)*/
71 /* New version - to be tested, seems OK so far ... */
72 #define free2_ultimate(...) free2_plus(__VA_ARGS__, __FILE__, __LINE__, __func__) /* Thats's silly */
73 #define free3(mem_block) {free2_ultimate(mem_block); mem_block = NULL;}
74 
75 /*
76  * Wrappers for these file ops funcs
77  */
78 /* === BUGGY SO NOT USED SO FAR === */
79 int mkdir_plus(const char *, mode_t, const char *, unsigned int, const char *); /* path, mode, __FILE__, __LINE__, __func__ */
80 //#define mkdir_2(...) mkdir_plus(__VA_ARGS__, __FILE__, __LINE__, __func__)
81 #define mkdir_2(...) mkdir(__VA_ARGS__)
82 
83 /* === BUGGY SO NOT USED SO FAR === */
84 FILE *fopen_plus(const char *, const char *, const char *, unsigned int, const char *); /* path, mode, __FILE__, __LINE__, __func__ */
85 //#define fopen_2(...) fopen_plus(__VA_ARGS__, __FILE__, __LINE__, __func__)
86 #define fopen_2(...) fopen(__VA_ARGS__)
87 /*
88 void *SDL_LoadFile_2(pathname, &size);
89 size_t SDL_RWread_2(SDL_RWops *, void *, size_t, size_t);
90 size_t SDL_RWwrite_2(SDL_RWops *, void *, size_t, size_t);
91 int SDL_RWclose_2(SDL_RWops *);
92 SDL_RWops* SDL_RWFromFile_2(const char *, const char *);
93 */
94 
95 #ifndef WIN32_V
96 void sig_handler(int, siginfo_t *, void *);
97 
98 void set_sig_handler();
99 #endif
100 
101 void test_big_error(int);
102 
103 #endif /* LG_ERROR_H */
test_big_error
void test_big_error(int test)
Definition: lg_error.c:388
lg_set_error_context
void lg_set_error_context(const char *file, unsigned int line, const char *func, const char *txt, int code)
Definition: lg_error.c:128
mkdir_plus
int mkdir_plus(const char *path, mode_t mode, const char *file, unsigned int line, const char *func)
Definition: lg_error.c:263
set_sig_handler
void set_sig_handler()
Definition: lg_error.c:362
lg_save_error_context
void lg_save_error_context(LG_ErrorContext *err_ctx)
Definition: lg_error.c:181
lg_log_time
long long lg_log_time()
Definition: lg_error.c:116
sig_handler
void sig_handler(int sig_num, siginfo_t *sig_info, void *context)
Definition: lg_error.c:306
lg_get_full_error_context
const char * lg_get_full_error_context()
Definition: lg_error.c:152
malloc2_plus
void * malloc2_plus(size_t size, const char *file, unsigned int line, const char *func)
Definition: lg_error.c:230
lg_clear_error_context
void lg_clear_error_context()
Definition: lg_error.c:165
LG_ErrorContext
Definition: lg_error.h:40
free2_plus
void free2_plus(void *mem, const char *file, unsigned int line, const char *func)
Definition: lg_error.c:247
lg_get_error_context_code
int lg_get_error_context_code()
Definition: lg_error.c:142
lg_restore_error_context
void lg_restore_error_context(LG_ErrorContext *err_ctx)
Definition: lg_error.c:189
fopen_plus
FILE * fopen_plus(const char *path, const char *mode, const char *file, unsigned int line, const char *func)
Definition: lg_error.c:282