aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTristan Riehs <tristan.riehs@bordeaux-inp.fr>2024-06-01 02:36:17 +0900
committerTristan Riehs <tristan.riehs@bordeaux-inp.fr>2024-06-01 02:36:17 +0900
commit1cacc7bf2fbb30538b4b3fb57ea79451ae6a80d3 (patch)
treeafc688cf45d1f111a3a62f3203cd04779f2b9ca4
parent7da497992bb69f356ef25929137a3ba13c9a75ae (diff)
Add option for setting log file
-rw-r--r--src/calculer.c17
1 files 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};