Compare commits
7 Commits
964425e7b8
...
1.0.1
| Author | SHA1 | Date | |
|---|---|---|---|
| 8027a1bd8c | |||
|
aa9212e9da
|
|||
|
e0b58dc1be
|
|||
|
a025ecdae4
|
|||
|
4ad40539f2
|
|||
|
73893fa132
|
|||
|
2ab8477d7d
|
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
|||||||
build/
|
build/
|
||||||
|
logs/*.log
|
||||||
83
LLog.c
83
LLog.c
@@ -27,18 +27,83 @@ int lLogString_array_append(lLogString_array* array, lLogString* string) {
|
|||||||
return array->count - 1;
|
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) {
|
int create_timer(Logger* logger) {
|
||||||
return timer_array_append(&logger->timers, (lTimer){});
|
return timer_array_append(&logger->timers, (lTimer){});
|
||||||
}
|
}
|
||||||
|
|
||||||
void start_timer(Logger* logger, int timer_id) {
|
void start_timer(Logger* logger, int timer_id) {
|
||||||
if (logger->timers.count < (timer_id + 1)) return;
|
if (logger->timers.count < (timer_id + 1)) return;
|
||||||
logger->timers.items[timer_id].time.start = time(NULL);
|
lTimer* timer = &logger->timers.items[timer_id];
|
||||||
|
|
||||||
|
timer->time.start = time(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void stop_timer(Logger* logger, int timer_id) {
|
void stop_timer(Logger* logger, int timer_id) {
|
||||||
if (logger->timers.count < (timer_id + 1)) return;
|
if (logger->timers.count < (timer_id + 1)) return;
|
||||||
logger->timers.items[timer_id].time.end = time(NULL);
|
lTimer* timer = &logger->timers.items[timer_id];
|
||||||
|
|
||||||
|
// next property will never be set to 0 unless it is uninitialized
|
||||||
|
if (timer->next != 0) {
|
||||||
|
stop_timer(logger, timer->next);
|
||||||
|
} else {
|
||||||
|
timer->time.end = time(NULL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void pause_timer(Logger* logger, int timer_id) {
|
||||||
|
if (logger->timers.count < (timer_id + 1)) return;
|
||||||
|
lTimer* timer = &logger->timers.items[timer_id];
|
||||||
|
|
||||||
|
stop_timer(logger, timer_id);
|
||||||
|
timer->next = create_timer(logger);
|
||||||
|
}
|
||||||
|
|
||||||
|
void resume_timer(Logger* logger, int timer_id) {
|
||||||
|
if (logger->timers.count < (timer_id + 1)) return;
|
||||||
|
lTimer* timer = &logger->timers.items[timer_id];
|
||||||
|
|
||||||
|
if (timer->next != 0) {
|
||||||
|
resume_timer(logger, logger->timers.items[timer_id].next);
|
||||||
|
} else {
|
||||||
|
start_timer(logger, timer_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
double get_elapsed_time(Logger* logger, int timer_id) {
|
||||||
|
if (timer_id != 0) if (0) {}
|
||||||
|
if (logger->timers.count < (timer_id + 1)) return 0.0f;
|
||||||
|
lTimer* timer = &logger->timers.items[timer_id];
|
||||||
|
double current_elapsed = difftime(timer->time.end, timer->time.start);
|
||||||
|
|
||||||
|
if (timer->next != 0) {
|
||||||
|
return get_elapsed_time(logger, timer->next) + current_elapsed;
|
||||||
|
}
|
||||||
|
|
||||||
|
return current_elapsed;
|
||||||
}
|
}
|
||||||
|
|
||||||
lLogString* lLogString_new(const char* fmt, ...) {
|
lLogString* lLogString_new(const char* fmt, ...) {
|
||||||
@@ -61,13 +126,23 @@ lLogString* lLogString_new(const char* fmt, ...) {
|
|||||||
return str;
|
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) {
|
int lLog(Logger* logger, enum lLogLevel LOG_LEVEL, lLogString* MESSAGE) {
|
||||||
char* LOG_LEVEL_STRING = LOG_LEVEL == 0 ? "INFO" :
|
char* LOG_LEVEL_STRING = LOG_LEVEL == 0 ? "INFO" :
|
||||||
LOG_LEVEL == 1 ? "WARNING" :
|
LOG_LEVEL == 1 ? "WARNING" :
|
||||||
LOG_LEVEL == 2 ? "ERROR" :
|
LOG_LEVEL == 2 ? "ERROR" :
|
||||||
LOG_LEVEL == 3 ? "DEBUG" :
|
LOG_LEVEL == 3 ? "DEBUG" :
|
||||||
"invalid_log_level";
|
"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);
|
if (logger->logFileEnabled) Logger_write_to_log(logger, formated_message);
|
||||||
return lLogString_array_append(&logger->history, MESSAGE);
|
printf("%.*s", (int)formated_message->len, formated_message->buf);
|
||||||
|
|
||||||
|
return lLogString_array_append(&logger->history, formated_message);
|
||||||
}
|
}
|
||||||
@@ -3,8 +3,15 @@
|
|||||||
|
|
||||||
#include "time.h"
|
#include "time.h"
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#ifndef max_string_len
|
||||||
#define max_string_len 1024
|
#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.1"
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char *buf;
|
char *buf;
|
||||||
@@ -25,6 +32,7 @@ typedef struct {
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
lTimespan time;
|
lTimespan time;
|
||||||
|
int next;
|
||||||
} lTimer;
|
} lTimer;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@@ -42,14 +50,21 @@ typedef struct {
|
|||||||
int lLogString_array_append(lLogString_array* array, lLogString* string);
|
int lLogString_array_append(lLogString_array* array, lLogString* string);
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
lLogString logfolder;
|
FILE* log_file;
|
||||||
timer_array timers;
|
timer_array timers;
|
||||||
|
int historyEnabled;
|
||||||
|
int logFileEnabled;
|
||||||
lLogString_array history;
|
lLogString_array history;
|
||||||
|
|
||||||
} Logger;
|
} Logger;
|
||||||
|
Logger* Logger_new(lLogString* logfolder, int history_enabled);
|
||||||
|
|
||||||
int create_timer(Logger* logger);
|
int create_timer(Logger* logger);
|
||||||
void start_timer(Logger* logger, int timer_id);
|
void start_timer(Logger* logger, int timer_id);
|
||||||
void stop_timer(Logger* logger, int timer_id);
|
void stop_timer(Logger* logger, int timer_id);
|
||||||
|
void pause_timer(Logger* logger, int timer_id);
|
||||||
|
void resume_timer(Logger* logger, int timer_id);
|
||||||
|
double get_elapsed_time(Logger* logger, int timer_id);
|
||||||
|
|
||||||
lLogString* lLogString_new(const char* fmt, ...);
|
lLogString* lLogString_new(const char* fmt, ...);
|
||||||
|
|
||||||
|
|||||||
28
test/test.c
28
test/test.c
@@ -2,20 +2,26 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
Logger logger = {0};
|
Logger* logger = Logger_new(lLogString_new("./logs"), 1);
|
||||||
int message_1_id = lLog(&logger, lINFO, lLogString_new("Hello, INFO!"));
|
lLog(logger, lINFO, lLogString_new("Author: %s", LLog_Author));
|
||||||
lLog(&logger, lINFO, logger.history.items[message_1_id]);
|
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!"));
|
lLog(logger, lINFO, lLogString_new("Hello, INFO!"));
|
||||||
int message_3_id = lLog(&logger, lERROR, lLogString_new("Hello, ERROR!"));
|
|
||||||
int message_4_id = lLog(&logger, lDEBUG, lLogString_new("Hello, DEBUG!"));
|
|
||||||
|
|
||||||
int timer = create_timer(&logger);
|
lLog(logger, lWARNING, lLogString_new("Hello, WARNING!"));
|
||||||
lTimespan* time_object = &logger.timers.items[timer].time;
|
lLog(logger, lERROR, lLogString_new("Hello, ERROR!"));
|
||||||
start_timer(&logger, timer);
|
lLog(logger, lDEBUG, lLogString_new("Hello, DEBUG!"));
|
||||||
|
int timer = create_timer(logger);
|
||||||
|
|
||||||
|
start_timer(logger, timer);
|
||||||
|
|
||||||
|
sleep(4);
|
||||||
|
pause_timer(logger, timer);
|
||||||
sleep(2);
|
sleep(2);
|
||||||
|
resume_timer(logger, timer);
|
||||||
|
sleep(2);
|
||||||
|
stop_timer(logger, timer);
|
||||||
|
|
||||||
stop_timer(&logger, timer);
|
lLog(logger, lINFO, lLogString_new("%f seconds.", get_elapsed_time(logger, timer)));
|
||||||
lLog(&logger, lINFO, lLogString_new("%f seconds.", difftime(time_object->end, time_object->start)));
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user