]> sourceware.org Git - lvm2.git/commitdiff
lvm: shell: extend log report to cover whole lvm shell's main loop
authorPeter Rajnoha <prajnoha@redhat.com>
Thu, 4 Aug 2016 11:42:57 +0000 (13:42 +0200)
committerPeter Rajnoha <prajnoha@redhat.com>
Tue, 9 Aug 2016 16:49:10 +0000 (18:49 +0200)
When lvm commands are executed in lvm shell, we cover the whole lvm
command execution within this shell now. That means, all messages logged
and status caught during each command execution is now recorded in the
log report, including overall command's return code.

lib/commands/toolcontext.c
tools/lvm.c
tools/reporter.c
tools/toollib.c

index 57d3bdc294d108518ecc0dbee5ed85c242c3cf56..a7f76ba9142903ceabdcb24482368b8d64389113 100644 (file)
@@ -2218,11 +2218,6 @@ void destroy_toolcontext(struct cmd_context *cmd)
        if (cmd->cft_def_hash)
                dm_hash_destroy(cmd->cft_def_hash);
 
-       if (cmd->cmd_report.log_rh) {
-               dm_report_free(cmd->cmd_report.log_rh);
-               cmd->cmd_report.log_rh = NULL;
-       }
-
        if (cmd->libmem)
                dm_pool_destroy(cmd->libmem);
 
index c2d509669736009fe54974bf035cd8e720e82589..2b95c418776b0c1db7554de616fc5ae65dc42fb0 100644 (file)
@@ -200,6 +200,12 @@ static int _log_shell_command_status(struct cmd_context *cmd, int ret_code)
                             stored_errno(), ret_code);
 }
 
+static void _discard_log_report_content(struct cmd_context *cmd)
+{
+       if (cmd->cmd_report.log_rh)
+               dm_report_destroy_rows(cmd->cmd_report.log_rh);
+}
+
 int lvm_shell(struct cmd_context *cmd, struct cmdline_context *cmdline)
 {
        log_report_t saved_log_report_state = log_get_report_state();
@@ -210,65 +216,76 @@ int lvm_shell(struct cmd_context *cmd, struct cmdline_context *cmdline)
        rl_attempted_completion_function = (rl_completion_func_t *) _completion;
 
        _read_history(cmd);
-
        _cmdline = cmdline;
 
        cmd->is_interactive = 1;
+
+       if (!report_format_init(cmd))
+               return_ECMD_FAILED;
+
        log_set_report_context(LOG_REPORT_CONTEXT_SHELL);
        log_set_report_object_type(LOG_REPORT_OBJECT_TYPE_CMD);
 
        while (1) {
+               log_set_report(cmd->cmd_report.log_rh);
                log_set_report_object_name_and_id(NULL, NULL);
+
                free(input);
                input = readline("lvm> ");
 
                /* EOF */
                if (!input) {
+                       _discard_log_report_content(cmd);
                        /* readline sends prompt to stdout */
                        printf("\n");
                        break;
                }
 
                /* empty line */
-               if (!*input)
+               if (!*input) {
+                       _discard_log_report_content(cmd);
                        continue;
+               }
 
                add_history(input);
 
                argv = args;
 
                if (lvm_split(input, &argc, argv, MAX_ARGS) == MAX_ARGS) {
+                       _discard_log_report_content(cmd);
                        log_error("Too many arguments, sorry.");
                        continue;
                }
 
-               if (!argc)
+               if (!argc) {
+                       _discard_log_report_content(cmd);
                        continue;
+               }
 
                if (!strcmp(argv[0], "lvm")) {
                        argv++;
                        argc--;
                }
 
-               if (!argc)
+               if (!argc) {
+                       _discard_log_report_content(cmd);
                        continue;
+               }
 
                log_set_report_object_name_and_id(argv[0], NULL);
 
+               is_lastlog_cmd = !strcmp(argv[0], "lastlog");
+
+               if (!is_lastlog_cmd)
+                       _discard_log_report_content(cmd);
+
                if (!strcmp(argv[0], "quit") || !strcmp(argv[0], "exit")) {
+                       _discard_log_report_content(cmd);
                        remove_history(history_length - 1);
                        log_error("Exiting.");
                        break;
                }
 
-               is_lastlog_cmd = !strcmp(argv[0], "lastlog");
-
-               if (cmd->cmd_report.log_rh && !is_lastlog_cmd) {
-                       /* drop old log report */
-                       dm_report_free(cmd->cmd_report.log_rh);
-                       cmd->cmd_report.log_rh = NULL;
-               }
-
                ret = lvm_run_command(cmd, argc, argv);
                if (ret == ENO_SUCH_CMD)
                        log_error("No such command '%s'.  Try 'help'.",
@@ -282,12 +299,35 @@ int lvm_shell(struct cmd_context *cmd, struct cmdline_context *cmdline)
 
                if (!is_lastlog_cmd)
                        _log_shell_command_status(cmd, ret);
+
+               log_set_report(NULL);
+               dm_report_group_output_and_pop_all(cmd->cmd_report.report_group);
+
+               if (cmd->cmd_report.log_rh &&
+                   !(dm_report_group_push(cmd->cmd_report.report_group,
+                                          cmd->cmd_report.log_rh,
+                                          (void *) cmd->cmd_report.log_name))) {
+                       log_set_report(NULL);
+                       log_error("Failed to add log report.");
+                       break;
+               }
        }
 
        log_restore_report_state(saved_log_report_state);
        cmd->is_interactive = 0;
 
        free(input);
+
+       if (cmd->cmd_report.report_group) {
+               dm_report_group_destroy(cmd->cmd_report.report_group);
+               cmd->cmd_report.report_group = NULL;
+       }
+
+       if (cmd->cmd_report.log_rh) {
+               dm_report_free(cmd->cmd_report.log_rh);
+               cmd->cmd_report.report_group = NULL;
+       }
+
        return 0;
 }
 
index d386efca092ceb187c0ce32ad11467174db6036c..4a622ed35bc7611c2abe1adea764a75a342f8e0b 100644 (file)
@@ -1387,6 +1387,11 @@ static int _report(struct cmd_context *cmd, int argc, char **argv, report_type_t
        } else
                r = _do_report(cmd, handle, &args, single_args);
 
+       if (!args.log_only && !dm_report_group_pop(cmd->cmd_report.report_group)) {
+               log_error("Failed to finalize main report section in report group.");
+               r = ECMD_FAILED;
+       }
+
        destroy_processing_handle(cmd, handle);
        return r;
 }
