107 lines
3.4 KiB
C
107 lines
3.4 KiB
C
#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);
|
|
} |