added latest log feature && simplified LOG_FORMATTING

This commit is contained in:
Václav Šmejkal 2022-12-09 15:10:41 +01:00
parent 215121f209
commit cd6600a83f
No known key found for this signature in database
GPG Key ID: FD749A97DF2D5E19
2 changed files with 19 additions and 3 deletions

View File

@ -27,16 +27,19 @@ const enum RETURN_CODES //exit codes you fucking idiot (2#)
#define WRITE_MESSAGE "Hello from logger-test! 👋" #define WRITE_MESSAGE "Hello from logger-test! 👋"
#define WRITE_DIR "./logs" #define WRITE_DIR "./logs"
#define LOG_LATEST "latest.log"
#define LOG_FORMAT "yyyy-mm-dd_xxx.log" //THE LAST xxx IS FOR BASE-16 NUMBER OF USAGE THAT DAY (SO MAX IS 4095 [FFF] USAGES PER DAY) #define LOG_FORMAT "yyyy-mm-dd_xxx.log" //THE LAST xxx IS FOR BASE-16 NUMBER OF USAGE THAT DAY (SO MAX IS 4095 [FFF] USAGES PER DAY)
#define LOG_FORMAT_START "yyyy-mm-dd" //FIRST PART OF LOG_FORMAT #define LOG_FORMAT_START "yyyy-mm-dd" //FIRST PART OF LOG_FORMAT
#define LOG_FORMATTING "%s/%04d-%02d-%02d_%03x.log" //SAME THING AS LOG_FORMAT, BUT USED IN sprintf IN logger.c, NOT AS LENGTH #define LOG_FORMATTING "%s/%s_%03x.log" //SAME THING AS LOG_FORMAT, BUT USED IN sprintf IN logger.c, NOT AS LENGTH
#define LOG_FORMATTING_START "%04d-%02d-%02d" //FIRST PART OF LOG_FORMATTING #define LOG_FORMATTING_START "%04d-%02d-%02d" //FIRST PART OF LOG_FORMATTING
#define WRITE_FORMAT "[hh:mm:ss] " //LOG MESSAGE'S PREFIX (THE SPACE AT THE END IS INTENTIONAL) #define WRITE_FORMAT "[hh:mm:ss] " //LOG MESSAGE'S PREFIX (THE SPACE AT THE END IS INTENTIONAL)
#define WRITE_FORMATTING "[%02d:%02d:%02d]: %s" //guess what #define WRITE_FORMATTING "[%02d:%02d:%02d]: %s" //guess what
#define LOG_LATEST_FORMATTING "%s/%s"
#define MAX_USAGE 0xfff //LOOK AT LOG_FORMAT #define MAX_USAGE 0xfff //LOOK AT LOG_FORMAT
//TYPES //TYPES

View File

@ -40,6 +40,8 @@ logFile initLogger(char *directoryPath)
int file; int file;
char *filePath = malloc(strlen(directoryPath) + 1 + strlen(LOG_FORMAT) + 1); char *filePath = malloc(strlen(directoryPath) + 1 + strlen(LOG_FORMAT) + 1);
char *dateBuffer = malloc(strlen(LOG_FORMAT_START) + 1); char *dateBuffer = malloc(strlen(LOG_FORMAT_START) + 1);
char *latestBuffer = malloc(strlen(WRITE_DIR) + strlen(LOG_LATEST) + 2);
char *latestFilePath = NULL;
DIR *dir; DIR *dir;
sprintf(dateBuffer, LOG_FORMATTING_START, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday); sprintf(dateBuffer, LOG_FORMATTING_START, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
@ -67,14 +69,25 @@ logFile initLogger(char *directoryPath)
goto deallocation; goto deallocation;
} }
sprintf(filePath, LOG_FORMATTING, directoryPath, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, buffer); //GENERATE LOG-NAME sprintf(filePath, LOG_FORMATTING, directoryPath, dateBuffer, buffer); //GENERATE LOG-NAME
file = open(filePath, O_WRONLY | O_APPEND | O_CREAT, 0644); 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);
if (access(latestBuffer, R_OK) == 0) { unlink(latestBuffer); }
symlink(latestFilePath + (strlen(WRITE_DIR) + 1), latestBuffer); //CREATE
deallocation: deallocation:
//DEALLOCATION //DEALLOCATION
free(dateBuffer); free(dateBuffer);
free(latestBuffer);
free(latestFilePath);
closedir(dir); closedir(dir);
return (logFile) return (logFile)