]> git.ozlabs.org Git - petitboot/blobdiff - ui/ncurses/generic-main.c
log: Allow runtime selection of 'debug' log level
[petitboot] / ui / ncurses / generic-main.c
index 5d8debbbacd09ba3a53c9f49ab7e219f11e386aa..67b0b618fac704d7ec25447561adcd1e9b16f627 100644 (file)
 #include "config.h"
 #endif
 
-#define _GNU_SOURCE
+#include <assert.h>
 #include <errno.h>
 #include <getopt.h>
 #include <signal.h>
 #include <stdlib.h>
 #include <string.h>
+#include <limits.h>
 #include <sys/time.h>
 
 #include "log/log.h"
@@ -36,6 +37,8 @@
 #include "ui/common/discover-client.h"
 #include "nc-cui.h"
 
+extern const char *main_menu_help_text;
+
 static void print_version(void)
 {
        printf("petitboot-nc (" PACKAGE_NAME ") " PACKAGE_VERSION "\n");
@@ -46,7 +49,7 @@ static void print_usage(void)
        print_version();
        printf(
 "Usage: petitboot-nc [-h, --help] [-l, --log log-file]\n"
-"                    [-s, --start-daemon] [-V, --version]\n");
+"                    [-s, --start-daemon] [-v, --verbose] [-V, --version]\n");
 }
 
 /**
@@ -63,6 +66,7 @@ struct opts {
        enum opt_value show_help;
        const char *log_file;
        enum opt_value start_daemon;
+       enum opt_value verbose;
        enum opt_value show_version;
 };
 
@@ -76,13 +80,12 @@ static int opts_parse(struct opts *opts, int argc, char *argv[])
                {"help",         no_argument,       NULL, 'h'},
                {"log",          required_argument, NULL, 'l'},
                {"start-daemon", no_argument,       NULL, 's'},
+               {"verbose",      no_argument,       NULL, 'v'},
                {"version",      no_argument,       NULL, 'V'},
                { 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 char short_options[] = "dhl:svV";
+       static const struct opts default_values = { 0 };
 
        *opts = default_values;
 
@@ -103,6 +106,9 @@ static int opts_parse(struct opts *opts, int argc, char *argv[])
                case 's':
                        opts->start_daemon = opt_yes;
                        break;
+               case 'v':
+                       opts->verbose = opt_yes;
+                       break;
                case 'V':
                        opts->show_version = opt_yes;
                        break;
@@ -115,6 +121,31 @@ static int opts_parse(struct opts *opts, int argc, char *argv[])
        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.
@@ -126,6 +157,24 @@ struct pb_cui {
        struct cui *cui;
 };
 
+static int pmenu_sysinfo(struct pmenu_item *item)
+{
+       cui_show_sysinfo(cui_from_item(item));
+       return 0;
+}
+
+static int pmenu_config(struct pmenu_item *item)
+{
+       cui_show_config(cui_from_item(item));
+       return 0;
+}
+
+static int pmenu_reinit(struct pmenu_item *item)
+{
+       cui_send_reinit(cui_from_item(item));
+       return 0;
+}
+
 /**
  * pb_mm_init - Setup the main menu instance.
  */
@@ -136,22 +185,31 @@ static struct pmenu *pb_mm_init(struct pb_cui *pb_cui)
        struct pmenu *m;
        struct pmenu_item *i;
 
-       m = pmenu_init(pb_cui->cui, 1, cui_on_exit);
+       m = pmenu_init(pb_cui->cui, 5, cui_on_exit);
 
        if (!m) {
                pb_log("%s: failed\n", __func__);
                return NULL;
        }
 
-       m->on_open = cui_on_open;
+       m->on_new = cui_item_new;
 
-       m->scr.frame.title = talloc_asprintf(m,
+       m->scr.frame.ltitle = talloc_asprintf(m,
                "Petitboot (" PACKAGE_VERSION ")");
+       m->scr.frame.rtitle = NULL;
        m->scr.frame.help = talloc_strdup(m,
-               "ESC=exit, Enter=accept, e=edit, o=open");
+               "Enter=accept, e=edit, n=new, x=exit, h=help");
        m->scr.frame.status = talloc_strdup(m, "Welcome to Petitboot");
 
-       i = pmenu_item_init(m, 0, "Exit to Shell");
+       i = pmenu_item_init(m, 0, " ");
+       item_opts_off(i->nci, O_SELECTABLE);
+       i = pmenu_item_init(m, 1, "System information");
+       i->on_execute = pmenu_sysinfo;
+       i = pmenu_item_init(m, 2, "System configuration");
+       i->on_execute = pmenu_config;
+       i = pmenu_item_init(m, 3, "Rescan devices");
+       i->on_execute = pmenu_reinit;
+       i = pmenu_item_init(m, 4, "Exit to shell");
        i->on_execute = pmenu_exit_cb;
 
        result = pmenu_setup(m);
@@ -162,6 +220,9 @@ static struct pmenu *pb_mm_init(struct pb_cui *pb_cui)
                goto fail_setup;
        }
 
+       m->help_title = "main menu";
+       m->help_text = main_menu_help_text;
+
        menu_opts_off(m->ncm, O_SHOWDESC);
        set_menu_mark(m->ncm, " *");
        set_current_item(m->ncm, i->nci);
@@ -180,10 +241,6 @@ static void sig_handler(int signum)
        DBGS("%d\n", signum);
 
        switch (signum) {
-       case SIGALRM:
-               if (pb.cui)
-                       ui_timer_sigalrm(&pb.cui->timer);
-               break;
        case SIGWINCH:
                if (pb.cui)
                        cui_resize(pb.cui);
@@ -207,9 +264,11 @@ static void sig_handler(int signum)
 int main(int argc, char *argv[])
 {
        static struct sigaction sa;
+       const char *log_filename;
        int result;
        int cui_result;
        struct opts opts;
+       FILE *log;
 
        result = opts_parse(&opts, argc, argv);
 
@@ -228,17 +287,23 @@ int main(int argc, char *argv[])
                return EXIT_SUCCESS;
        }
 
-       if (strcmp(opts.log_file, "-")) {
-               FILE *log = fopen(opts.log_file, "a");
+       if (opts.log_file)
+               log_filename = opts.log_file;
+       else
+               log_filename = default_log_filename();
 
-               assert(log);
-               pb_log_set_stream(log);
-       } else
-               pb_log_set_stream(stderr);
+       log = stderr;
+       if (strcmp(log_filename, "-")) {
+               log = fopen(log_filename, "a");
 
-#if defined(DEBUG)
-       pb_log_always_flush(1);
-#endif
+               if (!log)
+                       log = fopen("/dev/null", "a");
+       }
+
+       pb_log_init(log);
+
+       if (opts.verbose)
+               pb_log_set_debug(true);
 
        pb_log("--- petitboot-nc ---\n");
 
@@ -260,7 +325,6 @@ int main(int argc, char *argv[])
                return EXIT_FAILURE;
 
        pb.mm = pb_mm_init(&pb);
-       ui_timer_disable(&pb.cui->timer);
 
        cui_result = cui_run(pb.cui, pb.mm, 0);