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 {
i32 line;
str file;
str func;
cstr file;
cstr func;
} ErrorCallPos;
#define ErrorCallPos_here() (ErrorCallPos){\
.line = __LINE__,\
.file = str_construct((char*)(void*)__FILE__, sizeof(__FILE__), true),\
.func = str_construct((char*)(void*)__func__, sizeof(__func__), true)\
.file = __FILE__,\
.func = __func__\
}
typedef struct Error {
@ -21,7 +21,7 @@ typedef struct Error {
List call_stack;
} 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_addCallPos(Error* e, ErrorCallPos p);
str Error_toStr(Error* e);
@ -56,6 +56,7 @@ typedef struct Result_ {
#define try_fatal(VAR, RSLT_CALL, DEFER_CODE) \
Result_ VAR = RSLT_CALL;\
if(VAR.error){\
Error_addCallPos(VAR.error, ErrorCallPos_here());\
DEFER_CODE;\
Error_printAndExit(VAR.error);\
};

View File

@ -16,7 +16,7 @@ void StringBuilder_destroy(StringBuilder* b);
/// @param count set to -1 to clear StringBuilder
void StringBuilder_removeFromEnd(StringBuilder* b, u32 count);
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_i64(StringBuilder* b, i64 a);
void StringBuilder_append_u64(StringBuilder* b, u64 a);

View File

@ -3,11 +3,12 @@
#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));
e->msg = str_construct((char*)(void*)msg, strlen(msg), true);
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;
}
@ -30,12 +31,12 @@ str Error_toStr(Error* e){
for(u32 i = 0; i < len; i++){
ErrorCallPos* ep = (ErrorCallPos*)e->call_stack.data + i;
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_i64(&b, ep->line);
StringBuilder_append_char(&b, ' ');
StringBuilder_append_str(&b, ep->func);
StringBuilder_append_str(&b, STR("()\n"));
StringBuilder_append_cstr(&b, ep->func);
StringBuilder_append_str(&b, STR("()"));
}
return StringBuilder_getStr(&b);
@ -43,7 +44,6 @@ str Error_toStr(Error* e){
void Error_printAndExit(Error* e){
Error_addCallPos(e, ErrorCallPos_here());\
str e_str = Error_toStr(e);
printfe("%s\n", 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);
}
void StringBuilder_append_cstr(StringBuilder* b, char* s){
StringBuilder_append_str(b, str_construct(s, strlen(s), true));
void StringBuilder_append_cstr(StringBuilder* b, cstr s){
StringBuilder_append_str(b, str_construct((char*)(void*)s, strlen(s), true));
}
void StringBuilder_append_i64(StringBuilder* b, i64 n){

View File

@ -17,14 +17,14 @@
nsec_t getTimeNsec(){
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;
return n;
}
usec_t getTimeUsec(){
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;
return n;
}
@ -34,7 +34,7 @@ void sleepNsec(nsec_t time){
.tv_sec = time / G,
.tv_nsec = time % G
};
assert(nanosleep(&t, NULL) != 0);
assert(nanosleep(&t, NULL) == 0);
}
void sleepUsec(usec_t time){