From d5d446cca6567db81af6c55a51d239ab5e278ff5 Mon Sep 17 00:00:00 2001
From: Steffen Vogel <post@steffenvogel.de>
Date: Sat, 12 May 2018 18:02:18 +0200
Subject: [PATCH] io: add header to villas.human and json formats

---
 lib/formats/csv.c          | 18 +++++++++++++++++-
 lib/formats/villas_human.c | 18 +++++++++++++++++-
 2 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/lib/formats/csv.c b/lib/formats/csv.c
index 8c682306d..c8f6d83c7 100644
--- a/lib/formats/csv.c
+++ b/lib/formats/csv.c
@@ -28,6 +28,7 @@
 #include <villas/formats/csv.h>
 #include <villas/plugin.h>
 #include <villas/sample.h>
+#include <villas/signal.h>
 #include <villas/timing.h>
 
 size_t csv_sprint_single(struct io *io, char *buf, size_t len, struct sample *s)
@@ -159,7 +160,22 @@ void csv_header(struct io *io)
 {
 	FILE *f = io_stream_output(io);
 
-	fprintf(f, "# secs%cnsecs%coffset%csequence%cdata[]\n", CSV_SEPARATOR, CSV_SEPARATOR, CSV_SEPARATOR, CSV_SEPARATOR);
+	fprintf(f, "# secs%cnsecs%coffset%csequence", CSV_SEPARATOR, CSV_SEPARATOR, CSV_SEPARATOR);
+
+	if (io->output.signals) {
+		for (int i = 0; i < list_length(io->output.signals); i++) {
+			struct signal *s = (struct signal *) list_at(io->output.signals, i);
+
+			fprintf(f, "%c%s", CSV_SEPARATOR, s->name);
+
+			if (s->unit)
+				fprintf(f, "[%s]", s->unit);
+		}
+	}
+	else
+		fprintf(f, "%cdata[]", CSV_SEPARATOR);
+
+	fprintf(f, "\n");
 }
 
 static struct plugin p = {
diff --git a/lib/formats/villas_human.c b/lib/formats/villas_human.c
index 357de557c..aa4d22917 100644
--- a/lib/formats/villas_human.c
+++ b/lib/formats/villas_human.c
@@ -29,6 +29,7 @@
 #include <villas/utils.h>
 #include <villas/timing.h>
 #include <villas/sample.h>
+#include <villas/signal.h>
 #include <villas/formats/villas_human.h>
 
 size_t villas_human_sprint_single(struct io *io, char *buf, size_t len, struct sample *s)
@@ -190,7 +191,22 @@ void villas_human_header(struct io *io)
 {
 	FILE *f = io_stream_output(io);
 
-	fprintf(f, "# %-20s\t\t%s\n", "sec.nsec+offset", "data[]");
+	fprintf(f, "# %-20s", "sec.nsec+offset");
+
+	if (io->output.signals) {
+		for (int i = 0; i < list_length(io->output.signals); i++) {
+			struct signal *s = (struct signal *) list_at(io->output.signals, i);
+
+			fprintf(f, "\t%s", s->name);
+
+			if (s->unit)
+				fprintf(f, "[%s]", s->unit);
+		}
+	}
+	else
+		fprintf(f, "\tdata[]");
+
+	fprintf(f, "\n");
 }
 
 static struct plugin p = {