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
This commit is contained in:
Richard W.M. Jones
2016-06-29 12:51:39 +01:00
parent d2abfc7b48
commit b15b6e69e3
5 changed files with 45 additions and 6 deletions

View File

@@ -243,6 +243,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)
@@ -257,8 +262,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:
@@ -266,12 +275,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);

View File

@@ -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
@@ -45,7 +45,7 @@ else
metacity &
nm-applet &
esac
exec @libexecdir@/virt-p2v --iso
exec @libexecdir@/virt-p2v --iso --colours
else
xinit "$0" run
fi

View File

@@ -44,6 +44,7 @@ char **all_removable;
char **all_interfaces;
int is_iso_environment = 0;
int feature_colours_option = 0;
int force_colour = 0;
static const char *test_disk = NULL;
@@ -58,6 +59,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 },
@@ -81,6 +86,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"
" --test-disk=DISK.IMG For testing, use disk as /dev/sda\n"
" -v|--verbose Verbose messages\n"
@@ -154,6 +160,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;
}

View File

@@ -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;

View File

@@ -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