2022-11-18 17:27:47 +01:00
|
|
|
#include <why2/logger/logger.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2022-11-18 19:54:33 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <why2/logger/flags.h>
|
2022-11-18 17:27:47 +01:00
|
|
|
|
2022-11-18 18:32:28 +01:00
|
|
|
int initLogger(char *directoryPath)
|
2022-11-18 17:27:47 +01:00
|
|
|
{
|
2022-11-18 19:54:33 +01:00
|
|
|
//VARIABLES
|
|
|
|
struct stat st;
|
|
|
|
struct dirent * entry;
|
|
|
|
time_t timeL = time(NULL);
|
|
|
|
struct tm tm = *localtime(&timeL);
|
|
|
|
int buffer = 1;
|
2022-11-21 11:57:49 +00:00
|
|
|
int returning;
|
2022-11-18 19:54:33 +01:00
|
|
|
char *filePath = malloc(strlen(directoryPath) + strlen(LOG_FORMAT) + 1);
|
|
|
|
DIR *dir;
|
|
|
|
|
|
|
|
//CREATE directoryPath DIRECTORY IF IT DOESN'T EXISTS ALREADY
|
|
|
|
if (stat(directoryPath, &st) == -1)
|
|
|
|
{
|
|
|
|
mkdir(directoryPath, 0700);
|
|
|
|
}
|
|
|
|
|
|
|
|
//COUNT HOW MANY LOGS WERE GENERATED THIS DAY
|
|
|
|
dir = opendir(directoryPath);
|
|
|
|
while ((entry = readdir(dir)) != NULL)
|
|
|
|
{
|
|
|
|
if (entry -> d_type == DT_REG)
|
|
|
|
{
|
|
|
|
if (strcmp(entry -> d_name, ".gitkeep") == 0) continue;
|
|
|
|
|
|
|
|
buffer++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-22 18:29:00 +01:00
|
|
|
if (buffer > MAX_USAGE) return INVALID_FILE; //MAX_USAGE WAS REACHED
|
2022-11-18 19:54:33 +01:00
|
|
|
|
2022-11-22 18:29:00 +01:00
|
|
|
sprintf(filePath, LOG_FORMATTING, directoryPath, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, buffer); //GENERATE LOG-NAME
|
2022-11-18 19:54:33 +01:00
|
|
|
|
2022-11-21 11:57:49 +00:00
|
|
|
returning = open(filePath, O_WRONLY | O_APPEND | O_CREAT, 0644);
|
|
|
|
|
2022-11-18 19:54:33 +01:00
|
|
|
//DEALLOCATION
|
2022-11-21 11:57:49 +00:00
|
|
|
free(filePath);
|
2022-11-18 19:54:33 +01:00
|
|
|
closedir(dir);
|
|
|
|
|
2022-11-21 11:57:49 +00:00
|
|
|
return returning;
|
2022-11-22 18:29:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void writeLog(int loggerFile, char *logMessage)
|
|
|
|
{
|
|
|
|
write(loggerFile, logMessage, strlen(logMessage));
|
2022-11-21 11:57:49 +00:00
|
|
|
}
|