add logger history

This commit is contained in:
2026-05-19 18:43:57 -05:00
parent fbb618711f
commit 964425e7b8
3 changed files with 38 additions and 16 deletions

29
LLog.c
View File

@@ -16,6 +16,17 @@ int timer_array_append(timer_array* array, lTimer timer) {
return array->count - 1;
}
int lLogString_array_append(lLogString_array* array, lLogString* string) {
if (array->count >= array->capacity) {
if (array->capacity == 0) array->capacity = 8;
else array->capacity *= 2;
array->items = realloc(array->items, array->capacity * sizeof(*array->items));
}
array->items[array->count++] = string;
return array->count - 1;
}
int create_timer(Logger* logger) {
return timer_array_append(&logger->timers, (lTimer){});
}
@@ -30,7 +41,7 @@ void stop_timer(Logger* logger, int timer_id) {
logger->timers.items[timer_id].time.end = time(NULL);
}
lLogString lLogString_new(const char* fmt, ...) {
lLogString* lLogString_new(const char* fmt, ...) {
va_list args;
va_start(args, fmt);
@@ -39,22 +50,24 @@ lLogString lLogString_new(const char* fmt, ...) {
va_end(args);
lLogString str;
str.len = strlen(temp_string);
lLogString* str = calloc(1, sizeof(lLogString));
str->len = strlen(temp_string);
str.buf = malloc(str.len + 1);
if (str.buf != NULL) {
strcpy(str.buf, temp_string);
str->buf = malloc(str->len + 1);
if (str->buf != NULL) {
strcpy(str->buf, temp_string);
}
return str;
};
void lLog(Logger logger, enum lLogLevel LOG_LEVEL, lLogString MESSAGE) {
int lLog(Logger* logger, enum lLogLevel LOG_LEVEL, lLogString* MESSAGE) {
char* LOG_LEVEL_STRING = LOG_LEVEL == 0 ? "INFO" :
LOG_LEVEL == 1 ? "WARNING" :
LOG_LEVEL == 2 ? "ERROR" :
LOG_LEVEL == 3 ? "DEBUG" :
"invalid_log_level";
printf("[%s]: %.*s\n", LOG_LEVEL_STRING, (int)MESSAGE.len, MESSAGE.buf);
printf("[%s]: %.*s\n", LOG_LEVEL_STRING, (int)MESSAGE->len, MESSAGE->buf);
return lLogString_array_append(&logger->history, MESSAGE);
}