]> git.ozlabs.org Git - petitboot/commitdiff
log: Allow runtime selection of 'debug' log level
authorJeremy Kerr <jk@ozlabs.org>
Fri, 4 Apr 2014 05:05:18 +0000 (13:05 +0800)
committerJeremy Kerr <jk@ozlabs.org>
Mon, 7 Apr 2014 03:49:08 +0000 (11:49 +0800)
Currently, we need to compile with -DDEBUG to implement debug-level
logging in the UIs and discover server.

Since we may not be able to easily replace a system's petitboot
binaries, this change introduces a -v|--verbose option to the discver
server and ncurses UI, which enables debug at runtime. We also move some
of the udev debug code out of an #ifdef DEBUG block.

Since petitboot is generally started on boot, we also add a little
infrastructure to pass -v to petitboot on certain system contitions:
either petitboot.debug on the kernel command line, or a petitboot,debug?
NVRAM property containing the value 'true'.

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
discover/pb-discover.c
discover/udev.c
lib/log/log.c
lib/log/log.h
ui/ncurses/generic-main.c
utils/pb-console
utils/pb-sysinfo

index 713d99d3cee1cdcd1061164a017447df5bc5962e..e895f47653945fcef31fc888e2473d632cc21000 100644 (file)
@@ -29,7 +29,7 @@ static void print_usage(void)
        print_version();
        printf(
 "Usage: pb-discover [-a, --no-autoboot] [-h, --help] [-l, --log log-file]\n"
-"                   [-n, --dry-run] [-V, --version]\n");
+"                   [-n, --dry-run] [-v, --verbose] [-V, --version]\n");
 }
 
 /**
@@ -48,6 +48,7 @@ struct opts {
        const char *log_file;
        enum opt_value dry_run;
        enum opt_value show_version;
+       enum opt_value verbose;
 };
 
 /**
@@ -61,14 +62,16 @@ static int opts_parse(struct opts *opts, int argc, char *argv[])
                {"help",           no_argument,       NULL, 'h'},
                {"log",            required_argument, NULL, 'l'},
                {"dry-run",        no_argument,       NULL, 'n'},
+               {"verbose",        no_argument,       NULL, 'v'},
                {"version",        no_argument,       NULL, 'V'},
                { NULL, 0, NULL, 0},
        };
-       static const char short_options[] = "ahl:nV";
+       static const char short_options[] = "ahl:nvV";
        static const struct opts default_values = {
                .no_autoboot = opt_no,
                .log_file = "/var/log/petitboot/pb-discover.log",
                .dry_run = opt_no,
+               .verbose = opt_no,
        };
 
        *opts = default_values;
@@ -93,6 +96,9 @@ static int opts_parse(struct opts *opts, int argc, char *argv[])
                case 'n':
                        opts->dry_run = opt_yes;
                        break;
+               case 'v':
+                       opts->verbose = opt_yes;
+                       break;
                case 'V':
                        opts->show_version = opt_yes;
                        break;
@@ -147,6 +153,9 @@ int main(int argc, char *argv[])
        }
        pb_log_init(log);
 
+       if (opts.verbose)
+               pb_log_set_debug(true);
+
        pb_log("--- pb-discover ---\n");
 
        /* we look for closed sockets when we write, so ignore SIGPIPE */
index 7db36a7de5f3777428951f9312c0c78de0b62b8a..9e1a5ce8a5155294efe33b5ee817853c3c57fc43 100644 (file)
@@ -263,23 +263,18 @@ static int udev_handle_dev_change(struct pb_udev *udev, struct udev_device *dev)
 static int udev_handle_dev_action(struct udev_device *dev, const char *action)
 {
        struct pb_udev *udev = udev_get_userdata(udev_device_get_udev(dev));
+       struct udev_list_entry *list;
+       const char *name;
 
-#ifdef DEBUG
-       {
-               struct udev_list_entry *list;
-               const char *name;
-
-               list = udev_device_get_properties_list_entry(dev);
-               name = udev_device_get_sysname(dev);
+       list = udev_device_get_properties_list_entry(dev);
+       name = udev_device_get_sysname(dev);
 
-               pb_debug("%s: action %s, device %s\n", __func__, action, name);
-               pb_debug("%s properties:\n", __func__);
+       pb_debug("udev: action %s, device %s\n", action, name);
+       pb_debug("udev: properties:\n");
 
-               for (; list; list = udev_list_entry_get_next(list))
-                       pb_log("\t%-20s: %s\n", udev_list_entry_get_name(list),
-                                       udev_list_entry_get_value(list));
-       } while (0);
-#endif
+       for (; list; list = udev_list_entry_get_next(list))
+               pb_debug("\t%-20s: %s\n", udev_list_entry_get_name(list),
+                               udev_list_entry_get_value(list));
 
        if (!strcmp(action, "add"))
                return udev_handle_dev_add(udev, dev);
index a4f5c22adde5ba9f54263237acfe17bea3117852..44543d03b4ce900368cbeb593e2f08feaf855059 100644 (file)
@@ -41,6 +41,11 @@ void __pb_log_init(FILE *fp, bool _debug)
        debug = _debug;
 }
 
+void pb_log_set_debug(bool _debug)
+{
+       debug = _debug;
+}
+
 FILE *pb_log_get_stream(void)
 {
        static FILE *null_stream;
index e34de33bbe6134ce23a8bcf0c009258e86084b6b..94545960f59690f0c087849a2c87b411f0249ccf 100644 (file)
@@ -15,6 +15,7 @@ void __pb_log_init(FILE *stream, bool debug);
 #define pb_log_init(s) __pb_log_init(s, false)
 #endif
 
+void pb_log_set_debug(bool debug);
 FILE *pb_log_get_stream(void);
 
 #endif /* _LOG_H */
index ad4c0cf9b8f3a1067591482912e648cd4c94d623..67b0b618fac704d7ec25447561adcd1e9b16f627 100644 (file)
@@ -49,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");
 }
 
 /**
@@ -66,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;
 };
 
@@ -79,10 +80,11 @@ 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 char short_options[] = "dhl:svV";
        static const struct opts default_values = { 0 };
 
        *opts = default_values;
@@ -104,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;
@@ -297,6 +302,9 @@ int main(int argc, char *argv[])
 
        pb_log_init(log);
 
+       if (opts.verbose)
+               pb_log_set_debug(true);
+
        pb_log("--- petitboot-nc ---\n");
 
        sa.sa_handler = sig_handler;
index d846ea7614703dd80f590f45a6f74341889dd773..9da899fdf7b242f4c47652cd0d753c0691445253 100644 (file)
@@ -96,9 +96,16 @@ do
        fi
 done
 
+verbose_opt=
+sysinfo_bin=$(dirname $0)/pb-sysinfo
+if [ -x $sysinfo_bin ] && $sysinfo_bin --debug-enabled
+then
+       verbose_opt=--verbose
+fi
+
 while :
 do
-       $ui
+       $ui $verbose_opt
        reset
        echo "Exiting petitboot. Type 'exit' to return."
        $shell
index bd2f0fedc601a8a95d75431266359ba54f6c4d77..001535a46fac14934e7fc5be2a4ccf86317de9f8 100755 (executable)
@@ -7,4 +7,10 @@ case "$1" in
 '--id')
        tr -d '\0' < /proc/device-tree/system-id
        ;;
+'--debug-enabled')
+       exec >/dev/null 2>&1
+       nvram --print-config=petitboot,debug? | grep -q true && exit 0
+       grep -Fq petitboot.debug /proc/cmdline && exit 0
+       exit 1
+       ;;
 esac