summary refs log tree commit diff
path: root/collect.c
diff options
context:
space:
mode:
Diffstat (limited to 'collect.c')
-rw-r--r--collect.c23
1 files changed, 14 insertions, 9 deletions
diff --git a/collect.c b/collect.c
index 875213a..2ae9c2e 100644
--- a/collect.c
+++ b/collect.c
@@ -21,13 +21,17 @@
 
 #include "stdlib.c"
 
-char *path;
+FILE *fout = NULL;
 
 void init(int argc, const char *const *argv, const char **envp)
 {
 	environ = envp;
-	path = getenv("TAOSC_COLLECT"); /* TODO: fopen */
-	setvbuf(stderr, NULL, _IOFBF, 0);
+	const char *const path = getenv("TAOSC_OUTPUT");
+	if (path != NULL)
+		fout = fopen(path, "w");
+	if (fout == NULL)
+		fout = stderr;
+	setvbuf(fout, NULL, _IOFBF, 0);
 }
 
 
@@ -36,12 +40,13 @@ void log(const struct STATE *state)
 	static mutex_t mutex = MUTEX_INITIALIZER;
 	if (mutex_lock(&mutex) < 0)
 		return;
-	clearerr(stderr);
-	fprintf(stderr, "[begin]\n");
+	clearerr(fout);
+	fprintf(fout, "[begin]\n");
 	const int64_t *const env = (const int64_t *) state;
-	for (unsigned char i = 1; i < sizeof(state) / sizeof(int64_t); ++i)
-		fprintf(stderr, "%hhu %lld\n", i, env[i]);
-	fprintf(stderr, "[end]\n");
-	fflush(stderr);
+	for (unsigned char i = 1; i < sizeof(*state) / sizeof(*env); ++i)
+		fprintf(fout, "%hhu %lld\n", i, env[i]);
+	fprintf(fout, "[end]\n");
+	fflush(fout);
+	fclose(fout); /* FIXME: reopen */
 	mutex_unlock(&mutex);
 }