Compare commits

..

4 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
5 changed files with 66 additions and 17 deletions

1
.gitignore vendored
View File

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

38
LLog.c
View File

@@ -27,6 +27,30 @@ int lLogString_array_append(lLogString_array* array, lLogString* 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){});
}
@@ -61,13 +85,23 @@ lLogString* lLogString_new(const char* fmt, ...) {
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);
printf("[%s]: %.*s\n", LOG_LEVEL_STRING, (int)MESSAGE->len, MESSAGE->buf);
return lLogString_array_append(&logger->history, MESSAGE);
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);
}

View File

@@ -3,8 +3,15 @@
#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;
@@ -42,10 +49,14 @@ typedef struct {
int lLogString_array_append(lLogString_array* array, lLogString* string);
typedef struct {
lLogString logfolder;
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);

0
logs/temp Normal file
View File

View File

@@ -2,20 +2,23 @@
#include <unistd.h>
int main(void) {
Logger logger = {0};
int message_1_id = lLog(&logger, lINFO, lLogString_new("Hello, INFO!"));
lLog(&logger, lINFO, logger.history.items[message_1_id]);
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));
int message_2_id = lLog(&logger, lWARNING, lLogString_new("Hello, WARNING!"));
int message_3_id = lLog(&logger, lERROR, lLogString_new("Hello, ERROR!"));
int message_4_id = lLog(&logger, lDEBUG, lLogString_new("Hello, DEBUG!"));
lLog(logger, lINFO, lLogString_new("Hello, INFO!"));
int timer = create_timer(&logger);
lTimespan* time_object = &logger.timers.items[timer].time;
start_timer(&logger, timer);
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)));
stop_timer(logger, timer);
lLog(logger, lINFO, lLogString_new("%f seconds.", difftime(time_object->end, time_object->start)));
}