From 964950f5bdd49263b3d729b1c31510cef0317d7c Mon Sep 17 00:00:00 2001 From: Hugo LAMY Date: Fri, 21 Jan 2022 16:16:05 +0100 Subject: [PATCH] messages en couleurs --- Makefile | 3 ++- headers/philo.h | 1 + headers/philo_macro.h | 24 ++++++++++++++++++++++++ srcs/exec.c | 25 ++++++++++++++++--------- 4 files changed, 43 insertions(+), 10 deletions(-) create mode 100644 headers/philo_macro.h diff --git a/Makefile b/Makefile index 8752a49..10dd4c3 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,8 @@ INCLUDES = -I$(HEADERS_D) -I$(LIBFT_D) HEADERS_D = ./headers HEADERS = philo.h \ philo_struct.h \ - philo_proto.h + philo_proto.h \ + philo_macro.h LIBS = -L $(LIBFT_D) -lft \ -lpthread diff --git a/headers/philo.h b/headers/philo.h index 5b15273..f2d131d 100644 --- a/headers/philo.h +++ b/headers/philo.h @@ -11,5 +11,6 @@ # include "philo_struct.h" # include "philo_proto.h" +# include "philo_macro.h" #endif diff --git a/headers/philo_macro.h b/headers/philo_macro.h new file mode 100644 index 0000000..0147b5b --- /dev/null +++ b/headers/philo_macro.h @@ -0,0 +1,24 @@ +#ifndef PHILO_MACRO_H +# define PHILO_MACRO_H + +# define GRAY "\e[0;30m" +# define RED "\e[0;31m" +# define GREEN "\e[0;32m" +# define YELLOW "\e[0;33m" +# define BLUE "\e[0;34m" +# define PURPLE "\e[0;35m" +# define CYAN "\e[0;36m" +# define WHITE "\e[0;37m" + +# define B_GRAY "\e[1;30m" +# define B_RED "\e[1;31m" +# define B_GREEN "\e[1;32m" +# define B_YELLOW "\e[1;33m" +# define B_BLUE "\e[1;34m" +# define B_PURPLE "\e[1;35m" +# define B_CYAN "\e[1;36m" +# define B_WHITE "\e[1;37m" + +# define RESET "\e[0m" + +#endif diff --git a/srcs/exec.c b/srcs/exec.c index 4cd665b..8b5da1c 100644 --- a/srcs/exec.c +++ b/srcs/exec.c @@ -2,8 +2,22 @@ void print_message(t_philo *philo, char *msg) { + long int time_stamp; + struct timeval stime; + char *color; + + gettimeofday(&stime, NULL); + time_stamp = (stime.tv_sec - philo->t_start_s) * 1000; + time_stamp += (stime.tv_usec - philo->t_start_u) / 1000; + color = WHITE; + if (ft_strnstr(msg, "eating", ft_strlen(msg))) + color = B_YELLOW; + if (ft_strnstr(msg, "sleeping", ft_strlen(msg))) + color = B_BLUE; + if (ft_strnstr(msg, "thinking", ft_strlen(msg))) + color = B_GREEN; pthread_mutex_lock(philo->m_print); - ft_printf("%i %s\n", philo->p_nbr, msg); + ft_printf("%s%i %i %s%s\n", color, time_stamp, philo->p_nbr, msg, RESET); pthread_mutex_unlock(philo->m_print); } @@ -23,12 +37,10 @@ void init_time(t_philo *philo) void update_time(t_philo *philo) { struct timeval stime; -// long int time_stamp; gettimeofday(&stime, NULL); philo->t_last_meal_s = stime.tv_sec; philo->t_last_meal_u = stime.tv_usec; -// time_stamp = (stime.tv_sec - scd) * 1000 + (stime.tv_usec - mcr) / 1000; } void take_forks(t_philo *philo) @@ -45,11 +57,6 @@ void take_forks(t_philo *philo) pthread_mutex_unlock(&(philo->m_fork)); } -// print_message(philo, "has taken a fork"); -// print_message(philo, "is eating"); -// print_message(philo, "is thinking"); -// print_message(philo, "is sleeping"); -// // int n_phi; // number_of_philosophers // int t_die; // time_to_die // int t_eat; // time_to_eat @@ -69,7 +76,7 @@ void *philo_exec(void *arg) print_message(philo, "is sleeping"); usleep(philo->params->t_slp * 1000); - print_message(philo, "is sleeping"); + print_message(philo, "is thinking"); } return (NULL); }