Zadania rekrutacyjne i ćwiczeniowe
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

62 lines
1.2 KiB

#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;
}
}