From 1cacc7bf2fbb30538b4b3fb57ea79451ae6a80d3 Mon Sep 17 00:00:00 2001 From: Tristan Riehs Date: Sat, 1 Jun 2024 02:36:17 +0900 Subject: Add option for setting log file --- src/calculer.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/calculer.c b/src/calculer.c index 5eefb87..0e442de 100644 --- a/src/calculer.c +++ b/src/calculer.c @@ -71,8 +71,10 @@ measure_after(void *data, int correct) } #define print_opt_noarg(opt, desc) \ - fprintf(stream, " -"opt"\t"desc"\n") + fprintf(stream, " -"opt"\t\t"desc"\n") +#define print_opt_arg(opt, arg, desc) \ + fprintf(stream, " -"opt" "arg"\t"desc"\n"); void print_usage(FILE *stream) @@ -81,6 +83,7 @@ print_usage(FILE *stream) fprintf(stream, "OPTIONS\n"); print_opt_noarg("V", "Print version and exit."); print_opt_noarg("h", "Print help and exit."); + print_opt_arg("l", "LOGFILE", "Use LOGFILE as log file.\n"); } void @@ -100,9 +103,9 @@ print_version(void) int main(int argc, char *argv[]) { + char *logpath = "./calculer.log"; + srand(time(NULL)); - logfile = fopen("./calculer.log", "a"); - assert(logfile); time_t now = time(NULL); struct tm *tm = localtime(&now); @@ -110,7 +113,7 @@ main(int argc, char *argv[]) assert(bytes_written); int opt; - while ((opt = getopt(argc, argv, ":Vh")) != -1) + while ((opt = getopt(argc, argv, ":Vhl:")) != -1) { switch (opt) { @@ -120,6 +123,9 @@ main(int argc, char *argv[]) case 'h': print_help(); exit(0); + case 'l': + logpath = optarg; + break; case ':': fprintf(stderr, "calculer: option %d requires an argument\n", @@ -132,6 +138,9 @@ main(int argc, char *argv[]) } } + logfile = fopen(logpath, "a"); + assert(logfile); + while (1) { char prompt[32] = {0}; -- cgit v1.2.3