From 5468f737e1eb021f8a69fe3ba559c43aa22d1455 Mon Sep 17 00:00:00 2001 From: Nguyễn Gia Phong Date: Tue, 19 Nov 2024 11:41:45 +0900 Subject: Polish a tad --- collect.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) (limited to 'collect.c') 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); } -- cgit 1.4.1