2024-07-08 10:33:28 +02:00
|
|
|
/* ************************************************************************** */
|
|
|
|
/* */
|
|
|
|
/* ::: :::::::: */
|
|
|
|
/* log.c :+: :+: :+: */
|
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
/* Created: 2024/07/07 16:12:20 by adjoly #+# #+# */
|
2024-08-12 20:52:02 +02:00
|
|
|
/* Updated: 2024/08/12 19:38:58 by adjoly ### ########.fr */
|
2024-07-08 10:33:28 +02:00
|
|
|
/* */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
|
|
#include "philo.h"
|
|
|
|
#include "philo_msg.h"
|
2024-07-21 20:55:35 +02:00
|
|
|
#include <stdio.h>
|
2024-07-08 10:33:28 +02:00
|
|
|
|
2024-08-12 20:52:02 +02:00
|
|
|
bool print_err(uint8_t error)
|
|
|
|
{
|
|
|
|
if (error == 0)
|
|
|
|
printf(ERR_MAX_PHIL);
|
|
|
|
else if (error == 1)
|
|
|
|
printf(ERR_MAX_DIE_TIME);
|
|
|
|
else if (error == 2)
|
|
|
|
printf(ERR_MAX_EAT_TIME);
|
|
|
|
else if (error == 3)
|
|
|
|
printf(ERR_MAX_SLEEP_TIME);
|
|
|
|
else if (error == 4)
|
|
|
|
printf(ERR_MAX_MEAL);
|
|
|
|
else if (error == 5)
|
|
|
|
printf(ERR_NB_ARG);
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
|
2024-08-08 18:48:47 +02:00
|
|
|
void log_philo(t_philo *philo)
|
2024-07-08 10:33:28 +02:00
|
|
|
{
|
2024-07-23 18:11:44 +02:00
|
|
|
uint32_t timestamp;
|
2024-07-25 16:13:23 +02:00
|
|
|
struct timeval t1;
|
2024-07-21 20:55:35 +02:00
|
|
|
static pthread_mutex_t print = {0};
|
|
|
|
|
2024-07-25 16:13:23 +02:00
|
|
|
gettimeofday(&t1, NULL);
|
2024-07-21 20:55:35 +02:00
|
|
|
pthread_mutex_lock(&print);
|
2024-08-12 20:52:02 +02:00
|
|
|
timestamp = get_time_in_ms(philo->t0, t1);
|
2024-08-08 18:48:47 +02:00
|
|
|
if (philo->state == EAT)
|
|
|
|
printf("%u %hu %s", timestamp, philo->id, EATING_MSG);
|
|
|
|
else if (philo->state == THINK)
|
|
|
|
printf("%u %hu %s", timestamp, philo->id, THINK_MSG);
|
|
|
|
else if (philo->state == SLEEP)
|
|
|
|
printf("%u %hu %s", timestamp, philo->id, SLEEP_MSG);
|
|
|
|
else if (philo->state == DEAD)
|
|
|
|
printf("%u %hu %s", timestamp, philo->id, DIED_MSG);
|
|
|
|
else if (philo->state == FORK_TAKEN)
|
|
|
|
printf("%u %hu %s", timestamp, philo->id, FORK_MSG);
|
2024-07-21 20:55:35 +02:00
|
|
|
pthread_mutex_unlock(&print);
|
2024-07-08 10:33:28 +02:00
|
|
|
}
|