]> git.ozlabs.org Git - petitboot/commitdiff
ui/ncurses: Use tty name in default log filename
authorJeremy Kerr <jk@ozlabs.org>
Thu, 3 Apr 2014 07:17:27 +0000 (15:17 +0800)
committerJeremy Kerr <jk@ozlabs.org>
Thu, 3 Apr 2014 07:17:27 +0000 (15:17 +0800)
When we have multiple ncurses UIs running, we'd like to log to separate
files. Currenly, all UIs log to the same file, which makes it diffifult
to determine which UI is logging each message.

This change uses the output of ttyname() (sanitised appropriately) as a
component of the default log filename.

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
ui/ncurses/generic-main.c

index 2cfb9c319f634d2570662b350d3ef598e8a9056c..ad4c0cf9b8f3a1067591482912e648cd4c94d623 100644 (file)
@@ -28,6 +28,7 @@
 #include <signal.h>
 #include <stdlib.h>
 #include <string.h>
 #include <signal.h>
 #include <stdlib.h>
 #include <string.h>
+#include <limits.h>
 #include <sys/time.h>
 
 #include "log/log.h"
 #include <sys/time.h>
 
 #include "log/log.h"
@@ -82,9 +83,7 @@ static int opts_parse(struct opts *opts, int argc, char *argv[])
                { NULL,          0,                 NULL, 0},
        };
        static const char short_options[] = "dhl:sV";
                { NULL,          0,                 NULL, 0},
        };
        static const char short_options[] = "dhl:sV";
-       static const struct opts default_values = {
-               .log_file = "/var/log/petitboot/petitboot-nc.log",
-       };
+       static const struct opts default_values = { 0 };
 
        *opts = default_values;
 
 
        *opts = default_values;
 
@@ -117,6 +116,31 @@ static int opts_parse(struct opts *opts, int argc, char *argv[])
        return 0;
 }
 
        return 0;
 }
 
+static char *default_log_filename(void)
+{
+       const char *base = "/var/log/petitboot/petitboot-nc";
+       static char name[PATH_MAX];
+       char *tty;
+       int i;
+
+       tty = ttyname(STDIN_FILENO);
+
+       /* strip /dev/ */
+       if (tty && !strncmp(tty, "/dev/", 5))
+               tty += 5;
+
+       /* change slashes to hyphens */
+       for (i = 0; tty && tty[i]; i++)
+               if (tty[i] == '/')
+                       tty[i] = '-';
+
+       if (!tty || !*tty)
+               tty = "unknown";
+
+       snprintf(name, sizeof(name), "%s.%s.log", base, tty);
+
+       return name;
+}
 /**
  * struct pb_cui - Main cui program instance.
  * @mm: Main menu.
 /**
  * struct pb_cui - Main cui program instance.
  * @mm: Main menu.
@@ -235,6 +259,7 @@ static void sig_handler(int signum)
 int main(int argc, char *argv[])
 {
        static struct sigaction sa;
 int main(int argc, char *argv[])
 {
        static struct sigaction sa;
+       const char *log_filename;
        int result;
        int cui_result;
        struct opts opts;
        int result;
        int cui_result;
        struct opts opts;
@@ -257,9 +282,14 @@ int main(int argc, char *argv[])
                return EXIT_SUCCESS;
        }
 
                return EXIT_SUCCESS;
        }
 
+       if (opts.log_file)
+               log_filename = opts.log_file;
+       else
+               log_filename = default_log_filename();
+
        log = stderr;
        log = stderr;
-       if (strcmp(opts.log_file, "-")) {
-               log = fopen(opts.log_file, "a");
+       if (strcmp(log_filename, "-")) {
+               log = fopen(log_filename, "a");
 
                if (!log)
                        log = fopen("/dev/null", "a");
 
                if (!log)
                        log = fopen("/dev/null", "a");