5 changed files with 163 additions and 0 deletions
@ -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() |
||||
|
||||
|
||||
@ -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
|
||||
@ -0,0 +1,62 @@
|
||||
#include "log/Log.h" |
||||
|
||||
#include <string.h> |
||||
#include <time.h> |
||||
#include <stdio.h> |
||||
#include <stdarg.h> |
||||
|
||||
#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; |
||||
} |
||||
} |
||||
@ -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) |
||||
@ -0,0 +1,54 @@
|
||||
#include "log/Log.h" |
||||
|
||||
#include <stdio.h> |
||||
#include <unistd.h> |
||||
#include <assert.h> |
||||
|
||||
#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; |
||||
} |
||||
Loading…
Reference in new issue