@@ -1508,32 +1513,22 @@ bad:
        return 0;
 }
 
-int lastlog(struct cmd_context *cmd, int argc, char **argv)
+int lastlog(struct cmd_context *cmd, int argc __attribute((unused)), char **argv __attribute__((unused)))
 {
-       struct dm_report_group *report_group = NULL;
        const char *selection;
-       int r = ECMD_FAILED;
 
        if (!cmd->cmd_report.log_rh) {
                log_error("No log report stored.");
-               goto out;
+               return ECMD_FAILED;
        }
 
        if (!_do_report_get_selection(cmd, CMDLOG, 1, NULL, &selection))
-               goto_out;
+               return_ECMD_FAILED;
 
        if (!dm_report_set_selection(cmd->cmd_report.log_rh, selection)) {
                log_error("Failed to set selection for log report.");
-               goto out;
+               return ECMD_FAILED;
        }
 
-       if (!dm_report_output(cmd->cmd_report.log_rh) ||
-           !dm_report_group_pop(report_group))
-               goto_out;
-
-       r = ECMD_PROCESSED;
-out:
-       if (!dm_report_group_destroy(report_group))
-               stack;
-       return r;
+       return ECMD_PROCESSED;
 }
index bcf94ead2597ad0607d0611ca47f52a3098b19c0..e24946fffbf47614da6c48072d88c5514036ab3d 100644 (file)
@@ -1798,11 +1798,15 @@ void destroy_processing_handle(struct cmd_context *cmd, struct processing_handle
 
                log_restore_report_state(cmd->cmd_report.saved_log_report_state);
 
-               if (!dm_report_group_destroy(cmd->cmd_report.report_group))
-                       stack;
-               if (!cmd->is_interactive && cmd->cmd_report.log_rh) {
-                       dm_report_free(cmd->cmd_report.log_rh);
-                       cmd->cmd_report.log_rh = NULL;
+               if (!cmd->is_interactive) {
+                       if (!dm_report_group_destroy(cmd->cmd_report.report_group))
+                               stack;
+                       cmd->cmd_report.report_group = NULL;
+
+                       if (cmd->cmd_report.log_rh) {
+                               dm_report_free(cmd->cmd_report.log_rh);
+                               cmd->cmd_report.log_rh = NULL;
+                       }
                }
                /*
                 * TODO: think about better alternatives:
This page took 0.043495 seconds and 5 git commands to generate.