Browse code

common.c: introduce a verbosity level.

Xavier G authored on 25/08/2016 01:35:19
Showing 1 changed files
... ...
@@ -26,6 +26,12 @@
26 26
 
27 27
 
28 28
 unsigned int *array;
29
+int verbose;
30
+
31
+
32
+#define VERBOSE(level, format, ...) \
33
+	if (verbose >= level) fprintf(stdout, format, __VA_ARGS__);
34
+
29 35
 
30 36
 void array_fill(unsigned int *array, unsigned int count, unsigned int max) {
31 37
 	if (!array || !count) return;
... ...
@@ -53,7 +59,7 @@ int usage(char *program_name) {
53 59
 
54 60
 int main(int argc, char **argv, char **envp) {
55 61
 	int count_arg;
56
-	char *endptr;
62
+	char *endptr, *verbose_ve;
57 63
 	struct timeval before_start, after_end, diff;
58 64
 	unsigned int count;
59 65
 	unsigned int max;
... ...
@@ -61,6 +67,7 @@ int main(int argc, char **argv, char **envp) {
61 67
 	/* By default, generate an array of 100 integers between 0 and 10000: */
62 68
 	count = 100;
63 69
 	max = 10000;
70
+	verbose = -1;
64 71
 
65 72
 	/* Accept a single, optional command-line argument to override count: */
66 73
 	if (argc > 2) {
... ...
@@ -79,6 +86,11 @@ int main(int argc, char **argv, char **envp) {
79 86
 		}
80 87
 	}
81 88
 
89
+	/* Allow overriding verbosity through an environment variable: */
90
+	if ((verbose_ve = getenv("SORTING_VERBOSE"))) {
91
+		verbose = strtol(verbose_ve, NULL, 10);
92
+	}
93
+
82 94
 	/* Require memory for an array of integers, fill it with random crap: */
83 95
 	array = malloc(sizeof(unsigned int) * count);
84 96
 	srand(time(NULL));