2022-12-04 18:54:57 +01:00
|
|
|
/*
|
|
|
|
This is part of WHY2
|
|
|
|
Copyright (C) 2022 Václav Šmejkal
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
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-12-09 18:55:18 +01:00
|
|
|
#include <why2/encrypter.h>
|
|
|
|
#include <why2/misc.h>
|
|
|
|
|
2022-12-04 19:13:05 +01:00
|
|
|
logFile 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-12-04 19:13:05 +01:00
|
|
|
int file;
|
2022-11-23 19:52:03 +01:00
|
|
|
char *filePath = malloc(strlen(directoryPath) + 1 + strlen(LOG_FORMAT) + 1);
|
|
|
|
char *dateBuffer = malloc(strlen(LOG_FORMAT_START) + 1);
|
2022-12-09 15:10:41 +01:00
|
|
|
char *latestBuffer = malloc(strlen(WRITE_DIR) + strlen(LOG_LATEST) + 2);
|
|
|
|
char *latestFilePath = NULL;
|
2022-11-18 19:54:33 +01:00
|
|
|
DIR *dir;
|
|
|
|
|
2022-11-23 19:52:03 +01:00
|
|
|
sprintf(dateBuffer, LOG_FORMATTING_START, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
|
2022-11-23 19:44:35 +01:00
|
|
|
|
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-12-04 19:13:05 +01:00
|
|
|
if (buffer > MAX_USAGE) //MAX_USAGE WAS REACHED
|
|
|
|
{
|
|
|
|
file = INVALID_FILE;
|
|
|
|
|
|
|
|
goto deallocation;
|
|
|
|
}
|
2022-11-18 19:54:33 +01:00
|
|
|
|
2022-12-09 15:10:41 +01:00
|
|
|
sprintf(filePath, LOG_FORMATTING, directoryPath, dateBuffer, buffer); //GENERATE LOG-NAME
|
2022-11-18 19:54:33 +01:00
|
|
|
|
2022-12-09 15:10:41 +01:00
|
|
|
file = open(filePath, O_WRONLY | O_APPEND | O_CREAT, 0644); //CREATE LOG FILE
|
|
|
|
|
|
|
|
//CREATE SYMLINK
|
|
|
|
sprintf(latestBuffer, LOG_LATEST_FORMATTING, WRITE_DIR, LOG_LATEST); //GENERATE LATEST.log PATH
|
|
|
|
|
|
|
|
latestFilePath = malloc(strlen(filePath) + 1);
|
|
|
|
strcpy(latestFilePath, filePath);
|
|
|
|
|
2022-12-09 15:21:02 +01:00
|
|
|
if (access(latestBuffer, R_OK) == 0) { unlink(latestBuffer); } //REMOVE SYMLINK IF IT ALREADY EXISTS
|
2022-12-09 15:12:50 +01:00
|
|
|
(void) (symlink(latestFilePath + (strlen(WRITE_DIR) + 1), latestBuffer) + 1); //TODO: Try to create some function for processing exit value //CREATE
|
2022-12-04 19:13:05 +01:00
|
|
|
|
|
|
|
deallocation:
|
2022-11-21 11:57:49 +00:00
|
|
|
|
2022-11-18 19:54:33 +01:00
|
|
|
//DEALLOCATION
|
2022-11-23 19:44:35 +01:00
|
|
|
free(dateBuffer);
|
2022-12-09 15:10:41 +01:00
|
|
|
free(latestBuffer);
|
|
|
|
free(latestFilePath);
|
2022-11-18 19:54:33 +01:00
|
|
|
closedir(dir);
|
|
|
|
|
2022-12-04 19:13:05 +01:00
|
|
|
return (logFile)
|
|
|
|
{
|
|
|
|
file,
|
|
|
|
filePath
|
|
|
|
};
|
2022-11-22 18:29:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void writeLog(int loggerFile, char *logMessage)
|
|
|
|
{
|
2022-12-06 18:14:34 +01:00
|
|
|
//CHECK IF loggerFile IS CREATED
|
|
|
|
if (loggerFile == INVALID_FILE)
|
|
|
|
{
|
|
|
|
return; //TODO: Add some kind of error message
|
|
|
|
}
|
|
|
|
|
2022-11-22 18:47:56 +01:00
|
|
|
//VARIABLES
|
2022-12-09 18:55:18 +01:00
|
|
|
char *buffer = NULL;
|
|
|
|
char *message = NULL; //COPY OF logMessage
|
2022-11-22 18:47:56 +01:00
|
|
|
time_t timeL = time(NULL);
|
|
|
|
struct tm tm = *localtime(&timeL);
|
2022-12-09 18:55:18 +01:00
|
|
|
logFlags flags = getLogFlags();
|
|
|
|
|
|
|
|
if (flags.key != NULL) //ENCRYPT TEXT IF KEY WAS CHANGED
|
|
|
|
{
|
|
|
|
outputFlags encrypted = encryptText(logMessage, flags.key); //ENCRYPT
|
|
|
|
|
|
|
|
message = strdup(encrypted.outputText); //COPY
|
|
|
|
|
|
|
|
//DEALLOCATION
|
|
|
|
free(encrypted.usedKey);
|
|
|
|
} else //FUCK ENCRYPTION, LET'S DO IT; WHY WOULD WE EVEN USE WHY2-CORE? HUH?
|
|
|
|
{
|
|
|
|
message = strdup(logMessage); //COPY
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer = malloc(strlen(WRITE_FORMAT) + strlen(message) + 2); //ALLOCATE
|
2022-11-22 18:47:56 +01:00
|
|
|
|
2022-12-09 18:55:18 +01:00
|
|
|
sprintf(buffer, WRITE_FORMATTING, tm.tm_hour, tm.tm_min, tm.tm_sec, message); //LOAD MESSAGE
|
2022-11-22 18:47:56 +01:00
|
|
|
|
2022-12-09 14:09:15 +01:00
|
|
|
(void) (write(loggerFile, buffer, strlen(buffer)) + 1); //TODO: Try to create some function for processing exit value
|
2022-11-22 18:47:56 +01:00
|
|
|
|
|
|
|
//DEALLOCATION
|
|
|
|
free(buffer);
|
2022-12-09 18:55:18 +01:00
|
|
|
free(message);
|
2022-11-21 11:57:49 +00:00
|
|
|
}
|