]> git.dkaiser.de - 42/Philosophers.git/commitdiff
Add ft_log func
authorDominik Kaiser <dkaiser@student.42heilbronn.de>
Sat, 18 Jan 2025 11:50:55 +0000 (12:50 +0100)
committerDominik Kaiser <dkaiser@student.42heilbronn.de>
Sat, 18 Jan 2025 11:50:55 +0000 (12:50 +0100)
philo/include/ft_utils.h
philo/include/philo.h
philo/src/ft_utils.c
philo/src/simulation.c

index 3c9bd698b879062979997ab56b6a6744b174d5d3..4000815276328abdbaaf8fad7bad2e219d06c85b 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/01/17 11:57:44 by dkaiser           #+#    #+#             */
-/*   Updated: 2025/01/17 12:39:17 by dkaiser          ###   ########.fr       */
+/*   Updated: 2025/01/18 12:47:25 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -15,6 +15,7 @@
 
 # include <stdlib.h>
 # include <stdio.h>
+# include <sys/time.h>
 
 /*
 ** Prints error message and returns EXIT_FAILURE
@@ -26,4 +27,5 @@ int ft_err(const char *str);
 */
 int ft_atoi(const char *str);
 
+void ft_log(int id, const char *str);
 #endif
index 9566136310925db9a59d898d9a0df9e9fd52b505..308857be5dd7d0d697064871606b9b9d5896ea1c 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/01/17 10:23:19 by dkaiser           #+#    #+#             */
-/*   Updated: 2025/01/18 11:36:45 by dkaiser          ###   ########.fr       */
+/*   Updated: 2025/01/18 12:46:42 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -17,7 +17,6 @@
 # include <stdlib.h>
 # include <stdio.h>
 # include <pthread.h>
-# include <sys/time.h>
 # include "ft_utils.h"
 
 # define ERR_USAGE "Usage: <nbr_of_philos> <ttd> <tte> <tts> [times_must_eat]"
index 9eb660a7996118ee2f2ddb454c38eeb83ea9608a..7570b1d7632015019692c5e669bc4ea84e5755a1 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/01/17 11:57:11 by dkaiser           #+#    #+#             */
-/*   Updated: 2025/01/17 11:59:28 by dkaiser          ###   ########.fr       */
+/*   Updated: 2025/01/18 12:47:38 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -18,6 +18,16 @@ int ft_err(const char *str)
     return (EXIT_FAILURE);
 }
 
+void ft_log(int id, const char *str)
+{
+       int timestamp_in_ms;
+       struct timeval t;
+
+       gettimeofday(&t, NULL);
+       timestamp_in_ms = (t.tv_sec * 1000) + (t.tv_usec / 1000);
+       printf("%d %d %s\n", timestamp_in_ms, id, str);
+}
+
 int    ft_atoi(const char *str)
 {
        int     result;
index 54ac8e5ac2a6e00ee4e0aa52ec42f7ba85f6df2c..30353850169318e68de0e30b2ee862c4fde338a4 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/01/17 14:38:04 by dkaiser           #+#    #+#             */
-/*   Updated: 2025/01/18 12:41:31 by dkaiser          ###   ########.fr       */
+/*   Updated: 2025/01/18 12:49:42 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -26,8 +26,10 @@ void philo_eat(t_philo *philo)
     pthread_mutex_lock(&left_fork->mutex);
     pthread_mutex_lock(&right_fork->mutex);
     left_fork->available = 0;
+    ft_log(philo->id, "has taken a fork");
     right_fork->available = 0;
-    printf("Philo %d is eating\n", philo->id);
+    ft_log(philo->id, "has taken a fork");
+    ft_log(philo->id, "is eating");
     usleep(1000000);
     left_fork->available = 1;
     right_fork->available = 1;
@@ -37,13 +39,13 @@ void philo_eat(t_philo *philo)
 
 void philo_sleep(t_philo *philo)
 {
-    printf("Philo %d is sleeping\n", philo->id);
+    ft_log(philo->id, "is sleeping");
     usleep(1000000);
 }
 
 void philo_think(t_philo *philo)
 {
-    printf("Philo %d is thinking\n", philo->id);
+    ft_log(philo->id, "is thinking");
 }
 
 int *process_philo(void *arg)