messages en couleurs

This commit is contained in:
Hugo LAMY
2022-01-21 16:16:05 +01:00
parent 3ee91785f6
commit 964950f5bd
4 changed files with 43 additions and 10 deletions

View File

@@ -12,7 +12,8 @@ INCLUDES = -I$(HEADERS_D) -I$(LIBFT_D)
HEADERS_D = ./headers HEADERS_D = ./headers
HEADERS = philo.h \ HEADERS = philo.h \
philo_struct.h \ philo_struct.h \
philo_proto.h philo_proto.h \
philo_macro.h
LIBS = -L $(LIBFT_D) -lft \ LIBS = -L $(LIBFT_D) -lft \
-lpthread -lpthread

View File

@@ -11,5 +11,6 @@
# include "philo_struct.h" # include "philo_struct.h"
# include "philo_proto.h" # include "philo_proto.h"
# include "philo_macro.h"
#endif #endif

24
headers/philo_macro.h Normal file
View File

@@ -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

View File

@@ -2,8 +2,22 @@
void print_message(t_philo *philo, char *msg) 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); 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); pthread_mutex_unlock(philo->m_print);
} }
@@ -23,12 +37,10 @@ void init_time(t_philo *philo)
void update_time(t_philo *philo) void update_time(t_philo *philo)
{ {
struct timeval stime; struct timeval stime;
// long int time_stamp;
gettimeofday(&stime, NULL); gettimeofday(&stime, NULL);
philo->t_last_meal_s = stime.tv_sec; philo->t_last_meal_s = stime.tv_sec;
philo->t_last_meal_u = stime.tv_usec; 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) void take_forks(t_philo *philo)
@@ -45,11 +57,6 @@ void take_forks(t_philo *philo)
pthread_mutex_unlock(&(philo->m_fork)); 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 n_phi; // number_of_philosophers
// int t_die; // time_to_die // int t_die; // time_to_die
// int t_eat; // time_to_eat // int t_eat; // time_to_eat
@@ -69,7 +76,7 @@ void *philo_exec(void *arg)
print_message(philo, "is sleeping"); print_message(philo, "is sleeping");
usleep(philo->params->t_slp * 1000); usleep(philo->params->t_slp * 1000);
print_message(philo, "is sleeping"); print_message(philo, "is thinking");
} }
return (NULL); return (NULL);
} }