Compare commits

..

8 Commits

Author SHA1 Message Date
a025ecdae4 Note to self: Be less trigger happy 2026-05-20 15:38:30 -05:00
4ad40539f2 Add version info for first release 2026-05-20 15:34:09 -05:00
73893fa132 make max_string_len modifyable 2026-05-20 15:29:16 -05:00
2ab8477d7d add log file functionality 2026-05-19 22:25:09 -05:00
964425e7b8 add logger history 2026-05-19 18:43:57 -05:00
fbb618711f add formatting to lLogString 2026-05-19 18:33:37 -05:00
ba2df46189 add logger object and timers 2026-05-19 01:16:39 -05:00
2d6ed0b99d create project structure 2026-05-18 16:21:05 -05:00
6 changed files with 293 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
build/
logs/*.log

107
LLog.c Normal file
View File

@@ -0,0 +1,107 @@
#include "include/LLog.h"
#include <string.h>
#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) {
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++] = 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;
}
Logger* Logger_new(lLogString* logfolder, int history_enabled) {
Logger* logger = calloc(1, sizeof(Logger));
logger->historyEnabled = history_enabled;
logger->logFileEnabled = 1;
time_t time_now = time(NULL);
char file_name[64];
strftime(file_name, sizeof(file_name), "%Y-%m-%d--%H:%M:%S.log", localtime(&time_now));
lLogString* logFile = lLogString_new("%.*s/%s", (int)logfolder->len, logfolder->buf, file_name);
logger->log_file = fopen(logFile->buf, "a");
if (logger->log_file == NULL) {
logger->logFileEnabled = 0;
lLog(logger,
lERROR,
lLogString_new("Could not open log file: \"%.*s\" Continuing without log file.", (int)logFile->len, logFile->buf)
);
}
return logger;
}
int create_timer(Logger* logger) {
return timer_array_append(&logger->timers, (lTimer){});
}
void start_timer(Logger* logger, int timer_id) {
if (logger->timers.count < (timer_id + 1)) return;
logger->timers.items[timer_id].time.start = time(NULL);
}
void stop_timer(Logger* logger, int timer_id) {
if (logger->timers.count < (timer_id + 1)) return;
logger->timers.items[timer_id].time.end = time(NULL);
}
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 = calloc(1, sizeof(lLogString));
str->len = strlen(temp_string);
str->buf = malloc(str->len + 1);
if (str->buf != NULL) {
strcpy(str->buf, temp_string);
}
return str;
};
int Logger_write_to_log(Logger* logger, lLogString* string) {
if (logger->logFileEnabled == 0 || logger->log_file == NULL) return 1;
fprintf(logger->log_file, "%.*s", (int)string->len, string->buf);
return 0;
}
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";
lLogString* formated_message = lLogString_new("[%s]: %.*s\n", LOG_LEVEL_STRING, (int)MESSAGE->len, MESSAGE->buf);
if (logger->logFileEnabled) Logger_write_to_log(logger, formated_message);
printf("%.*s", (int)formated_message->len, formated_message->buf);
return lLogString_array_append(&logger->history, formated_message);
}

91
Makefile Normal file
View File

@@ -0,0 +1,91 @@
# You may have to run this command or something similar
# while in the base directory to please the dynamic linker
#
# $ export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$PWD/build/dynamic"
CC = gcc
SHELL := /bin/bash
INCLUDE := include
CFLAGS = -Wall -I$(INCLUDE) -ggdb
BUILD_DIR := build
STATIC_BUILD_DIR := $(BUILD_DIR)/static
DYNAMIC_BUILD_DIR := $(BUILD_DIR)/dynamic
TEST_BUILD_DIR := $(BUILD_DIR)/test
INCLUDE_INSTALL_PATH := /usr/local/include/LLog
LIB_INSTALL_PATH := /usr/local/lib
STATIC_BUILD_FILES := $(STATIC_BUILD_DIR)/LLog.o \
DYNAMIC_BUILD_FILES := $(DYNAMIC_BUILD_DIR)/LLog.o \
TEST_BUILD_FILES := $(TEST_BUILD_DIR)/test.o \
all: dynamic static
dynamic: $(DYNAMIC_BUILD_FILES)
$(CC) -shared -o $(DYNAMIC_BUILD_DIR)/libLLog.so $(DYNAMIC_BUILD_FILES)
$(DYNAMIC_BUILD_DIR)/%.o: %.c | $(DYNAMIC_BUILD_DIR)
$(CC) $(CFLAGS) -c -fPIC $< -o $@
static: $(STATIC_BUILD_FILES)
ar rcs $(STATIC_BUILD_DIR)/libLLog.a $(STATIC_BUILD_FILES)
$(STATIC_BUILD_DIR)/%.o: %.c | $(STATIC_BUILD_DIR)
$(CC) $(CFLAGS) -c $< -o $@
test: $(TEST_BUILD_FILES) test_dynamic test_static
test_dynamic: dynamic $(TEST_BUILD_FILES)
$(CC) $(CFLAGS) -L$(CURDIR)/$(DYNAMIC_BUILD_DIR) -lLLog -o $(TEST_BUILD_DIR)/test_dynamic $(TEST_BUILD_FILES)
@echo ""
@echo "Running dynamic test..."
@echo ""
LD_LIBRARY_PATH=$(LD_LIBRARY_PATH):$(CURDIR)/build/dynamic ./$(TEST_BUILD_DIR)/test_dynamic
test_static: static $(TEST_BUILD_FILES)
$(CC) $(CFLAGS) -o $(TEST_BUILD_DIR)/test_static $(TEST_BUILD_FILES) $(STATIC_BUILD_DIR)/libLLog.a
@echo ""
@echo "Running static test..."
@echo ""
./$(TEST_BUILD_DIR)/test_static
$(TEST_BUILD_DIR)/%.o: test/%.c | $(TEST_BUILD_DIR)
$(CC) $(CFLAGS) -c $< -o $@
$(DYNAMIC_BUILD_DIR):
mkdir -p $(DYNAMIC_BUILD_DIR)
$(STATIC_BUILD_DIR):
mkdir -p $(STATIC_BUILD_DIR)
$(TEST_BUILD_DIR):
mkdir -p $(TEST_BUILD_DIR)
$(INCLUDE_INSTALL_PATH):
mkdir -p $(INCLUDE_INSTALL_PATH)
$(LIB_INSTALL_PATH):
mkdir -p $(LIB_INSTALL_PATH)
CLEANUP_BUILD_DIR:
rm -rf $(BUILD_DIR)/*/*.o $(BUILD_DIR)/test
install: $(INCLUDE_INSTALL_PATH) $(LIB_INSTALL_PATH) CLEANUP_BUILD_DIR
cp $(INCLUDE)/* $(INCLUDE_INSTALL_PATH)
cp $(STATIC_BUILD_DIR)/* $(LIB_INSTALL_PATH)
cp $(DYNAMIC_BUILD_DIR)/* $(LIB_INSTALL_PATH)
ldconfig
clean:
rm -rf $(BUILD_DIR)

69
include/LLog.h Normal file
View File

@@ -0,0 +1,69 @@
#ifndef LLog_H
#define LLog_H
#include "time.h"
#include <stdarg.h>
#include <stdio.h>
#ifndef max_string_len
#define max_string_len 1024
#endif
#define LLog_Author "Lucielle <luci@git.lunarware.tech>"
#define LLog_Repo "https://git.lunarware.tech/lucielle/LLog"
#define LLog_Version "1.0.0"
typedef struct {
char *buf;
size_t len;
} lLogString;
enum lLogLevel {
lINFO,
lWARNING,
lERROR,
lDEBUG
};
typedef struct {
time_t start;
time_t end;
} lTimespan;
typedef struct {
lTimespan time;
} lTimer;
typedef struct {
size_t count;
size_t capacity;
lTimer* items;
} timer_array;
int timer_array_append(timer_array* array, lTimer timer);
typedef struct {
size_t count;
size_t capacity;
lLogString** items;
} lLogString_array;
int lLogString_array_append(lLogString_array* array, lLogString* string);
typedef struct {
FILE* log_file;
timer_array timers;
int historyEnabled;
int logFileEnabled;
lLogString_array history;
} Logger;
Logger* Logger_new(lLogString* logfolder, int history_enabled);
int create_timer(Logger* logger);
void start_timer(Logger* logger, int timer_id);
void stop_timer(Logger* logger, int timer_id);
lLogString* lLogString_new(const char* fmt, ...);
int lLog(Logger* logger, enum lLogLevel LOG_LEVEL, lLogString* MESSAGE);
#endif

0
logs/temp Normal file
View File

24
test/test.c Normal file
View File

@@ -0,0 +1,24 @@
#include "../include/LLog.h"
#include <unistd.h>
int main(void) {
Logger* logger = Logger_new(lLogString_new("./logs"), 1);
lLog(logger, lINFO, lLogString_new("Author: %s", LLog_Author));
lLog(logger, lINFO, lLogString_new("Repo: %s", LLog_Repo));
lLog(logger, lINFO, lLogString_new("Version: %s\n", LLog_Version));
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(2);
stop_timer(logger, timer);
lLog(logger, lINFO, lLogString_new("%f seconds.", difftime(time_object->end, time_object->start)));
}