add formatting to lLogString

This commit is contained in:
2026-05-19 18:33:37 -05:00
parent ba2df46189
commit fbb618711f
3 changed files with 26 additions and 14 deletions

22
LLog.c
View File

@@ -3,6 +3,7 @@
#include <stdio.h>
#include <stdlib.h>
#include "time.h"
#include <stdarg.h>
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;
};

View File

@@ -2,6 +2,9 @@
#define LLog_H
#include "time.h"
#include <stdarg.h>
#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);

View File

@@ -1,20 +1,19 @@
#include "../include/LLog.h"
#include <stdio.h>
#include <unistd.h>
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)));
}