fixed error message generation

This commit is contained in:
Timerix 2025-07-22 15:13:10 +05:00
parent d3a8c03fe5
commit 447c15bc46
5 changed files with 18 additions and 17 deletions

View File

@ -5,13 +5,13 @@
typedef struct ErrorCallPos { typedef struct ErrorCallPos {
i32 line; i32 line;
str file; cstr file;
str func; cstr func;
} ErrorCallPos; } ErrorCallPos;
#define ErrorCallPos_here() (ErrorCallPos){\ #define ErrorCallPos_here() (ErrorCallPos){\
.line = __LINE__,\ .line = __LINE__,\
.file = str_construct((char*)(void*)__FILE__, sizeof(__FILE__), true),\ .file = __FILE__,\
.func = str_construct((char*)(void*)__func__, sizeof(__func__), true)\ .func = __func__\
} }
typedef struct Error { typedef struct Error {
@ -21,7 +21,7 @@ typedef struct Error {
List call_stack; List call_stack;
} Error; } Error;
Error* Error_create(const char* msg, bool is_msg_on_heap, ErrorCallPos); Error* Error_create(const char* msg, bool is_msg_on_heap, ErrorCallPos p);
void Error_destroy(Error* e); void Error_destroy(Error* e);
void Error_addCallPos(Error* e, ErrorCallPos p); void Error_addCallPos(Error* e, ErrorCallPos p);
str Error_toStr(Error* e); str Error_toStr(Error* e);
@ -56,6 +56,7 @@ typedef struct Result_ {
#define try_fatal(VAR, RSLT_CALL, DEFER_CODE) \ #define try_fatal(VAR, RSLT_CALL, DEFER_CODE) \
Result_ VAR = RSLT_CALL;\ Result_ VAR = RSLT_CALL;\
if(VAR.error){\ if(VAR.error){\
Error_addCallPos(VAR.error, ErrorCallPos_here());\
DEFER_CODE;\ DEFER_CODE;\
Error_printAndExit(VAR.error);\ Error_printAndExit(VAR.error);\
}; };

View File

@ -16,7 +16,7 @@ void StringBuilder_destroy(StringBuilder* b);
/// @param count set to -1 to clear StringBuilder /// @param count set to -1 to clear StringBuilder
void StringBuilder_removeFromEnd(StringBuilder* b, u32 count); void StringBuilder_removeFromEnd(StringBuilder* b, u32 count);
void StringBuilder_append_char(StringBuilder* b, char c); void StringBuilder_append_char(StringBuilder* b, char c);
void StringBuilder_append_cstr(StringBuilder* b, char* s); void StringBuilder_append_cstr(StringBuilder* b, cstr s);
void StringBuilder_append_str(StringBuilder* b, str s); void StringBuilder_append_str(StringBuilder* b, str s);
void StringBuilder_append_i64(StringBuilder* b, i64 a); void StringBuilder_append_i64(StringBuilder* b, i64 a);
void StringBuilder_append_u64(StringBuilder* b, u64 a); void StringBuilder_append_u64(StringBuilder* b, u64 a);

View File

@ -3,11 +3,12 @@
#define ERRMSG_LENGTH 1024 #define ERRMSG_LENGTH 1024
Error* Error_create(const char* msg, bool is_msg_on_heap, ErrorCallPos){ Error* Error_create(const char* msg, bool is_msg_on_heap, ErrorCallPos p){
Error* e = (Error*)malloc(sizeof(Error)); Error* e = (Error*)malloc(sizeof(Error));
e->msg = str_construct((char*)(void*)msg, strlen(msg), true); e->msg = str_construct((char*)(void*)msg, strlen(msg), true);
e->is_msg_on_heap = is_msg_on_heap; e->is_msg_on_heap = is_msg_on_heap;
e->call_stack = List_alloc(ErrorCallPos, 1); e->call_stack = List_alloc(ErrorCallPos, 16);
List_push(&e->call_stack, ErrorCallPos, p);
return e; return e;
} }
@ -30,12 +31,12 @@ str Error_toStr(Error* e){
for(u32 i = 0; i < len; i++){ for(u32 i = 0; i < len; i++){
ErrorCallPos* ep = (ErrorCallPos*)e->call_stack.data + i; ErrorCallPos* ep = (ErrorCallPos*)e->call_stack.data + i;
StringBuilder_append_str(&b, STR("\n at ")); StringBuilder_append_str(&b, STR("\n at "));
StringBuilder_append_str(&b, ep->file); StringBuilder_append_cstr(&b, ep->file);
StringBuilder_append_char(&b, ':'); StringBuilder_append_char(&b, ':');
StringBuilder_append_i64(&b, ep->line); StringBuilder_append_i64(&b, ep->line);
StringBuilder_append_char(&b, ' '); StringBuilder_append_char(&b, ' ');
StringBuilder_append_str(&b, ep->func); StringBuilder_append_cstr(&b, ep->func);
StringBuilder_append_str(&b, STR("()\n")); StringBuilder_append_str(&b, STR("()"));
} }
return StringBuilder_getStr(&b); return StringBuilder_getStr(&b);
@ -43,7 +44,6 @@ str Error_toStr(Error* e){
void Error_printAndExit(Error* e){ void Error_printAndExit(Error* e){
Error_addCallPos(e, ErrorCallPos_here());\
str e_str = Error_toStr(e); str e_str = Error_toStr(e);
printfe("%s\n", e_str.data); printfe("%s\n", e_str.data);
free(e_str.data); free(e_str.data);

View File

@ -33,8 +33,8 @@ void StringBuilder_append_str(StringBuilder* b, str s){
List_push_size(&b->buffer, s.data, s.size); List_push_size(&b->buffer, s.data, s.size);
} }
void StringBuilder_append_cstr(StringBuilder* b, char* s){ void StringBuilder_append_cstr(StringBuilder* b, cstr s){
StringBuilder_append_str(b, str_construct(s, strlen(s), true)); StringBuilder_append_str(b, str_construct((char*)(void*)s, strlen(s), true));
} }
void StringBuilder_append_i64(StringBuilder* b, i64 n){ void StringBuilder_append_i64(StringBuilder* b, i64 n){

View File

@ -17,14 +17,14 @@
nsec_t getTimeNsec(){ nsec_t getTimeNsec(){
struct timespec t; struct timespec t;
assert(clock_gettime(CLOCK_REALTIME, &t) != 0); assert(clock_gettime(CLOCK_REALTIME, &t) == 0);
u64 n = t.tv_sec * G + t.tv_nsec; u64 n = t.tv_sec * G + t.tv_nsec;
return n; return n;
} }
usec_t getTimeUsec(){ usec_t getTimeUsec(){
struct timespec t; struct timespec t;
assert(clock_gettime(CLOCK_REALTIME, &t) != 0); assert(clock_gettime(CLOCK_REALTIME, &t) == 0);
u64 n = t.tv_sec * M + t.tv_nsec / K; u64 n = t.tv_sec * M + t.tv_nsec / K;
return n; return n;
} }
@ -34,7 +34,7 @@ void sleepNsec(nsec_t time){
.tv_sec = time / G, .tv_sec = time / G,
.tv_nsec = time % G .tv_nsec = time % G
}; };
assert(nanosleep(&t, NULL) != 0); assert(nanosleep(&t, NULL) == 0);
} }
void sleepUsec(usec_t time){ void sleepUsec(usec_t time){