diff --git a/include/logger/flags.h b/include/logger/flags.h index f7e2a20..3330e5b 100644 --- a/include/logger/flags.h +++ b/include/logger/flags.h @@ -27,16 +27,19 @@ const enum RETURN_CODES //exit codes you fucking idiot (2#) #define WRITE_MESSAGE "Hello from logger-test! 👋" #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_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 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 LOG_LATEST_FORMATTING "%s/%s" + #define MAX_USAGE 0xfff //LOOK AT LOG_FORMAT //TYPES diff --git a/src/logger/lib/logger.c b/src/logger/lib/logger.c index 47f45a7..b2b1dbd 100644 --- a/src/logger/lib/logger.c +++ b/src/logger/lib/logger.c @@ -40,6 +40,8 @@ logFile initLogger(char *directoryPath) int file; char *filePath = malloc(strlen(directoryPath) + 1 + strlen(LOG_FORMAT) + 1); char *dateBuffer = malloc(strlen(LOG_FORMAT_START) + 1); + char *latestBuffer = malloc(strlen(WRITE_DIR) + strlen(LOG_LATEST) + 2); + char *latestFilePath = NULL; DIR *dir; 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; } - 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 free(dateBuffer); + free(latestBuffer); + free(latestFilePath); closedir(dir); return (logFile)