mrc0mmand / rpms / libguestfs

Forked from rpms/libguestfs 3 years ago
Clone
Blob Blame History Raw
From b7dd708d309d86625e3d6cb2f479bed5e5b55ee1 Mon Sep 17 00:00:00 2001
From: "Richard W.M. Jones" <rjones@redhat.com>
Date: Wed, 29 Jun 2016 12:51:39 +0100
Subject: [PATCH] p2v: Colourize kernel-conversion status messages from
 virt-p2v.

The messages that come "through" from virt-v2v are already colourized.
However the other messages are not.  This colourizes the ones
generated during kernel-mode conversions.

Note that because of the way journal/syslog works we have to write the
ansi_restore before the end of line, thus code like this:

 ansi_magenta (stdout);
 printf ("%s: %s", guestfs_int_program_name, data);
 ansi_restore (stdout);
 putchar ('\n');

This also adds a "Conversion finished successfully" message (in green).

Thanks: Ming Xie
(cherry picked from commit b15b6e69e37cb3b6520d301442a67b9971ae8d51)
---
 p2v/kernel.c           | 23 +++++++++++++++++++----
 p2v/launch-virt-p2v.in |  4 ++--
 p2v/main.c             | 12 ++++++++++++
 p2v/p2v.h              |  3 +++
 p2v/virt-p2v.pod       |  9 +++++++++
 5 files changed, 45 insertions(+), 6 deletions(-)

diff --git a/p2v/kernel.c b/p2v/kernel.c
index 6d9388b..6914ab5 100644
--- a/p2v/kernel.c
+++ b/p2v/kernel.c
@@ -250,6 +250,11 @@ kernel_conversion (struct config *config, char **cmdline, int cmdline_source)
     exit (EXIT_FAILURE);
   }
 
+  ansi_green (stdout);
+  printf ("Conversion finished successfully.");
+  ansi_restore (stdout);
+  putchar ('\n');
+
   p = get_cmdline_key (cmdline, "p2v.post");
   if (!p) {
     if (geteuid () == 0 && cmdline_source == CMDLINE_SOURCE_PROC_CMDLINE)
@@ -264,8 +269,12 @@ notify_ui_callback (int type, const char *data)
 {
   switch (type) {
   case NOTIFY_LOG_DIR:
-    printf ("%s: remote log directory location: %s\n",
-            guestfs_int_program_name, data);
+    ansi_magenta (stdout);
+    printf ("%s: remote log directory location: ", guestfs_int_program_name);
+    ansi_red (stdout);
+    fputs (data, stdout);
+    ansi_restore (stdout);
+    putchar ('\n');
     break;
 
   case NOTIFY_REMOTE_MESSAGE:
@@ -273,12 +282,18 @@ notify_ui_callback (int type, const char *data)
     break;
 
   case NOTIFY_STATUS:
-    printf ("%s: %s\n", guestfs_int_program_name, data);
+    ansi_magenta (stdout);
+    printf ("%s: %s", guestfs_int_program_name, data);
+    ansi_restore (stdout);
+    putchar ('\n');
     break;
 
   default:
-    printf ("%s: unknown message during conversion: type=%d data=%s\n",
+    ansi_red (stdout);
+    printf ("%s: unknown message during conversion: type=%d data=%s",
             guestfs_int_program_name, type, data);
+    ansi_restore (stdout);
+    putchar ('\n');
   }
 
   fflush (stdout);
diff --git a/p2v/launch-virt-p2v.in b/p2v/launch-virt-p2v.in
index 3e4f1f9..ca971a0 100755
--- a/p2v/launch-virt-p2v.in
+++ b/p2v/launch-virt-p2v.in
@@ -23,7 +23,7 @@
 cmdline=$(</proc/cmdline)
 if [[ $cmdline == *p2v.server=* ]]; then
     # Non-GUI mode, don't run X.  Just run virt-p2v directly.
-    exec @libexecdir@/virt-p2v --iso
+    exec @libexecdir@/virt-p2v --iso --colours
 
 else
     # GUI mode.  Run xinit to start X.  To save one script, we invoke
@@ -32,7 +32,7 @@ else
         cd /
         metacity &
         nm-applet &
-        exec @libexecdir@/virt-p2v --iso
+        exec @libexecdir@/virt-p2v --iso --colours
     else
         xinit "$0" run
     fi
diff --git a/p2v/main.c b/p2v/main.c
index 99d0011..c5ab233 100644
--- a/p2v/main.c
+++ b/p2v/main.c
@@ -43,6 +43,7 @@ char **all_removable;
 char **all_interfaces;
 int is_iso_environment = 0;
 int feature_colours_option = 0;
+int force_colour = 0;
 
 static void udevadm_settle (void);
 static void set_config_defaults (struct config *config);
@@ -55,6 +56,10 @@ static const char options[] = "Vv";
 static const struct option long_options[] = {
   { "help", 0, 0, HELP_OPTION },
   { "cmdline", 1, 0, 0 },
+  { "color", 0, 0, 0 },
+  { "colors", 0, 0, 0 },
+  { "colour", 0, 0, 0 },
+  { "colours", 0, 0, 0 },
   { "iso", 0, 0, 0 },
   { "long-options", 0, 0, 0 },
   { "short-options", 0, 0, 0 },
@@ -77,6 +82,7 @@ usage (int status)
               "Options:\n"
               "  --help                 Display brief help\n"
               " --cmdline=CMDLINE       Used to debug command line parsing\n"
+              " --colours               Use ANSI colour sequences even if not tty\n"
               " --iso                   Running in the ISO environment\n"
               "  -v|--verbose           Verbose messages\n"
               "  -V|--version           Display version and exit\n"
@@ -158,6 +164,12 @@ main (int argc, char *argv[])
         cmdline = parse_cmdline_string (optarg);
         cmdline_source = CMDLINE_SOURCE_COMMAND_LINE;
       }
+      else if (STREQ (long_options[option_index].name, "color") ||
+               STREQ (long_options[option_index].name, "colour") ||
+               STREQ (long_options[option_index].name, "colors") ||
+               STREQ (long_options[option_index].name, "colours")) {
+        force_colour = 1;
+      }
       else if (STREQ (long_options[option_index].name, "iso")) {
         is_iso_environment = 1;
       }
diff --git a/p2v/p2v.h b/p2v/p2v.h
index 3e75690..52d5ccb 100644
--- a/p2v/p2v.h
+++ b/p2v/p2v.h
@@ -61,6 +61,9 @@ extern int is_iso_environment;
 /* True if virt-v2v supports the --colours option. */
 extern int feature_colours_option;
 
+/* virt-p2v --colours option (used by ansi_* macros). */
+extern int force_colour;
+
 /* config.c */
 struct config {
   int verbose;
diff --git a/p2v/virt-p2v.pod b/p2v/virt-p2v.pod
index 421925f..247acce 100644
--- a/p2v/virt-p2v.pod
+++ b/p2v/virt-p2v.pod
@@ -591,6 +591,15 @@ Display help.
 This is used for debugging. Instead of parsing the kernel command line
 from F</proc/cmdline>, parse the string parameter C<CMDLINE>.
 
+=item B<--colors>
+
+=item B<--colours>
+
+Use ANSI colour sequences to colourize messages.  This is the default
+when the output is a tty.  If the output of the program is redirected
+to a file, ANSI colour sequences are disabled unless you use this
+option.
+
 =item B<--iso>
 
 This flag is passed to virt-p2v when it is launched inside the
-- 
1.8.3.1