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>
|
2022-11-22 18:30:55 +01:00
|
|
|
#include <unistd.h>
|
2022-11-18 19:54:33 +01:00
|
|
|
#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;
|
2022-11-22 18:47:56 +01:00
|
|
|
struct dirent *entry;
|
2022-11-18 19:54:33 +01:00
|
|
|
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);
|
2022-11-23 19:44:35 +01:00
|
|
|
char *dateBuffer = malloc(strlen("yyyy-mm-dd") + 17); //TODO: MAKE THIS VARIABLE; FIND OUT WHY TF YOU NEED 17 MORE BYTES
|
2022-11-18 19:54:33 +01:00
|
|
|
DIR *dir;
|
|
|
|
|
2022-11-23 19:44:35 +01:00
|
|
|
sprintf(dateBuffer, "%04d-%02d-%02d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
|
|
|
|
|
2022-11-18 19:54:33 +01:00
|
|
|
//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)
|
|
|
|
{
|
2022-11-23 19:44:35 +01:00
|
|
|
if (strncmp(dateBuffer, entry -> d_name, strlen(dateBuffer)) == 0) buffer++;
|
2022-11-18 19:54:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-23 19:44:35 +01:00
|
|
|
free(dateBuffer);
|
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)
|
|
|
|
{
|
2022-11-22 18:47:56 +01:00
|
|
|
//VARIABLES
|
2022-11-22 18:50:41 +01:00
|
|
|
char *buffer = malloc(strlen(WRITE_FORMAT) + strlen(logMessage) + 2);
|
2022-11-22 18:47:56 +01:00
|
|
|
time_t timeL = time(NULL);
|
|
|
|
struct tm tm = *localtime(&timeL);
|
|
|
|
|
|
|
|
sprintf(buffer, WRITE_FORMATTING, tm.tm_hour, tm.tm_min, tm.tm_sec, logMessage); //LOAD MESSAGE
|
|
|
|
|
|
|
|
write(loggerFile, buffer, strlen(buffer)); //WRITE (YAY)
|
|
|
|
|
|
|
|
//DEALLOCATION
|
|
|
|
free(buffer);
|
2022-11-21 11:57:49 +00:00
|
|
|
}
|