diff options
| author | Tristan Riehs <tristan.riehs@bordeaux-inp.fr> | 2024-06-01 01:00:22 +0900 | 
|---|---|---|
| committer | Tristan Riehs <tristan.riehs@bordeaux-inp.fr> | 2024-06-01 01:00:22 +0900 | 
| commit | 4307efc624972ee0ddeb835d0d30e3bfdd4d2820 (patch) | |
| tree | 891cb834127ac4485f6deecb7bd6c6fbf8bbe9f7 /src | |
| parent | 0f951e2d72606854ce2689f201332f551a5a2b81 (diff) | |
Improve logging
Also keep track of whether the user got right or wrong. Optimize the
allocation and modification of the log character buffer.
Diffstat (limited to 'src')
| -rw-r--r-- | src/calculer.c | 61 | 
1 files changed, 42 insertions, 19 deletions
| diff --git a/src/calculer.c b/src/calculer.c index 2d6c973..062fff6 100644 --- a/src/calculer.c +++ b/src/calculer.c @@ -30,6 +30,8 @@ static int lower = 0;  static int upper = 100;  static FILE *logfile; +static char logbuf[64]; +#define LOGBUF_OFFSET 11  /* Return a random number x such that lower <= x < upper. */  static int @@ -38,47 +40,60 @@ calculer_rand(void)  	return lower + rand()%(upper-lower);  } +static intptr_t +tp_ms(struct timespec const *tp) +{ +	return tp->tv_sec*1000 + tp->tv_nsec/1000000; +} +  static void *  measure_before(void)  { -	/* TODO: measure time in ms */ -	return (void *) time(NULL); +	struct timespec tp; +	clock_gettime(CLOCK_REALTIME, &tp); +	return (void *)tp_ms(&tp);  }  int -measure_after(void *data) +measure_after(void *data, int correct)  {  	int t0 = (intptr_t) data; -	time_t now = time(NULL); -	int dt = now - t0; -	char buf[64] = {0}; -	struct tm *tm = localtime(&now); -	size_t bytes_written = strftime(buf, sizeof(buf), "%F\t", tm); -	assert(bytes_written); -	bytes_written += sprintf(buf + bytes_written, "%d\n", dt); - -	return bytes_written != fwrite(buf, 1, bytes_written, logfile); +	struct timespec tp; +	clock_gettime(CLOCK_REALTIME, &tp); +	int t1 = tp_ms(&tp); +	int dt = t1 - t0; +	size_t bytes_written = +		LOGBUF_OFFSET + sprintf(logbuf + LOGBUF_OFFSET, +					"%d\t%d\n", dt, correct); + +	return bytes_written != fwrite(logbuf, 1, bytes_written, logfile);  }  int  main(void)  { -	char prompt[31] = {0}; -	int x, y, res; -	char *input; -  	srand(time(NULL));  	logfile = fopen("./calculer.log", "a");  	assert(logfile); +	time_t now = time(NULL); +	struct tm *tm = localtime(&now); +	size_t bytes_written = strftime(logbuf, sizeof(logbuf), "%F\t", tm); +	assert(bytes_written); +  	while (1)  	{ +		char prompt[32] = {0}; +		int x, y, res; +		char *input; +		int input_val; +  		x = calculer_rand();  		y = calculer_rand(); -		void *measure_data = measure_before();  		res = x+y; -		assert(!measure_after(measure_data)); +  		sprintf(prompt, "%d + %d = ", x, y); +		void *measure_data = measure_before();  		input = linenoise(prompt);  		if ((!input) || (*input == '\0')) @@ -88,7 +103,15 @@ main(void)  			exit(0);  		} -		if (atoi(input) == res) +		input_val = atoi(input); +		if (measure_after(measure_data, input_val == res)) +		{ +			free(input); +			fclose(logfile); +			exit(1); +		} + +		if (input_val == res)  			printf("RIGHT\n");  		else  			printf("WRONG\n"); | 
