From 0a72b65c0be291be9240e86261fd62739c4b5baf Mon Sep 17 00:00:00 2001 From: chodak166 Date: Sat, 29 Jul 2023 11:47:34 +0200 Subject: [PATCH] Added log library tests and implementation --- tieto-cpu-tracker/lib/log/CMakeLists.txt | 23 +++++++ tieto-cpu-tracker/lib/log/include/log/Log.h | 12 ++++ tieto-cpu-tracker/lib/log/src/Log.c | 62 +++++++++++++++++++ .../lib/log/tests/integration/CMakeLists.txt | 12 ++++ .../lib/log/tests/integration/Log.test.c | 54 ++++++++++++++++ 5 files changed, 163 insertions(+) create mode 100644 tieto-cpu-tracker/lib/log/include/log/Log.h create mode 100644 tieto-cpu-tracker/lib/log/src/Log.c create mode 100644 tieto-cpu-tracker/lib/log/tests/integration/CMakeLists.txt create mode 100644 tieto-cpu-tracker/lib/log/tests/integration/Log.test.c diff --git a/tieto-cpu-tracker/lib/log/CMakeLists.txt b/tieto-cpu-tracker/lib/log/CMakeLists.txt index b5caf61..3213fdd 100644 --- a/tieto-cpu-tracker/lib/log/CMakeLists.txt +++ b/tieto-cpu-tracker/lib/log/CMakeLists.txt @@ -1 +1,24 @@ cmake_minimum_required(VERSION 3.5) + +project(log LANGUAGES C VERSION 0.1.0) + +set(C_STANDARD 11) +set(TARGET ${PROJECT_NAME}) + +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +add_library(${TARGET} + src/Log.c + ) + +target_include_directories(${TARGET} + PRIVATE + src + PUBLIC + include + ) + +if (${BUILD_TESTS}) + add_subdirectory(tests/integration) +endif() + diff --git a/tieto-cpu-tracker/lib/log/include/log/Log.h b/tieto-cpu-tracker/lib/log/include/log/Log.h new file mode 100644 index 0000000..c00d8db --- /dev/null +++ b/tieto-cpu-tracker/lib/log/include/log/Log.h @@ -0,0 +1,12 @@ +#ifndef LOG_H +#define LOG_H + +void initLogger(const char* logfile); + +void clearLogs(void); + +void writeLog(const char* tag, const char* format, ...); + +void closeLogger(void); + +#endif // LOG_H diff --git a/tieto-cpu-tracker/lib/log/src/Log.c b/tieto-cpu-tracker/lib/log/src/Log.c new file mode 100644 index 0000000..87244d9 --- /dev/null +++ b/tieto-cpu-tracker/lib/log/src/Log.c @@ -0,0 +1,62 @@ +#include "log/Log.h" + +#include +#include +#include +#include + +#define LOG_FILE_PATH_MAX 256 +// #define LOG_LINE_MAX 256 + +static FILE* logFile = NULL; +static char logFilePath[LOG_FILE_PATH_MAX]; + +void initLogger(const char* logfile) +{ + strcpy(logFilePath, logfile); + if (logFile != NULL) { + fclose(logFile); + } + logFile = fopen(logFilePath, "a"); + if (logFile == NULL) { + fprintf(stderr, "Unable to open logfile\n"); + } +} + +void clearLogs(void) +{ + if (logFile != NULL) { + closeLogger(); + } + if (remove(logFilePath) != 0) { + fprintf(stderr, "Unable to remove logfile\n"); + } + initLogger(logFilePath); +} + +__attribute__((__format__ (__printf__, 2, 0))) +void writeLog(const char* tag, const char* format, ...) +{ + time_t now = time(NULL); + struct tm* timeinfo = localtime(&now); + char timestamp[20]; + va_list args; + + strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", timeinfo); + + va_start(args, format); + fprintf(logFile, "%s %s ", timestamp, tag); + vfprintf(logFile, format, args); + fprintf(logFile, "\n"); + va_end(args); + + fflush(logFile); +} + +void closeLogger(void) +{ + if (logFile != NULL) { + fclose(logFile); + logFile = NULL; + } +} diff --git a/tieto-cpu-tracker/lib/log/tests/integration/CMakeLists.txt b/tieto-cpu-tracker/lib/log/tests/integration/CMakeLists.txt new file mode 100644 index 0000000..6e1d8cc --- /dev/null +++ b/tieto-cpu-tracker/lib/log/tests/integration/CMakeLists.txt @@ -0,0 +1,12 @@ +cmake_minimum_required(VERSION 3.5) + +set(C_STANDARD 11) +enable_testing() + +set(SRC_DIR ../../src) + +include_directories(${SRC_DIR}) + +add_executable(LogTest Log.test.c) +target_link_libraries(LogTest log) +add_test(LogTest ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/LogTest) diff --git a/tieto-cpu-tracker/lib/log/tests/integration/Log.test.c b/tieto-cpu-tracker/lib/log/tests/integration/Log.test.c new file mode 100644 index 0000000..cf60ade --- /dev/null +++ b/tieto-cpu-tracker/lib/log/tests/integration/Log.test.c @@ -0,0 +1,54 @@ +#include "log/Log.h" + +#include +#include +#include + +#define TAG "TEST" + +static const char* LOG_FILE = "/tmp/log-test.log"; + +static int countLines(const char* filePath) { + int count = 0; + int ch; + FILE* file = fopen(filePath, "r"); + if (file == NULL) { + return -1; + } + while ((ch = fgetc(file)) != EOF) { + if (ch == '\n') { + count++; + } + } + fclose(file); + return count; +} + + +int main(void) +{ + int lineCount = 0; + + initLogger(LOG_FILE); + clearLogs(); + + lineCount = countLines(LOG_FILE); + assert(lineCount == 0); + + writeLog(TAG, "First log message"); + writeLog(TAG, "Second log message"); + writeLog(TAG, "Third log message"); + + lineCount = countLines(LOG_FILE); + assert(lineCount == 3); + + writeLog(TAG, "This is %ith line that is %s", 4, "formatted"); + + lineCount = countLines(LOG_FILE); + assert(lineCount == 4); + + closeLogger(); + + printf("Log file %s contains %d lines\n", LOG_FILE, lineCount); + return 0; +}