From 9f7f22f8a8062f86aa29d0b6046899f458f8eecd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Mogu=C3=A9rou?= Date: Tue, 16 Apr 2024 01:03:42 +0200 Subject: [PATCH] =?UTF-8?q?Int=C3=A9gration=20du=20Logger?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 5 +- src/algorithm.c | 18 +++---- src/display.c | 3 +- src/logger.c | 132 ++++++++++++++++++++++++++++++++++++++++++++++++ src/logger.h | 42 +++++++++++++++ src/main.c | 10 +++- src/structure.c | 25 ++++----- 7 files changed, 210 insertions(+), 25 deletions(-) create mode 100644 src/logger.c create mode 100644 src/logger.h diff --git a/Makefile b/Makefile index 50a8d24..b34f7b1 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ all: bin/main test: bin/main bin/main MP2I-creneaux.txt 33 MP2I-colleurs.txt 16 6 15 output.csv -OBJECTS = obj/structure.o obj/algorithm.o obj/display.o obj/main.o +OBJECTS = obj/structure.o obj/algorithm.o obj/display.o obj/logger.o obj/main.o bin/main: bin $(OBJECTS) $(CC) -o $@ $(LFLAGS) $(FLAGS) $(OBJECTS) @@ -24,6 +24,9 @@ obj/display.o: src/display.c src/display.h obj obj/algorithm.o: src/algorithm.c src/algorithm.h obj $(CC) -o $@ -c $(FLAGS) $< +obj/logger.o: src/logger.c src/logger.h obj + $(CC) -o $@ -c $(FLAGS) $< + bin: mkdir -p bin diff --git a/src/algorithm.c b/src/algorithm.c index 0fb9455..6480762 100644 --- a/src/algorithm.c +++ b/src/algorithm.c @@ -21,6 +21,7 @@ #include #include +#include "logger.h" #include "structure.h" #include "algorithm.h" @@ -454,7 +455,7 @@ colleur* get_colleurs(colleur* cl, int len_cl, date d, int* how_many) { if(is_equal_date(cl[i].disp[j], d)) { //printf("%s\n", cl[i].name); if(ptr >= 30) { - printf("warning : too many colleurs detected for a creneau\n"); + warn("Too many colleurs detected for a creneau\n"); } res[ptr] = cl[i]; ptr++; @@ -1079,8 +1080,7 @@ void aux_2(creneau* edt, int len_edt, colleur* chads, int len_chads, int n_group int temp = 0; int skipped = 0; int a = 0; - printf("Testing %d combinations...\n", n_sim); - printf("\n"); + printf("Testing %d combinations...\n\n", n_sim); for(int k = 0; k < n_sim*(1 - (max_score == n_groups*100)); k++) { if(k >= a) { printf("\x1b[1F"); @@ -1126,17 +1126,17 @@ void aux_2(creneau* edt, int len_edt, colleur* chads, int len_chads, int n_group printf("\x1b[1F"); printf("\x1b[2K"); if(max_score == 100*n_groups) { - printf("Interrupting early due to a perfect score achieved\n"); + info("Interrupting early due to a perfect score achieved"); } else { - printf("100%% Completed, best score is %d/%d (without skip penalty) with %d skipped colle(s)\n", max_score+global_skipped*30, 100*n_groups, global_skipped); - printf("Most screwed group is %d with a score of %d/100\n", screwed_group, global_min); - printf("Stats for all groups are :\n"); + info("100%% Completed, best score is %d/%d (without skip penalty) with %d skipped colle(s)", max_score+global_skipped*30, 100*n_groups, global_skipped); + info("Most screwed group is %d with a score of %d/100\n", screwed_group, global_min); + info("Stats for all groups are :"); for(int i = 0; i < n_groups; i++) { - printf("Group %d : %d/100\n", i+1, group_stats[i]); + info("Group %d : %d/100", i+1, group_stats[i]); } } int end = time(NULL); - printf("Took %ds to found\n", end-start); + info("Took %ds to found", end-start); free(group_stats); free(group_temp); free(weeks_len); diff --git a/src/display.c b/src/display.c index d5759b6..c8504c2 100644 --- a/src/display.c +++ b/src/display.c @@ -19,6 +19,7 @@ #include +#include "logger.h" #include "structure.h" /* @@ -317,6 +318,6 @@ void print_all_edt(creneau* edt, int len_edt, int n_weeks, int len_oneweek) { void print_all_colleurs(colleur* chads, int n_chads) { for(int c = 0; c < n_chads; c++) { - printf("Colleur %s with %d available :\n", chads[c].name, chads[c].n_disp); + info("Colleur %s with %d available :\n", chads[c].name, chads[c].n_disp); } } diff --git a/src/logger.c b/src/logger.c new file mode 100644 index 0000000..c9a6928 --- /dev/null +++ b/src/logger.c @@ -0,0 +1,132 @@ +#include +#include +#include +#include +#include + +#include "logger.h" +Logger logger; + +void create_logger(char* logpath, LogLevel level) { + logger = malloc(sizeof(*logger)); + + logger->t0 = clock(); + logger->level = level; + + if (logpath[0] != '\0') + { + logger->logfile = fopen(logpath, "w"); + } + else + logger->logfile = NULL; +} + +void destroy_logger() { + if (logger->logfile != NULL) + fclose(logger->logfile); + free(logger); +} + +void logprint(LogLevel level, char* str, va_list args) +{ + clock_t t = clock(); + + char label[6]; + switch (level) + { + case (LOG_PANIC): + strcpy(label, "PANIC"); + break; + case (LOG_FATAL): + strcpy(label, "FATAL"); + break; + case (LOG_ERROR): + strcpy(label, "ERROR"); + break; + case (LOG_WARN): + strcpy(label, "WARN"); + break; + case (LOG_INFO): + strcpy(label, "INFO"); + break; + case (LOG_DEBUG): + strcpy(label, "DEBUG"); + break; + case (LOG_TRACE): + strcpy(label, "TRACE"); + break; + } + + FILE* stream = (level >= LOG_WARN) ? stderr : stdout; + + if (level < logger->level) + return; + + fprintf(stream, "[%s][%f]: ", label, (double)t / CLOCKS_PER_SEC); + vfprintf(stream, str, args); + fprintf(stream, "\n"); + + if (logger->logfile != NULL) + { + fprintf(logger->logfile, "[%s][%f]: ", label, (double)t / CLOCKS_PER_SEC); + vfprintf(logger->logfile, str, args); + fprintf(logger->logfile, "\n"); + } +} + + +void panic(char *str, ...) +{ + va_list args; + va_start(args, str); + logprint(LOG_PANIC, str, args); + va_end(args); +} + +void fatal(char *str, ...) +{ + va_list args; + va_start(args, str); + logprint(LOG_FATAL, str, args); + va_end(args); +} + +void error(char *str, ...) +{ + va_list args; + va_start(args, str); + logprint(LOG_ERROR, str, args); + va_end(args); +} + +void warn(char *str, ...) +{ + va_list args; + va_start(args, str); + logprint(LOG_WARN, str, args); + va_end(args); +} + +void info(char *str, ...) +{ + va_list args; + va_start(args, str); + logprint(LOG_INFO, str, args); + va_end(args); +} + +void debug(char *str, ...) +{ + va_list args; + va_start(args, str); + logprint(LOG_DEBUG, str, args); + va_end(args); +} + +void trace(char *str, ...) +{ + va_list args; + va_start(args, str); + logprint(LOG_TRACE, str, args); + va_end(args); +} diff --git a/src/logger.h b/src/logger.h new file mode 100644 index 0000000..9bbf6fc --- /dev/null +++ b/src/logger.h @@ -0,0 +1,42 @@ +#ifndef LOGGER_H_INCLUDED +#define LOGGER_H_INCLUDED + +#include +#include +#include + + +enum LogLevel { + LOG_PANIC=6, + LOG_FATAL=5, + LOG_ERROR=4, + LOG_WARN=3, + LOG_INFO=2, + LOG_DEBUG=1, + LOG_TRACE=0 +}; +typedef enum LogLevel LogLevel; + +struct Logger { + clock_t t0; + LogLevel level; + FILE* logfile; +}; +typedef struct Logger* Logger; + +extern Logger logger; + + +void create_logger(char *logpath, LogLevel level); +void destroy_logger(); + + +void panic(char *str, ...); +void fatal(char *str, ...); +void error(char *str, ...); +void warn(char *str, ...); +void info(char *str, ...); +void debug(char *str, ...); +void trace(char *str, ...); + +#endif diff --git a/src/main.c b/src/main.c index 3dd4465..208ea9f 100644 --- a/src/main.c +++ b/src/main.c @@ -22,6 +22,7 @@ #include #include +#include "logger.h" #include "structure.h" #include "algorithm.h" @@ -33,6 +34,8 @@ int main(int argc, char **argv) { fprintf(stderr, "Usage: %s \n", argv[0]); exit(1); } + + create_logger("colloscope.log", LOG_TRACE); char* path_creneaux = argv[1]; int n_creneaux = str_to_int(argv[2]); @@ -42,9 +45,9 @@ int main(int argc, char **argv) { int n_groups = str_to_int(argv[6]); char* path_output = argv[7]; - printf("%d %d %d %d\n", n_creneaux, n_colleurs, n_weeks, n_groups); + trace("%d %d %d %d\n", n_creneaux, n_colleurs, n_weeks, n_groups); - printf("Starting\n"); + info("Starting\n"); srand(time(NULL)); //creneau* edt = import_creneaux("file.txt", 76); @@ -66,5 +69,8 @@ int main(int argc, char **argv) { free(dudes[i].name); } free(dudes); + + destroy_logger(); + return 0; } diff --git a/src/structure.c b/src/structure.c index 084a7d2..bcecd6d 100644 --- a/src/structure.c +++ b/src/structure.c @@ -23,6 +23,7 @@ #include #include +#include "logger.h" #include "structure.h" //static int width = 1200; @@ -63,14 +64,14 @@ int date_dist(date d1, date d2) { /* returns distance between d1 and d2 in days bool is_sorted(creneau* edt, int len) { for(int i = 1; i < len; i++) { if(date_dist(edt[i-1].date, edt[i].date) == -1) { - printf("Anomaly detected at lane %d (dates are not sorted in ascending order) : \n", i); - printf("%d %d %d %d\n", edt[i-1].date.hour, edt[i-1].date.day, edt[i-1].date.month, edt[i-1].date.year); - printf("%d %d %d %d\n", edt[i].date.hour, edt[i].date.day, edt[i].date.month, edt[i].date.year); - printf("%d %d %d %d\n", edt[i+1].date.hour, edt[i+1].date.day, edt[i+1].date.month, edt[i+1].date.year); + warn("Anomaly detected at lane %d (dates are not sorted in ascending order) :", i); + warn("%d %d %d %d", edt[i-1].date.hour, edt[i-1].date.day, edt[i-1].date.month, edt[i-1].date.year); + warn("%d %d %d %d", edt[i].date.hour, edt[i].date.day, edt[i].date.month, edt[i].date.year); + warn("%d %d %d %d", edt[i+1].date.hour, edt[i+1].date.day, edt[i+1].date.month, edt[i+1].date.year); return false; } } - printf("No problem detected\n"); + info("No problem detected"); return true; } @@ -201,7 +202,7 @@ colleur* import_colleurs(char* filename, int n_colleurs, int max_available) { res[colleur_ptr].mat = INFO; } else { word[wordlen] = '\0'; - printf("Wait that's illegal (%s)\n", word); + fatal("Wait that's illegal (%s)", word); assert(0); } res[colleur_ptr].disp = malloc(sizeof(date)*max_available); @@ -234,7 +235,7 @@ colleur* import_colleurs(char* filename, int n_colleurs, int max_available) { } else if(date_ptr == 2) { res[colleur_ptr].disp[current-2].month = buffer; } else { - printf("Oh no (%d)\n", date_ptr); + fatal("Oh no (%d)", date_ptr); assert(0); } buffer = 0; @@ -247,7 +248,7 @@ colleur* import_colleurs(char* filename, int n_colleurs, int max_available) { } // {char* name; int namelen; topic mat; date* disp; int n_disp;} - printf("Successfully imported colleurs\n"); + info("Successfully imported colleurs"); fclose(ptr); free(word); @@ -352,7 +353,7 @@ creneau* import_creneaux_oneweek(char* filename, int len_file, int n_weeks) { if(!is_sorted(edt, n_weeks*len_file)) { assert(0); } - printf("Imported creneaux successfully\n"); + info("Imported creneaux successfully"); return edt; } @@ -409,7 +410,7 @@ colleur* import_colleurs_oneweek(char* filename, int n_colleurs, int n_weeks, in res[colleur_ptr].mat = INFO; } else { word[wordlen] = '\0'; - printf("Wait that's illegal (%s)\n", word); + fatal("Wait that's illegal (%s)", word); assert(0); } res[colleur_ptr].disp = malloc(sizeof(date)*len_oneweek); @@ -442,7 +443,7 @@ colleur* import_colleurs_oneweek(char* filename, int n_colleurs, int n_weeks, in } else if(date_ptr == 2) { res[colleur_ptr].disp[current-2].month = buffer; } else { - printf("Oh no (%d)\n", date_ptr); + fatal("Oh no (%d)", date_ptr); assert(0); } buffer = 0; @@ -462,7 +463,7 @@ colleur* import_colleurs_oneweek(char* filename, int n_colleurs, int n_weeks, in fclose(ptr); free(word); - printf("Imported colleurs with no problems\n"); + info("Imported colleurs with no problems"); return res; }