From fbb618711f34b148d484ad48561e1efa0e416af6 Mon Sep 17 00:00:00 2001 From: lucielle Date: Tue, 19 May 2026 18:33:37 -0500 Subject: [PATCH] add formatting to lLogString --- LLog.c | 22 ++++++++++++++++------ include/LLog.h | 5 ++++- test/test.c | 13 ++++++------- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/LLog.c b/LLog.c index 863e3ca..062a985 100644 --- a/LLog.c +++ b/LLog.c @@ -3,6 +3,7 @@ #include #include #include "time.h" +#include int timer_array_append(timer_array* array, lTimer timer) { if (array->count >= array->capacity) { @@ -29,14 +30,23 @@ void stop_timer(Logger* logger, int timer_id) { logger->timers.items[timer_id].time.end = time(NULL); } -lLogString to_lLogString(const char* string) { +lLogString lLogString_new(const char* fmt, ...) { + va_list args; + va_start(args, fmt); + + char temp_string[max_string_len]; + vsnprintf(temp_string, max_string_len, fmt, args); + + va_end(args); + lLogString str; - str.buf = (char *)string; - if (string == NULL) { - str.len = 0; - } else { - str.len = strlen(string); + str.len = strlen(temp_string); + + str.buf = malloc(str.len + 1); + if (str.buf != NULL) { + strcpy(str.buf, temp_string); } + return str; }; diff --git a/include/LLog.h b/include/LLog.h index 27355e6..a8e5e73 100644 --- a/include/LLog.h +++ b/include/LLog.h @@ -2,6 +2,9 @@ #define LLog_H #include "time.h" +#include + +#define max_string_len 1024 typedef struct { char *buf; @@ -41,7 +44,7 @@ int create_timer(Logger* logger); void start_timer(Logger* logger, int timer_id); void stop_timer(Logger* logger, int timer_id); -lLogString to_lLogString(const char* string); +lLogString lLogString_new(const char* fmt, ...); void lLog(Logger logger, enum lLogLevel LOG_LEVEL, lLogString MESSAGE); diff --git a/test/test.c b/test/test.c index 576588d..1e2d459 100644 --- a/test/test.c +++ b/test/test.c @@ -1,20 +1,19 @@ #include "../include/LLog.h" -#include #include int main(void) { Logger logger = {0}; - lLog(logger, lINFO, to_lLogString("Hello, INFO!")); - lLog(logger, lWARNING, to_lLogString("Hello, WARNING!")); - lLog(logger, lERROR, to_lLogString("Hello, ERROR!")); - lLog(logger, lDEBUG, to_lLogString("Hello, DEBUG!")); + lLog(logger, lINFO, lLogString_new("Hello, INFO!")); + lLog(logger, lWARNING, lLogString_new("Hello, WARNING!")); + lLog(logger, lERROR, lLogString_new("Hello, ERROR!")); + lLog(logger, lDEBUG, lLogString_new("Hello, DEBUG!")); int timer = create_timer(&logger); lTimespan* time_object = &logger.timers.items[timer].time; start_timer(&logger, timer); - sleep(5); + sleep(2); stop_timer(&logger, timer); - printf("\n%f seconds.\n", difftime(time_object->end, time_object->start)); + lLog(logger, lINFO, lLogString_new("%f seconds.", difftime(time_object->end, time_object->start))); } \ No newline at end of file