Compare commits
2 Commits
5e593720d6
...
ba2df46189
| Author | SHA1 | Date | |
|---|---|---|---|
|
ba2df46189
|
|||
|
2d6ed0b99d
|
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
build/
|
||||
50
LLog.c
Normal file
50
LLog.c
Normal file
@@ -0,0 +1,50 @@
|
||||
#include "include/LLog.h"
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "time.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 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 to_lLogString(const char* string) {
|
||||
lLogString str;
|
||||
str.buf = (char *)string;
|
||||
if (string == NULL) {
|
||||
str.len = 0;
|
||||
} else {
|
||||
str.len = strlen(string);
|
||||
}
|
||||
return str;
|
||||
};
|
||||
|
||||
void 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);
|
||||
}
|
||||
91
Makefile
Normal file
91
Makefile
Normal 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)
|
||||
48
include/LLog.h
Normal file
48
include/LLog.h
Normal file
@@ -0,0 +1,48 @@
|
||||
#ifndef LLog_H
|
||||
#define LLog_H
|
||||
|
||||
#include "time.h"
|
||||
|
||||
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 {
|
||||
lLogString logfolder;
|
||||
timer_array timers;
|
||||
} 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);
|
||||
|
||||
void lLog(Logger logger, enum lLogLevel LOG_LEVEL, lLogString MESSAGE);
|
||||
|
||||
#endif
|
||||
20
test/test.c
Normal file
20
test/test.c
Normal file
@@ -0,0 +1,20 @@
|
||||
#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 timer = create_timer(&logger);
|
||||
lTimespan* time_object = &logger.timers.items[timer].time;
|
||||
start_timer(&logger, timer);
|
||||
|
||||
sleep(5);
|
||||
|
||||
stop_timer(&logger, timer);
|
||||
printf("\n%f seconds.\n", difftime(time_object->end, time_object->start));
|
||||
}
|
||||
Reference in New Issue
Block a user