From 764531787aca138f517843bc350f07613d5f0b69 Mon Sep 17 00:00:00 2001
From: Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
Date: Tue, 9 Sep 2014 11:11:15 +0000
Subject: [PATCH] made histogram plotter more fancy

git-svn-id: https://zerberus.eonerc.rwth-aachen.de:8443/svn/s2ss/trunk@227 8ec27952-4edc-4aab-86aa-e87bb2611832
---
 server/include/utils.h |  1 +
 server/src/utils.c     | 27 +++++++++++++++++----------
 2 files changed, 18 insertions(+), 10 deletions(-)

diff --git a/server/include/utils.h b/server/include/utils.h
index 681cb5efd..43024c241 100644
--- a/server/include/utils.h
+++ b/server/include/utils.h
@@ -29,6 +29,7 @@
 #define MAG(str)	"\x1B[35m" str "\x1B[0m" /**< Print str in magenta */
 #define CYN(str)	"\x1B[36m" str "\x1B[0m" /**< Print str in cyan */
 #define WHT(str)	"\x1B[37m" str "\x1B[0m" /**< Print str in white */
+#define BLD(str)	"\x1B[1m"  str "\x1B[0m" /**< Print str in bold */ 
 
 /** The log level which is passed as first argument to print() */
 enum log_level { DEBUG, INFO, WARN, ERROR };
diff --git a/server/src/utils.c b/server/src/utils.c
index 32bfa498e..217af10da 100644
--- a/server/src/utils.c
+++ b/server/src/utils.c
@@ -124,23 +124,30 @@ struct timespec timespec_rate(double rate)
 
 void hist_print(unsigned *hist, int length)
 {
-	char buf[HIST_HEIGHT + 8];
+	char buf[HIST_HEIGHT + 32];
+	int bar;
 	int max = 0;
 
-	/* Get max */
+	/* Get max, first & last */
 	for (int i = 0; i < length; i++) {
 		if (hist[i] > hist[max])
 			max = i;
-
 	}
 
-	/* Print plot */
-	for (int i = 0; i < length; i++) {
-		memset(buf, 0, sizeof(buf));
-		for (int j = 0; j < HIST_HEIGHT * (float) hist[i] / hist[max]; j++)
-			strcat(buf, "#");
 
-		info("%2u: %s", i, buf);
+	/* Print header */
+	info("%2s | %5s | %s", "Id", "Value", "Histogram Plot:");
+
+	/* Print plot */
+	memset(buf, '#', sizeof(buf));
+	for (int i = 0; i < length; i++) {
+		bar = HIST_HEIGHT * (float) hist[i] / hist[max];
+		if (i == max)
+			info("%2u | " RED("%5u") " | " BLD("%.*s"), i, hist[i], bar, buf);
+		else if (hist[i] == 0)
+			info("%2u | " GRN("%5u") " | "     "%.*s",  i, hist[i], bar, buf);
+		else
+			info("%2u | "     "%5u"  " | "     "%.*s",  i, hist[i], bar, buf);
 	}
 }
 
@@ -156,5 +163,5 @@ void hist_dump(unsigned *hist, int length)
 		strncat(buf, tok, sizeof(buf)-strlen(buf));
 	}
 
-	info("hist = [ %s]", buf);
+	info("Matlab: hist = [ %s]", buf);
 }