summary refs log tree commit diff
path: root/collect.c
diff options
context:
space:
mode:
authorNguyễn Gia Phong <cnx@loang.net>2024-11-19 11:41:45 +0900
committerNguyễn Gia Phong <cnx@loang.net>2024-11-19 11:41:45 +0900
commit5468f737e1eb021f8a69fe3ba559c43aa22d1455 (patch)
treed84e2d9fdd6494e481dea6b35afe611db3774050 /collect.c
parent95f3fe2b800940f75949b069f50a2da4712435fd (diff)
downloadtaosc-main.tar.gz
Polish a tad HEAD main
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);
 }