Intégration du Logger

This commit is contained in:
Valentin Moguérou 2024-04-16 01:03:42 +02:00
parent c41bc74a53
commit 9f7f22f8a8
7 changed files with 210 additions and 25 deletions

View File

@ -8,7 +8,7 @@ all: bin/main
test: bin/main test: bin/main
bin/main MP2I-creneaux.txt 33 MP2I-colleurs.txt 16 6 15 output.csv 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) bin/main: bin $(OBJECTS)
$(CC) -o $@ $(LFLAGS) $(FLAGS) $(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 obj/algorithm.o: src/algorithm.c src/algorithm.h obj
$(CC) -o $@ -c $(FLAGS) $< $(CC) -o $@ -c $(FLAGS) $<
obj/logger.o: src/logger.c src/logger.h obj
$(CC) -o $@ -c $(FLAGS) $<
bin: bin:
mkdir -p bin mkdir -p bin

View File

@ -21,6 +21,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include "logger.h"
#include "structure.h" #include "structure.h"
#include "algorithm.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)) { if(is_equal_date(cl[i].disp[j], d)) {
//printf("%s\n", cl[i].name); //printf("%s\n", cl[i].name);
if(ptr >= 30) { 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]; res[ptr] = cl[i];
ptr++; 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 temp = 0;
int skipped = 0; int skipped = 0;
int a = 0; int a = 0;
printf("Testing %d combinations...\n", n_sim); printf("Testing %d combinations...\n\n", n_sim);
printf("\n");
for(int k = 0; k < n_sim*(1 - (max_score == n_groups*100)); k++) { for(int k = 0; k < n_sim*(1 - (max_score == n_groups*100)); k++) {
if(k >= a) { if(k >= a) {
printf("\x1b[1F"); 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[1F");
printf("\x1b[2K"); printf("\x1b[2K");
if(max_score == 100*n_groups) { 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 { } 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); 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);
printf("Most screwed group is %d with a score of %d/100\n", screwed_group, global_min); info("Most screwed group is %d with a score of %d/100\n", screwed_group, global_min);
printf("Stats for all groups are :\n"); info("Stats for all groups are :");
for(int i = 0; i < n_groups; i++) { 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); int end = time(NULL);
printf("Took %ds to found\n", end-start); info("Took %ds to found", end-start);
free(group_stats); free(group_stats);
free(group_temp); free(group_temp);
free(weeks_len); free(weeks_len);

View File

@ -19,6 +19,7 @@
#include <stdio.h> #include <stdio.h>
#include "logger.h"
#include "structure.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) { void print_all_colleurs(colleur* chads, int n_chads) {
for(int c = 0; c < n_chads; c++) { 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);
} }
} }

132
src/logger.c Normal file
View File

@ -0,0 +1,132 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdarg.h>
#include <string.h>
#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);
}

42
src/logger.h Normal file
View File

@ -0,0 +1,42 @@
#ifndef LOGGER_H_INCLUDED
#define LOGGER_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
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

View File

@ -22,6 +22,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <time.h> #include <time.h>
#include "logger.h"
#include "structure.h" #include "structure.h"
#include "algorithm.h" #include "algorithm.h"
@ -33,6 +34,8 @@ int main(int argc, char **argv) {
fprintf(stderr, "Usage: %s <creneaux> <n_creneaux> <colleurs> <n_colleurs> <n_weeks> <n_groups> <output>\n", argv[0]); fprintf(stderr, "Usage: %s <creneaux> <n_creneaux> <colleurs> <n_colleurs> <n_weeks> <n_groups> <output>\n", argv[0]);
exit(1); exit(1);
} }
create_logger("colloscope.log", LOG_TRACE);
char* path_creneaux = argv[1]; char* path_creneaux = argv[1];
int n_creneaux = str_to_int(argv[2]); 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]); int n_groups = str_to_int(argv[6]);
char* path_output = argv[7]; 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)); srand(time(NULL));
//creneau* edt = import_creneaux("file.txt", 76); //creneau* edt = import_creneaux("file.txt", 76);
@ -66,5 +69,8 @@ int main(int argc, char **argv) {
free(dudes[i].name); free(dudes[i].name);
} }
free(dudes); free(dudes);
destroy_logger();
return 0; return 0;
} }

View File

@ -23,6 +23,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <unistd.h> #include <unistd.h>
#include "logger.h"
#include "structure.h" #include "structure.h"
//static int width = 1200; //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) { bool is_sorted(creneau* edt, int len) {
for(int i = 1; i < len; i++) { for(int i = 1; i < len; i++) {
if(date_dist(edt[i-1].date, edt[i].date) == -1) { 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); warn("Anomaly detected at lane %d (dates are not sorted in ascending order) :", 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); 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);
printf("%d %d %d %d\n", edt[i].date.hour, edt[i].date.day, edt[i].date.month, edt[i].date.year); warn("%d %d %d %d", 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("%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; return false;
} }
} }
printf("No problem detected\n"); info("No problem detected");
return true; return true;
} }
@ -201,7 +202,7 @@ colleur* import_colleurs(char* filename, int n_colleurs, int max_available) {
res[colleur_ptr].mat = INFO; res[colleur_ptr].mat = INFO;
} else { } else {
word[wordlen] = '\0'; word[wordlen] = '\0';
printf("Wait that's illegal (%s)\n", word); fatal("Wait that's illegal (%s)", word);
assert(0); assert(0);
} }
res[colleur_ptr].disp = malloc(sizeof(date)*max_available); 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) { } else if(date_ptr == 2) {
res[colleur_ptr].disp[current-2].month = buffer; res[colleur_ptr].disp[current-2].month = buffer;
} else { } else {
printf("Oh no (%d)\n", date_ptr); fatal("Oh no (%d)", date_ptr);
assert(0); assert(0);
} }
buffer = 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;} // {char* name; int namelen; topic mat; date* disp; int n_disp;}
printf("Successfully imported colleurs\n"); info("Successfully imported colleurs");
fclose(ptr); fclose(ptr);
free(word); 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)) { if(!is_sorted(edt, n_weeks*len_file)) {
assert(0); assert(0);
} }
printf("Imported creneaux successfully\n"); info("Imported creneaux successfully");
return edt; return edt;
} }
@ -409,7 +410,7 @@ colleur* import_colleurs_oneweek(char* filename, int n_colleurs, int n_weeks, in
res[colleur_ptr].mat = INFO; res[colleur_ptr].mat = INFO;
} else { } else {
word[wordlen] = '\0'; word[wordlen] = '\0';
printf("Wait that's illegal (%s)\n", word); fatal("Wait that's illegal (%s)", word);
assert(0); assert(0);
} }
res[colleur_ptr].disp = malloc(sizeof(date)*len_oneweek); 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) { } else if(date_ptr == 2) {
res[colleur_ptr].disp[current-2].month = buffer; res[colleur_ptr].disp[current-2].month = buffer;
} else { } else {
printf("Oh no (%d)\n", date_ptr); fatal("Oh no (%d)", date_ptr);
assert(0); assert(0);
} }
buffer = 0; buffer = 0;
@ -462,7 +463,7 @@ colleur* import_colleurs_oneweek(char* filename, int n_colleurs, int n_weeks, in
fclose(ptr); fclose(ptr);
free(word); free(word);
printf("Imported colleurs with no problems\n"); info("Imported colleurs with no problems");
return res; return res;
} }