Compare commits
2 Commits
ba2df46189
...
964425e7b8
| Author | SHA1 | Date | |
|---|---|---|---|
|
964425e7b8
|
|||
|
fbb618711f
|
41
LLog.c
41
LLog.c
@@ -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) {
|
||||
@@ -15,6 +16,17 @@ int timer_array_append(timer_array* array, lTimer 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;
|
||||
}
|
||||
|
||||
int create_timer(Logger* logger) {
|
||||
return timer_array_append(&logger->timers, (lTimer){});
|
||||
}
|
||||
@@ -29,22 +41,33 @@ void stop_timer(Logger* logger, int timer_id) {
|
||||
logger->timers.items[timer_id].time.end = time(NULL);
|
||||
}
|
||||
|
||||
lLogString to_lLogString(const char* string) {
|
||||
lLogString str;
|
||||
str.buf = (char *)string;
|
||||
if (string == NULL) {
|
||||
str.len = 0;
|
||||
} else {
|
||||
str.len = strlen(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 = 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;
|
||||
};
|
||||
|
||||
void 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" :
|
||||
LOG_LEVEL == 1 ? "WARNING" :
|
||||
LOG_LEVEL == 2 ? "ERROR" :
|
||||
LOG_LEVEL == 3 ? "DEBUG" :
|
||||
"invalid_log_level";
|
||||
printf("[%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);
|
||||
}
|
||||
@@ -2,6 +2,9 @@
|
||||
#define LLog_H
|
||||
|
||||
#include "time.h"
|
||||
#include <stdarg.h>
|
||||
|
||||
#define max_string_len 1024
|
||||
|
||||
typedef struct {
|
||||
char *buf;
|
||||
@@ -29,20 +32,27 @@ typedef struct {
|
||||
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 {
|
||||
lLogString logfolder;
|
||||
timer_array timers;
|
||||
lLogString_array history;
|
||||
} Logger;
|
||||
|
||||
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);
|
||||
int lLog(Logger* logger, enum lLogLevel LOG_LEVEL, lLogString* MESSAGE);
|
||||
|
||||
#endif
|
||||
15
test/test.c
15
test/test.c
@@ -1,20 +1,21 @@
|
||||
#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!"));
|
||||
int message_1_id = lLog(&logger, lINFO, lLogString_new("Hello, INFO!"));
|
||||
lLog(&logger, lINFO, logger.history.items[message_1_id]);
|
||||
|
||||
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!"));
|
||||
|
||||
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)));
|
||||
}
|
||||
Reference in New Issue
Block a user